Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Fix phone numbers in email notification for failed messages

Acho Arnold9eb0bbdbdc0b
diff --git a/api/pkg/emails/factory.go b/api/pkg/emails/factory.go
new file mode 100644
index 00000000..5aebfbe8
--- /dev/null
+++ b/api/pkg/emails/factory.go
@@ -0,0 +1,10 @@
+package emails
+
+import "github.com/nyaruka/phonenumbers"
+
+type factory struct{}
+
+func (factory *factory) formatPhoneNumber(number string) string {
+ value, _ := phonenumbers.Parse(number, phonenumbers.UNKNOWN_REGION)
+ return phonenumbers.Format(value, phonenumbers.E164)
+}
diff --git a/api/pkg/emails/hermes_notification_email_factory.go b/api/pkg/emails/hermes_notification_email_factory.go
index dbb20b2c..629f3601 100644
--- a/api/pkg/emails/hermes_notification_email_factory.go
+++ b/api/pkg/emails/hermes_notification_email_factory.go
@@ -12,6 +12,7 @@ import (
)
type hermesNotificationEmailFactory struct {
+ factory
config *HermesGeneratorConfig
generator hermes.Hermes
}
@@ -29,17 +30,17 @@ func (factory *hermesNotificationEmailFactory) MessageExpired(user *entities.Use
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.", owner, 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(contact), user.UserTimeString(time.Now())),
},
Dictionary: []hermes.Entry{
{"ID", messageID.String()},
- {"From", owner},
- {"To", contact},
+ {"From", factory.formatPhoneNumber(owner)},
+ {"To", factory.formatPhoneNumber(contact)},
{"Message", content},
},
Actions: []hermes.Action{
{
- Instructions: "Messages expire because we couldn't connect with your mobile phone to send the outgoing SMS. You can fix this by making sure your phone is connected to the internet and also connect your phone to the charger all the time since Android may kill the httpSMS app if it has been active for a very long time so save phone battery.",
+ Instructions: "Messages usually expire because we couldn't connect with your mobile phone to send the outgoing SMS. You can fix this by making sure your phone is connected to the internet and also connect your phone to the charger all the time since Android may kill the httpSMS app if it has been active for a very long time so save phone battery.",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
@@ -78,12 +79,12 @@ func (factory *hermesNotificationEmailFactory) MessageFailed(user *entities.User
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.", owner, 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(contact), user.UserTimeString(time.Now())),
},
Dictionary: []hermes.Entry{
{"ID", messageID.String()},
- {"From", owner},
- {"To", contact},
+ {"From", factory.formatPhoneNumber(owner)},
+ {"To", factory.formatPhoneNumber(contact)},
{"Message", content},
{"Failure Reason", reason},
},
diff --git a/api/pkg/emails/hermes_user_email_factory.go b/api/pkg/emails/hermes_user_email_factory.go
index 8ee3cdfd..1ad992f5 100644
--- a/api/pkg/emails/hermes_user_email_factory.go
+++ b/api/pkg/emails/hermes_user_email_factory.go
@@ -10,6 +10,7 @@ import (
)
type hermesUserEmailFactory struct {
+ factory
config *HermesGeneratorConfig
generator hermes.Hermes
}
@@ -122,7 +123,7 @@ func (factory *hermesUserEmailFactory) PhoneDead(user *entities.User, lastHeartb
email := hermes.Email{
Body: hermes.Body{
Intros: []string{
- fmt.Sprintf("We haven't received any heartbeat event from android phone %s since %s.", owner, lastHeartbeatTimestamp.In(location).Format(time.RFC1123)),
+ fmt.Sprintf("We haven't received any heartbeat event from android phone %s since %s.", factory.formatPhoneNumber(owner), lastHeartbeatTimestamp.In(location).Format(time.RFC1123)),
fmt.Sprintf("Check if the mobile phone is powered on and if it has stable internet connection."),
},
Actions: []hermes.Action{