Skip to content

Conversation

songsc
Copy link

@songsc songsc commented Feb 26, 2021

Added Twilio SMS API to provide SMS notification service.

3 env variables are required for this service to function:

  1. TWILIO_ACC_ID
  2. TWILIO_AUTH_TOKEN
  3. TWILIO_PHONE_NUMBER

1 new endpoint POST sms/send is added. Following input format is expected:
{ "recipient": string (valid phone number), "message": string }

Following output format can be expected:
{ "message": string }

Note that the current make generate command may not work on certain platforms. Trying running npx @openapitools/openapi-generator-cli generate -i api/notification-service.yaml -g go-server -o ./ -p sourceFolder=internal/server -p packageName=server --git-user-id=commitdev --git-repo-id=zero-notification-service instead of openapi-generator generate -i api/notification-service.yaml -g go-server -o ./ -p sourceFolder=internal/server -p packageName=server --git-user-id=commitdev --git-repo-id=zero-notification-service in the make generate command if on Windows.

@songsc songsc requested a review from a team as a code owner February 26, 2021 01:30
@bmonkman
Copy link
Contributor

Running openapi-generator using docker is also an easy option:

docker run openapitools/openapi-generator-cli:v5.0.0-beta <openapi args>

- recipient
- message
properties:
recipient:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you indicate that the recipient is a phone number either by adding a description or by changing the name to something like recipientPhoneNumber? Or if it's possible for a recipient to be something other than a phone number, give some indictation here so the consumer knows what this is supposed to contain.

@@ -11,6 +11,9 @@ type Config struct {
Port int
SendgridAPIKey string
SlackAPIKey string
TwilioAccID string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to TwilioAccountID wherever it's used if "Acc" is short for "Account"?

Copy link
Contributor

@dtoki dtoki Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmonkman okay to leave the env variables as TWILIO_ACC_ID or should that be updated as well?

@@ -44,6 +50,15 @@ func loadConfig() *Config {
viper.SetDefault(SlackAPIKey, "")
viper.BindEnv(SlackAPIKey, "SLACK_API_KEY")

viper.SetDefault(TwilioAccID, "")
viper.BindEnv(TwilioAccID, "TWILIO_ACC_ID")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add these env vars to the helm chart as well. They should be referenced in values.yaml and then added to secret.yaml, and then the chart version can be bumped in Chart.yaml.
This will allow us to use these options immediately in staging/production systems running in Kubernetes.

// Set initial variables
accountSid := s.config.TwilioAccID
authToken := s.config.TwilioAuthToken
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use fmt.Sprintf()

fmt.Print(resp.Status)

if err != nil {
fmt.Printf("Error sending SMS: %v\n", resp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use Zap logger, for example:

		zap.S().Errorf("Error sending SMS: %v", resp)

return server.Response(http.StatusInternalServerError, nil), fmt.Errorf("Unable to send SMS: %v", err)
}
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
fmt.Printf("Failure from Twilio when sending SMS: %v", resp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


// Make request
resp, err := client.Do(req)
fmt.Print(resp.Status)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

}
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
fmt.Printf("Failure from Twilio when sending SMS: %v", resp)
return server.Response(http.StatusBadRequest, server.SendSmsResponse{Message: "Error sending SMS"}), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add the response code and body here so the user can debug. See how it's done in the email service. Also we should standardize on the response code. Email uses StatusInternalServerError where this is using StatusBadRequest. We can't really know if it should be 400-level or 500-level so we really could use either one. Maybe let's stick with Bad Request to encourage the user to check their request, and hopefully the response from Twilio will be enough to tell them if it's actually a server-side misconfiguration.

@dtoki dtoki mentioned this pull request Apr 9, 2021
@bmonkman
Copy link
Contributor

Completed in #25

@bmonkman bmonkman closed this Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants