Send and receive SMS messages using your Android phone programmatically via a simple HTTP API
Add end to end encryption on the Server #335
Acho Arnoldb75c3628604a
diff --git a/api/pkg/emails/factory.go b/api/pkg/emails/factory.goindex 4c7f1cf7..992f4864 100644--- a/api/pkg/emails/factory.go+++ b/api/pkg/emails/factory.go@@ -14,6 +14,13 @@ func (factory *factory) formatPhoneNumber(number string) string {return phonenumbers.Format(value, phonenumbers.INTERNATIONAL)}+func (factory *factory) formatBool(value bool) string {+ if value == true {+ return "Yes"+ }+ return "No"+}+func (factory *factory) formatHTTPResponseCode(code *int) string {responseCode := "-"if code != nil {diff --git a/api/pkg/emails/hermes_notification_email_factory.go b/api/pkg/emails/hermes_notification_email_factory.goindex 117b3923..824a9843 100644--- a/api/pkg/emails/hermes_notification_email_factory.go+++ b/api/pkg/emails/hermes_notification_email_factory.go@@ -6,8 +6,6 @@ import ("github.com/NdoleStudio/httpsms/pkg/events"- "github.com/google/uuid"-"github.com/NdoleStudio/httpsms/pkg/entities""github.com/matcornic/hermes/v2""github.com/palantir/stacktrace"@@ -129,18 +127,19 @@ func (factory *hermesNotificationEmailFactory) WebhookSendFailed(user *entities.}, nil}-func (factory *hermesNotificationEmailFactory) MessageExpired(user *entities.User, messageID uuid.UUID, owner string, contact string, content string) (*Email, error) {+func (factory *hermesNotificationEmailFactory) MessageExpired(user *entities.User, payload *events.MessageSendExpiredPayload) (*Email, error) {email := hermes.Email{Body: hermes.Body{Title: "Hello",Intros: []string{- fmt.Sprintf("The SMS message which you sent to %s has expired at %s and you will need to resend this message.", factory.formatPhoneNumber(contact), user.UserTimeString(time.Now())),+ fmt.Sprintf("The SMS message which you sent to %s has expired at %s and you will need to resend this message.", factory.formatPhoneNumber(payload.Contact), user.UserTimeString(time.Now())),},Dictionary: []hermes.Entry{- {"ID", messageID.String()},- {"From", factory.formatPhoneNumber(owner)},- {"To", factory.formatPhoneNumber(contact)},- {"Message", content},+ {"ID", payload.MessageID.String()},+ {"From", factory.formatPhoneNumber(payload.Owner)},+ {"To", factory.formatPhoneNumber(payload.Contact)},+ {"Message", payload.Content},+ {"Encrypted", factory.formatBool(payload.Encrypted)},},Actions: []hermes.Action{{@@ -178,19 +177,20 @@ func (factory *hermesNotificationEmailFactory) MessageExpired(user *entities.Use}, nil}-func (factory *hermesNotificationEmailFactory) MessageFailed(user *entities.User, messageID uuid.UUID, owner, contact, content, reason string) (*Email, error) {+func (factory *hermesNotificationEmailFactory) MessageFailed(user *entities.User, payload *events.MessageSendFailedPayload) (*Email, error) {email := hermes.Email{Body: hermes.Body{Title: "Hello",Intros: []string{- fmt.Sprintf("The SMS message which you sent to %s has failed at %s and you will need to resend this message.", factory.formatPhoneNumber(contact), user.UserTimeString(time.Now())),+ fmt.Sprintf("The SMS message which you sent to %s has failed at %s and you will need to resend this message.", factory.formatPhoneNumber(payload.Contact), user.UserTimeString(time.Now())),},Dictionary: []hermes.Entry{- {"ID", messageID.String()},- {"From", factory.formatPhoneNumber(owner)},- {"To", factory.formatPhoneNumber(contact)},- {"Message", content},- {"Failure Reason", reason},+ {"ID", payload.ID.String()},+ {"From", factory.formatPhoneNumber(payload.Owner)},+ {"To", factory.formatPhoneNumber(payload.Contact)},+ {"Message", payload.Content},+ {"Encrypted", factory.formatBool(payload.Encrypted)},+ {"Failure Reason", payload.ErrorMessage},},Actions: []hermes.Action{{diff --git a/api/pkg/emails/notification_email_factory.go b/api/pkg/emails/notification_email_factory.goindex a25b90a8..72c8c16f 100644--- a/api/pkg/emails/notification_email_factory.go+++ b/api/pkg/emails/notification_email_factory.go@@ -3,16 +3,15 @@ package emailsimport ("github.com/NdoleStudio/httpsms/pkg/entities""github.com/NdoleStudio/httpsms/pkg/events"- "github.com/google/uuid")// NotificationEmailFactory generates emails to users about a messagetype NotificationEmailFactory interface {// MessageExpired sends an email when the user's message is expired- MessageExpired(user *entities.User, messageID uuid.UUID, owner, contact, content string) (*Email, error)+ MessageExpired(user *entities.User, payload *events.MessageSendExpiredPayload) (*Email, error)// MessageFailed sends an email when the user's message is failed- MessageFailed(user *entities.User, messageID uuid.UUID, owner, contact, content, reason string) (*Email, error)+ MessageFailed(user *entities.User, payload *events.MessageSendFailedPayload) (*Email, error)// DiscordSendFailed sends an email when the user's discord message is failedDiscordSendFailed(user *entities.User, payload *events.DiscordSendFailedPayload) (*Email, error)diff --git a/api/pkg/entities/message.go b/api/pkg/entities/message.goindex eb1f8c84..bc46e626 100644--- a/api/pkg/entities/message.go+++ b/api/pkg/entities/message.go@@ -86,6 +86,7 @@ type Message struct {UserID UserID `json:"user_id" gorm:"index:idx_messages__user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`Contact string `json:"contact" example:"+18005550100"`Content string `json:"content" example:"This is a sample text message"`+ Encrypted bool `json:"encrypted" example:"false" gorm:"default:false"`Type MessageType `json:"type" example:"mobile-terminated"`Status MessageStatus `json:"status" example:"pending"`// SIM is the SIM card to use to send the messagediff --git a/api/pkg/events/message_api_deleted_event.go b/api/pkg/events/message_api_deleted_event.goindex 57f4a05f..49842830 100644--- a/api/pkg/events/message_api_deleted_event.go+++ b/api/pkg/events/message_api_deleted_event.go@@ -20,5 +20,6 @@ type MessageAPIDeletedPayload struct {Contact string `json:"contact"`Timestamp time.Time `json:"timestamp"`Content string `json:"content"`+ Encrypted bool `json:"encrypted"`SIM entities.SIM `json:"sim"`}diff --git a/api/pkg/events/message_api_sent_event.go b/api/pkg/events/message_api_sent_event.goindex 27f8a540..7abea843 100644--- a/api/pkg/events/message_api_sent_event.go+++ b/api/pkg/events/message_api_sent_event.go@@ -22,5 +22,6 @@ type MessageAPISentPayload struct {ScheduledSendTime *time.Time `json:"scheduled_send_time"`RequestReceivedAt time.Time `json:"request_received_at"`Content string `json:"content"`+ Encrypted bool `json:"encrypted"`SIM entities.SIM `json:"sim"`}diff --git a/api/pkg/events/message_notification_scheduled_event.go b/api/pkg/events/message_notification_scheduled_event.goindex e8bcc598..50171f3f 100644--- a/api/pkg/events/message_notification_scheduled_event.go+++ b/api/pkg/events/message_notification_scheduled_event.go@@ -17,6 +17,7 @@ type MessageNotificationScheduledPayload struct {Owner string `json:"owner"`Contact string `json:"contact"`Content string `json:"content"`+ Encrypted bool `json:"encrypted"`SIM entities.SIM `json:"sim"`UserID entities.UserID `json:"user_id"`PhoneID uuid.UUID `json:"phone_id"`diff --git a/api/pkg/events/message_phone_delivered_event.go b/api/pkg/events/message_phone_delivered_event.goindex 0c68df81..a8512653 100644--- a/api/pkg/events/message_phone_delivered_event.go+++ b/api/pkg/events/message_phone_delivered_event.go@@ -18,6 +18,7 @@ type MessagePhoneDeliveredPayload struct {Contact string `json:"contact"`RequestID *string `json:"request_id"`UserID entities.UserID `json:"user_id"`+ Encrypted bool `json:"encrypted"`Timestamp time.Time `json:"timestamp"`Content string `json:"content"`SIM entities.SIM `json:"sim"`diff --git a/api/pkg/events/message_phone_received_event.go b/api/pkg/events/message_phone_received_event.goindex 9159e4fc..abe3a014 100644--- a/api/pkg/events/message_phone_received_event.go+++ b/api/pkg/events/message_phone_received_event.go@@ -16,6 +16,7 @@ type MessagePhoneReceivedPayload struct {MessageID uuid.UUID `json:"message_id"`UserID entities.UserID `json:"user_id"`Owner string `json:"owner"`+ Encrypted bool `json:"encrypted"`Contact string `json:"contact"`Timestamp time.Time `json:"timestamp"`Content string `json:"content"`diff --git a/api/pkg/events/message_phone_sending_event.go b/api/pkg/events/message_phone_sending_event.goindex 31915634..c6615b93 100644--- a/api/pkg/events/message_phone_sending_event.go+++ b/api/pkg/events/message_phone_sending_event.go@@ -17,6 +17,7 @@ type MessagePhoneSendingPayload struct {RequestID *string `json:"request_id"`Timestamp time.Time `json:"timestamp"`Owner string `json:"owner"`+ Encrypted bool `json:"encrypted"`Contact string `json:"contact"`Content string `json:"content"`SIM entities.SIM `json:"sim"`diff --git a/api/pkg/events/message_phone_sent_event.go b/api/pkg/events/message_phone_sent_event.goindex fcbd95ee..f9f3fa4b 100644--- a/api/pkg/events/message_phone_sent_event.go+++ b/api/pkg/events/message_phone_sent_event.go@@ -18,6 +18,7 @@ type MessagePhoneSentPayload struct {RequestID *string `json:"request_id"`Owner string `json:"owner"`Contact string `json:"contact"`+ Encrypted bool `json:"encrypted"`Timestamp time.Time `json:"timestamp"`Content string `json:"content"`SIM entities.SIM `json:"sim"`diff --git a/api/pkg/events/message_send_expired_event.go b/api/pkg/events/message_send_expired_event.goindex 697deadd..ec51fcfa 100644--- a/api/pkg/events/message_send_expired_event.go+++ b/api/pkg/events/message_send_expired_event.go@@ -19,6 +19,7 @@ type MessageSendExpiredPayload struct {IsFinal bool `json:"is_final"`RequestID *string `json:"request_id"`Contact string `json:"contact"`+ Encrypted bool `json:"encrypted"`UserID entities.UserID `json:"user_id"`Timestamp time.Time `json:"timestamp"`Content string `json:"content"`diff --git a/api/pkg/events/message_send_failed_event.go b/api/pkg/events/message_send_failed_event.goindex 00a12552..fbc6836f 100644--- a/api/pkg/events/message_send_failed_event.go+++ b/api/pkg/events/message_send_failed_event.go@@ -19,6 +19,7 @@ type MessageSendFailedPayload struct {RequestID *string `json:"request_id"`Contact string `json:"contact"`Timestamp time.Time `json:"timestamp"`+ Encrypted bool `json:"encrypted"`Content string `json:"content"`SIM entities.SIM `json:"sim"`}diff --git a/api/pkg/events/message_send_retry_event.go b/api/pkg/events/message_send_retry_event.goindex 05549bea..e60c5c7b 100644--- a/api/pkg/events/message_send_retry_event.go+++ b/api/pkg/events/message_send_retry_event.go@@ -16,6 +16,7 @@ type MessageSendRetryPayload struct {MessageID uuid.UUID `json:"message_id"`Owner string `json:"owner"`Contact string `json:"contact"`+ Encrypted bool `json:"encrypted"`UserID entities.UserID `json:"user_id"`Timestamp time.Time `json:"timestamp"`Content string `json:"content"`diff --git a/api/pkg/listeners/phone_notification_listener.go b/api/pkg/listeners/phone_notification_listener.goindex f1d6d31f..715be3ee 100644--- a/api/pkg/listeners/phone_notification_listener.go+++ b/api/pkg/listeners/phone_notification_listener.go@@ -57,6 +57,7 @@ func (listener *PhoneNotificationListener) onMessageAPISent(ctx context.Context,Contact: payload.Contact,Content: payload.Content,SIM: payload.SIM,+ Encrypted: payload.Encrypted,Source: event.Source(),MessageID: payload.MessageID,}@@ -86,6 +87,7 @@ func (listener *PhoneNotificationListener) onMessageSendRetry(ctx context.ContexContact: payload.Contact,Content: payload.Content,SIM: payload.SIM,+ Encrypted: payload.Encrypted,Source: event.Source(),MessageID: payload.MessageID,}diff --git a/api/pkg/requests/message_bulk_send_request.go b/api/pkg/requests/message_bulk_send_request.goindex 07c8dc27..ff907429 100644--- a/api/pkg/requests/message_bulk_send_request.go+++ b/api/pkg/requests/message_bulk_send_request.go@@ -17,6 +17,9 @@ type MessageBulkSend struct {To []string `json:"to" example:"+18005550100,+18005550100"`Content string `json:"content" example:"This is a sample text message"`+ // Encrypted is used to determine if the content is end-to-end encrypted. Make sure to set the encryption key on the httpSMS mobile app+ Encrypted bool `json:"encrypted" example:"false"`+// RequestID is an optional parameter used to track a request from the client's perspectiveRequestID string `json:"request_id" example:"153554b5-ae44-44a0-8f4f-7bbac5657ad4" validate:"optional"`}@@ -41,6 +44,7 @@ func (input *MessageBulkSend) ToMessageSendParams(userID entities.UserID, sourceresult = append(result, services.MessageSendParams{Source: source,Owner: from,+ Encrypted: input.Encrypted,RequestID: input.sanitizeStringPointer(input.RequestID),UserID: userID,RequestReceivedAt: time.Now().UTC(),diff --git a/api/pkg/requests/message_receive_request.go b/api/pkg/requests/message_receive_request.goindex 158d9cbe..82bcf9f7 100644--- a/api/pkg/requests/message_receive_request.go+++ b/api/pkg/requests/message_receive_request.go@@ -17,6 +17,8 @@ type MessageReceive struct {From string `json:"from" example:"+18005550199"`To string `json:"to" example:"+18005550100"`Content string `json:"content" example:"This is a sample text message received on a phone"`+ // Encrypted is used to determine if the content is end-to-end encrypted. Make sure to set the encryption key on the httpSMS mobile app+ Encrypted bool `json:"encrypted" example:"false"`// SIM card that received the messageSIM entities.SIM `json:"sim" example:"SIM1"`// Timestamp is the time when the event was emitted, Please send the timestamp in UTC with as much precision as possible@@ -34,13 +36,14 @@ func (input *MessageReceive) Sanitize() MessageReceive {}// ToMessageReceiveParams converts MessageReceive to services.MessageReceiveParams-func (input *MessageReceive) ToMessageReceiveParams(userID entities.UserID, source string) services.MessageReceiveParams {+func (input *MessageReceive) ToMessageReceiveParams(userID entities.UserID, source string) *services.MessageReceiveParams {phone, _ := phonenumbers.Parse(input.To, phonenumbers.UNKNOWN_REGION)- return services.MessageReceiveParams{+ return &services.MessageReceiveParams{Source: source,Contact: input.From,UserID: userID,Timestamp: input.Timestamp,+ Encrypted: input.Encrypted,Owner: *phone,Content: input.Content,SIM: input.SIM,diff --git a/api/pkg/requests/message_send_request.go b/api/pkg/requests/message_send_request.goindex 6c632e92..df844868 100644--- a/api/pkg/requests/message_send_request.go+++ b/api/pkg/requests/message_send_request.go@@ -18,6 +18,8 @@ type MessageSend struct {To string `json:"to" example:"+18005550100"`Content string `json:"content" example:"This is a sample text message"`+ // Encrypted is used to determine if the content is end-to-end encrypted. Make sure to set the encryption key on the httpSMS mobile app+ Encrypted bool `json:"encrypted" example:"false"`// RequestID is an optional parameter used to track a request from the client's perspectiveRequestID string `json:"request_id" example:"153554b5-ae44-44a0-8f4f-7bbac5657ad4" validate:"optional"`// SendAt is an optional parameter used to schedule a message to be sent at a later time@@ -38,6 +40,7 @@ func (input *MessageSend) ToMessageSendParams(userID entities.UserID, source strreturn services.MessageSendParams{Source: source,Owner: from,+ Encrypted: input.Encrypted,RequestID: input.sanitizeStringPointer(input.RequestID),UserID: userID,SendAt: input.SendAt,diff --git a/api/pkg/services/email_notification_service.go b/api/pkg/services/email_notification_service.goindex 1fa9bc78..20f45152 100644--- a/api/pkg/services/email_notification_service.go+++ b/api/pkg/services/email_notification_service.go@@ -75,7 +75,7 @@ func (service *EmailNotificationService) NotifyMessageExpired(ctx context.Contexreturn nil}- email, err := service.factory.MessageExpired(user, payload.MessageID, payload.Owner, payload.Contact, payload.Content)+ email, err := service.factory.MessageExpired(user, payload)if err != nil {msg := fmt.Sprintf("cannot create email for user with ID [%s] and for expired message with ID [%s]", payload.UserID, payload.MessageID)return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))@@ -113,7 +113,7 @@ func (service *EmailNotificationService) NotifyMessageFailed(ctx context.Contextreturn nil}- email, err := service.factory.MessageFailed(user, payload.ID, payload.Owner, payload.Contact, payload.Content, payload.ErrorMessage)+ email, err := service.factory.MessageFailed(user, payload)if err != nil {msg := fmt.Sprintf("cannot create email for user with ID [%s] for [%s] message with ID [%s]", payload.UserID, payload.ID)return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))diff --git a/api/pkg/services/message_service.go b/api/pkg/services/message_service.goindex f7c38c9d..3a8f7c88 100644--- a/api/pkg/services/message_service.go+++ b/api/pkg/services/message_service.go@@ -72,6 +72,7 @@ func (service *MessageService) GetOutstanding(ctx context.Context, params MessagOwner: message.Owner,Contact: message.Contact,Timestamp: params.Timestamp,+ Encrypted: message.Encrypted,UserID: message.UserID,Content: message.Content,SIM: message.SIM,@@ -108,6 +109,7 @@ func (service *MessageService) DeleteMessage(ctx context.Context, source string,MessageID: message.ID,UserID: message.UserID,Owner: message.Owner,+ Encrypted: message.Encrypted,RequestID: message.RequestID,Contact: message.Contact,Timestamp: time.Now().UTC(),@@ -227,11 +229,12 @@ type MessageReceiveParams struct {Content stringSIM entities.SIMTimestamp time.Time+ Encrypted boolSource string}// ReceiveMessage handles message received by a mobile phone-func (service *MessageService) ReceiveMessage(ctx context.Context, params MessageReceiveParams) (*entities.Message, error) {+func (service *MessageService) ReceiveMessage(ctx context.Context, params *MessageReceiveParams) (*entities.Message, error) {ctx, span := service.tracer.Start(ctx)defer span.End()@@ -240,6 +243,7 @@ func (service *MessageService) ReceiveMessage(ctx context.Context, params MessageventPayload := events.MessagePhoneReceivedPayload{MessageID: uuid.New(),UserID: params.UserID,+ Encrypted: params.Encrypted,Owner: phonenumbers.Format(¶ms.Owner, phonenumbers.E164),Contact: params.Contact,Timestamp: params.Timestamp,@@ -277,6 +281,7 @@ func (service *MessageService) handleMessageSentEvent(ctx context.Context, paramRequestID: message.RequestID,Timestamp: params.Timestamp,Contact: message.Contact,+ Encrypted: message.Encrypted,Content: message.Content,SIM: message.SIM,})@@ -302,6 +307,7 @@ func (service *MessageService) handleMessageDeliveredEvent(ctx context.Context,UserID: message.UserID,RequestID: message.RequestID,Timestamp: params.Timestamp,+ Encrypted: message.Encrypted,Contact: message.Contact,Content: message.Content,SIM: message.SIM,@@ -332,6 +338,7 @@ func (service *MessageService) handleMessageFailedEvent(ctx context.Context, parOwner: message.Owner,ErrorMessage: errorMessage,Timestamp: params.Timestamp,+ Encrypted: message.Encrypted,Contact: message.Contact,RequestID: message.RequestID,UserID: message.UserID,@@ -354,6 +361,7 @@ func (service *MessageService) handleMessageFailedEvent(ctx context.Context, partype MessageSendParams struct {Owner *phonenumbers.PhoneNumberContact string+ Encrypted boolContent stringSource stringSendAt *time.Time@@ -374,6 +382,7 @@ func (service *MessageService) SendMessage(ctx context.Context, params MessageSeeventPayload := events.MessageAPISentPayload{MessageID: uuid.New(),UserID: params.UserID,+ Encrypted: params.Encrypted,MaxSendAttempts: sendAttempts,RequestID: params.RequestID,Owner: phonenumbers.Format(params.Owner, phonenumbers.E164),@@ -435,6 +444,7 @@ func (service *MessageService) storeReceivedMessage(ctx context.Context, paramsContact: params.Contact,Content: params.Content,SIM: params.SIM,+ Encrypted: params.Encrypted,Type: entities.MessageTypeMobileOriginated,Status: entities.MessageStatusReceived,RequestReceivedAt: params.Timestamp,@@ -660,6 +670,7 @@ func (service *MessageService) HandleMessageExpired(ctx context.Context, paramsTimestamp: time.Now().UTC(),Contact: message.Contact,Owner: message.Owner,+ Encrypted: message.Encrypted,UserID: message.UserID,Content: message.Content,SIM: message.SIM,@@ -748,6 +759,7 @@ func (service *MessageService) CheckExpired(ctx context.Context, params MessageCMessageID: message.ID,Owner: message.Owner,Contact: message.Contact,+ Encrypted: message.Encrypted,RequestID: message.RequestID,IsFinal: message.SendAttemptCount == message.MaxSendAttempts,SendAttemptCount: message.SendAttemptCount,@@ -806,6 +818,7 @@ func (service *MessageService) storeSentMessage(ctx context.Context, payload eveContent: payload.Content,RequestID: payload.RequestID,SIM: payload.SIM,+ Encrypted: payload.Encrypted,ScheduledSendTime: payload.ScheduledSendTime,Type: entities.MessageTypeMobileTerminated,Status: entities.MessageStatusPending,diff --git a/api/pkg/services/phone_notification_service.go b/api/pkg/services/phone_notification_service.goindex 480f6c5d..605c366f 100644--- a/api/pkg/services/phone_notification_service.go+++ b/api/pkg/services/phone_notification_service.go@@ -94,7 +94,7 @@ type PhoneNotificationSendParams struct {// Send sends a message when a message is sentfunc (service *PhoneNotificationService) Send(ctx context.Context, params *PhoneNotificationSendParams) error {- ctx, span := service.tracer.Start(ctx)+ ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)defer span.End()phone, err := service.phoneRepository.LoadByID(ctx, params.UserID, params.PhoneID)@@ -120,8 +120,11 @@ func (service *PhoneNotificationService) Send(ctx context.Context, params *PhoneToken: *phone.FcmToken,})if err != nil {- return service.handleNotificationFailed(ctx, err, params)+ ctxLogger.Warn(stacktrace.Propagate(err, "cannot send FCM to phone"))+ msg := fmt.Sprintf("cannot send notification for to your phone [%s]. Reinstall the httpSMS app on your Android phone.", phone.PhoneNumber)+ return service.handleNotificationFailed(ctx, errors.New(msg), params)}+return service.handleNotificationSent(ctx, phone, result, params)}@@ -130,6 +133,7 @@ type PhoneNotificationScheduleParams struct {UserID entities.UserIDOwner stringSource string+ Encrypted boolContact stringContent stringSIM entities.SIM@@ -200,6 +204,7 @@ func (service *PhoneNotificationService) dispatchMessageNotificationScheduled(ctMessageID: notification.MessageID,Owner: params.Owner,Contact: params.Contact,+ Encrypted: params.Encrypted,Content: params.Content,SIM: params.SIM,UserID: notification.UserID,