-
Notifications
You must be signed in to change notification settings - Fork 3
Twilio sms #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Twilio sms #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ type Config struct { | |
Port int | ||
SendgridAPIKey string | ||
SlackAPIKey string | ||
TwilioAccID string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
TwilioAuthToken string | ||
TwilioPhoneNumber string | ||
GracefulShutdownTimeout time.Duration | ||
StructuredLogging bool | ||
} | ||
|
@@ -22,6 +25,9 @@ const ( | |
Port | ||
SendgridAPIKey | ||
SlackAPIKey | ||
TwilioAccID | ||
TwilioAuthToken | ||
TwilioPhoneNumber | ||
GracefulShutdownTimeout | ||
StructuredLogging | ||
) | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
viper.SetDefault(TwilioAuthToken, "") | ||
viper.BindEnv(TwilioAuthToken, "TWILIO_AUTH_TOKEN") | ||
|
||
viper.SetDefault(TwilioPhoneNumber, "") | ||
viper.BindEnv(TwilioPhoneNumber, "TWILIO_PHONE_NUMBER") | ||
|
||
viper.SetDefault(GracefulShutdownTimeout, "10") | ||
viper.BindEnv(GracefulShutdownTimeout, "GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS") | ||
|
||
|
@@ -54,6 +69,9 @@ func loadConfig() *Config { | |
Port: viper.GetInt(Port), | ||
SendgridAPIKey: viper.GetString(SendgridAPIKey), | ||
SlackAPIKey: viper.GetString(SlackAPIKey), | ||
TwilioAccID: viper.GetString(TwilioAccID), | ||
TwilioAuthToken: viper.GetString(TwilioAuthToken), | ||
TwilioPhoneNumber: viper.GetString(TwilioPhoneNumber), | ||
GracefulShutdownTimeout: viper.GetDuration(GracefulShutdownTimeout), | ||
StructuredLogging: viper.GetBool(StructuredLogging), | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/commitdev/zero-notification-service/internal/config" | ||
"github.com/commitdev/zero-notification-service/internal/server" | ||
) | ||
|
||
// EmailApiService is a service that implents the logic for the EmailApiServicer | ||
// This service should implement the business logic for every endpoint for the EmailApi API. | ||
// Include any external packages or services that will be required by this service. | ||
type SmsApiService struct { | ||
config *config.Config | ||
} | ||
|
||
// NewSmsApiService creates a default api service | ||
func NewSmsApiService(c *config.Config) server.SmsApiServicer { | ||
return &SmsApiService{c} | ||
} | ||
|
||
// SendSMS - Send an email | ||
func (s *SmsApiService) SendSMS(ctx context.Context, sendSMSRequest server.SendSmsRequest) (server.ImplResponse, error) { | ||
// Set initial variables | ||
accountSid := s.config.TwilioAccID | ||
authToken := s.config.TwilioAuthToken | ||
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
|
||
// Build out the data for our message | ||
v := url.Values{} | ||
v.Set("To", sendSMSRequest.Recipient) | ||
v.Set("From", s.config.TwilioPhoneNumber) | ||
v.Set("Body", sendSMSRequest.Message) | ||
rb := *strings.NewReader(v.Encode()) | ||
|
||
// Create Client | ||
client := &http.Client{} | ||
|
||
req, _ := http.NewRequest("POST", urlStr, &rb) | ||
req.SetBasicAuth(accountSid, authToken) | ||
req.Header.Add("Accept", "application/json") | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
// Make request | ||
resp, err := client.Do(req) | ||
fmt.Print(resp.Status) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
|
||
if err != nil { | ||
fmt.Printf("Error sending SMS: %v\n", resp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
return server.Response(http.StatusBadRequest, server.SendSmsResponse{Message: "Error sending SMS"}), nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
return server.Response(http.StatusOK, server.SendSmsResponse{Message: "SMS Sent"}), nil | ||
} |
There was a problem hiding this comment.
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.