Skip to content

Commit e4b7210

Browse files
authored
added twilio sms api (#25)
* wrap response in json * cleanup and suggested fixes * bump version for releasea authors: songsc, dtoki
1 parent 77b539b commit e4b7210

11 files changed

Lines changed: 148 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.envrc
12
zero-notification-service
23
.history/
34
api/openapi.yaml

.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ internal/server/api_health.go
66
internal/server/api_health_service.go
77
internal/server/api_notification.go
88
internal/server/api_notification_service.go
9+
internal/server/api_sms.go
10+
internal/server/api_sms_service.go
911
internal/server/helpers.go
1012
internal/server/impl.go
1113
internal/server/model_email_recipient.go
@@ -20,6 +22,8 @@ internal/server/model_send_mail_request.go
2022
internal/server/model_send_mail_response.go
2123
internal/server/model_send_slack_message_request.go
2224
internal/server/model_send_slack_message_response.go
25+
internal/server/model_send_sms_request.go
26+
internal/server/model_send_sms_response.go
2327
internal/server/model_slack_message.go
2428
internal/server/model_slack_recipient.go
2529
internal/server/routers.go

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ A number of environment variables are available to configure the service at runt
3131
| GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS | The number of seconds the application will continue servicing in-flight requests before the application stops after it receives an interrupt signal | 10 |
3232
| STRUCTURED_LOGGING | If enabled, logs will be in JSON format, and only above INFO level | false |
3333
| ALLOW_EMAIL_TO_DOMAINS | A comma separated list of domains. Only addresses in this list can have email sent to them. If empty, disable this "sandboxing" functionality. | |
34+
| TWILIO_ACCOUNT_ID | The Account ID for Twilio | |
35+
| TWILIO_AUTH_TOKEN | The Account Auth Token for Twilio | |
36+
| TWILIO_PHONE_NUMBER | The Assigned Twilio Phone Number | |
3437

3538

3639
### Releasing a new version on GitHub and Brew

api/notification-service.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ paths:
8080
schema:
8181
$ref: '#/components/schemas/Error'
8282

83+
/sms/send:
84+
post:
85+
summary: Send an SMS
86+
operationId: sendSMS
87+
tags:
88+
- sms
89+
requestBody:
90+
description: Parameters of the message to send
91+
required: true
92+
content:
93+
application/json:
94+
schema:
95+
$ref: '#/components/schemas/SendSMSRequest'
96+
responses:
97+
200:
98+
description: OK
99+
content:
100+
application/json:
101+
schema:
102+
type: string
103+
default:
104+
description: unexpected error
105+
content:
106+
application/json:
107+
schema:
108+
$ref: '#/components/schemas/Error'
109+
83110
/notification/slack/send:
84111
post:
85112
summary: Send a Slack message
@@ -278,6 +305,23 @@ components:
278305
description: Schedule these mesages to go out at the time specified by this UNIX timestamp
279306
format: int64
280307

308+
SendSMSRequest:
309+
type: object
310+
required:
311+
- recipientPhoneNumber
312+
- message
313+
properties:
314+
recipientPhoneNumber:
315+
type: string
316+
message:
317+
type: string
318+
319+
SendSMSResponse:
320+
type: object
321+
properties:
322+
message:
323+
type: string
324+
281325
SlackRecipient:
282326
type: object
283327
required:

charts/zero-notifcation-service/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.0.7
18+
version: 0.0.8
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
23-
appVersion: 0.0.10
23+
appVersion: 0.0.11

charts/zero-notifcation-service/templates/secret.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ type: Opaque
77
data:
88
SENDGRID_API_KEY: {{ .Values.application.sendgridApiKey | b64enc | quote }}
99
SLACK_API_KEY: {{ .Values.application.slackApiKey | b64enc | quote }}
10+
TWILIO_ACCOUNT_ID: {{ .Values.application.twilioAccountID | b64enc | quote }}
11+
TWILIO_AUTH_TOKEN: {{ .Values.application.twilioAuthToken | b64enc | quote }}

charts/zero-notifcation-service/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ affinity: {}
8181
application:
8282
sendgridApiKey:
8383
slackApiKey:
84+
twilioAccountID:
85+
twilioAuthToken:
86+
twilioPhoneNumber:
8487
gracefulShutdownTimeout: 10
8588
structuredLogging: true
8689
allowEmailToDomains:

cmd/server/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ func main() {
4848
EmailApiService := service.NewEmailApiService(config)
4949
EmailApiController := server.NewEmailApiController(EmailApiService)
5050

51+
SmsApiService := service.NewSmsApiService(config)
52+
SmsApiController := server.NewSmsApiController(SmsApiService)
53+
5154
HealthApiService := service.NewHealthApiService(config)
5255
HealthApiController := server.NewHealthApiController(HealthApiService)
5356

5457
NotificationApiService := service.NewNotificationApiService(config)
5558
NotificationApiController := server.NewNotificationApiController(NotificationApiService)
5659

57-
router := server.Logger(server.NewRouter(EmailApiController, HealthApiController, NotificationApiController), "")
60+
router := server.NewRouter(EmailApiController, SmsApiController, HealthApiController, NotificationApiController)
5861

5962
serverAddress := fmt.Sprintf("0.0.0.0:%d", config.Port)
6063
server := &http.Server{Addr: serverAddress, Handler: router}

internal/config/config.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type Config struct {
1212
Port int
1313
SendgridAPIKey string
1414
SlackAPIKey string
15+
TwilioAccountID string
16+
TwilioAuthToken string
17+
TwilioPhoneNumber string
1518
GracefulShutdownTimeout time.Duration
1619
StructuredLogging bool
1720
DebugDumpRequests bool
@@ -25,6 +28,9 @@ const (
2528
Port
2629
SendgridAPIKey
2730
SlackAPIKey
31+
TwilioAccountID
32+
TwilioAuthToken
33+
TwilioPhoneNumber
2834
GracefulShutdownTimeout
2935
StructuredLogging
3036
DebugDumpRequests
@@ -49,6 +55,15 @@ func loadConfig() *Config {
4955
viper.SetDefault(SlackAPIKey, "")
5056
viper.BindEnv(SlackAPIKey, "SLACK_API_KEY")
5157

58+
viper.SetDefault(TwilioAccountID, "")
59+
viper.BindEnv(TwilioAccountID, "TWILIO_ACCOUNT_ID")
60+
61+
viper.SetDefault(TwilioAuthToken, "")
62+
viper.BindEnv(TwilioAuthToken, "TWILIO_AUTH_TOKEN")
63+
64+
viper.SetDefault(TwilioPhoneNumber, "")
65+
viper.BindEnv(TwilioPhoneNumber, "TWILIO_PHONE_NUMBER")
66+
5267
viper.SetDefault(GracefulShutdownTimeout, "10")
5368
viper.BindEnv(GracefulShutdownTimeout, "GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS")
5469

@@ -74,6 +89,9 @@ func loadConfig() *Config {
7489
Port: viper.GetInt(Port),
7590
SendgridAPIKey: viper.GetString(SendgridAPIKey),
7691
SlackAPIKey: viper.GetString(SlackAPIKey),
92+
TwilioAccountID: viper.GetString(TwilioAccountID),
93+
TwilioAuthToken: viper.GetString(TwilioAuthToken),
94+
TwilioPhoneNumber: viper.GetString(TwilioPhoneNumber),
7795
GracefulShutdownTimeout: viper.GetDuration(GracefulShutdownTimeout),
7896
StructuredLogging: viper.GetBool(StructuredLogging),
7997
DebugDumpRequests: viper.GetBool(DebugDumpRequests),

internal/service/api_email_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (s *EmailApiService) SendBulk(ctx context.Context, sendBulkMailRequest serv
9898
}
9999
responseCode := http.StatusOK
100100
if len(successful) == 0 {
101-
responseCode = http.StatusInternalServerError
101+
responseCode = http.StatusBadRequest
102102
}
103103
return server.Response(responseCode, server.SendBulkMailResponse{Successful: successful, Failed: failed}), nil
104104
}

0 commit comments

Comments
 (0)