Spaces:
Paused
Paused
File size: 9,978 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | package emails
import (
"fmt"
"strings"
"time"
"github.com/NdoleStudio/httpsms/pkg/events"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/stacktrace"
"github.com/go-hermes/hermes/v2"
)
type hermesNotificationEmailFactory struct {
factory
config *HermesGeneratorConfig
generator hermes.Hermes
}
const webhookSendFailedEventPayloadPlaceholder = "__HTTPSMS_WEBHOOK_SEND_FAILED_EVENT_PAYLOAD__"
// NewHermesNotificationEmailFactory creates a new instance of the UserEmailFactory
func NewHermesNotificationEmailFactory(config *HermesGeneratorConfig) NotificationEmailFactory {
return &hermesNotificationEmailFactory{
config: config,
generator: config.Generator(),
}
}
func (factory *hermesNotificationEmailFactory) DiscordSendFailed(user *entities.User, payload *events.DiscordSendFailedPayload) (*Email, error) {
email := hermes.Email{
Body: hermes.Body{
Title: "Hello",
Intros: []string{
fmt.Sprintf("We ran into an error while fowarding an incoming SMS to your discord server at %s", user.UserTimeString(time.Now())),
},
Dictionary: []hermes.Entry{
{Key: "Discord Channel ID", Value: payload.DiscordChannelID},
{Key: "Event Name", Value: payload.EventType},
{Key: "Phone Number", Value: factory.formatPhoneNumber(payload.Owner)},
{Key: "HTTP Response Code", Value: factory.formatHTTPResponseCode(payload.HTTPResponseStatusCode)},
{Key: "Error Message / HTTP Response", Value: payload.ErrorMessage},
},
Actions: []hermes.Action{
{
Instructions: "Usually this error happens because you have revoked permissions for the httpSMS discord app on your discord channel. You can always grant httpSMS permission to post to your discord channel under the settings page.",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "DISCORD SETTINGS",
Link: "https://httpsms.com/settings/#discord-settings",
},
},
},
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email. You can disable this email notification on https://httpsms.com/settings/#email-notifications"),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: "📢 We could not forward an incoming message to your discord server",
HTML: html,
Text: text,
}, nil
}
func (factory *hermesNotificationEmailFactory) WebhookSendFailed(user *entities.User, payload *events.WebhookSendFailedPayload) (*Email, error) {
formattedPayload, formattedPayloadHTML := formatEventPayload(payload.EventPayload)
email := hermes.Email{
Body: hermes.Body{
Title: "Hello",
Intros: []string{
fmt.Sprintf("We ran into an error while fowarding a webhook event from httpSMS to your webserver at %s", user.UserTimeString(time.Now())),
},
Dictionary: []hermes.Entry{
{Key: "Server URL", Value: payload.WebhookURL},
{Key: "Event Name", Value: payload.EventType},
{Key: "Event ID", Value: payload.EventID},
{Key: "Phone Number", Value: factory.formatPhoneNumber(payload.Owner)},
{Key: "HTTP Response Code", Value: factory.formatHTTPResponseCode(payload.HTTPResponseStatusCode)},
{Key: "Error Message / HTTP Response", Value: payload.ErrorMessage},
{
Key: "Event Payload",
Value: webhookSendFailedEventPayloadPlaceholder,
UnsafeValue: formattedPayloadHTML,
},
},
Actions: []hermes.Action{
{
Instructions: "Usually this error happens because your webserver is either offline or inaccessible, you can always configure the webhook endpoint on the httpSMS website under the settings page.",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "WEBHOOK SETTINGS",
Link: "https://httpsms.com/settings/#webhook-settings",
},
},
},
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email. You can disable this email notification on https://httpsms.com/settings/#email-notifications"),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate html email")
}
eventPayloadEntryIndex := len(email.Body.Dictionary) - 1
text, err := generateWebhookSendFailedPlainText(func(value string) (string, error) {
email.Body.Dictionary[eventPayloadEntryIndex].Value = value
return factory.generator.GeneratePlainText(email)
}, formattedPayload)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: "📢 We could not forward a webhook event to your server",
HTML: html,
Text: text,
}, nil
}
func generateWebhookSendFailedPlainText(generate func(string) (string, error), formattedPayload string) (string, error) {
text, err := generate(webhookSendFailedEventPayloadPlaceholder)
if err != nil {
return "", err
}
if strings.Contains(text, webhookSendFailedEventPayloadPlaceholder) {
// Hermes/html2text collapses dictionary whitespace, so restore the payload after plain-text generation.
return replaceWebhookSendFailedEventPayloadPlaceholder(text, formattedPayload), nil
}
// Preserve the real payload if a Hermes change stops carrying the placeholder through unchanged.
return generate(formattedPayload)
}
func replaceWebhookSendFailedEventPayloadPlaceholder(text string, formattedPayload string) string {
before, after, found := strings.Cut(text, webhookSendFailedEventPayloadPlaceholder)
if !found {
return text
}
return before + formattedPayload + after
}
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(payload.Contact), user.UserTimeString(time.Now())),
},
Dictionary: []hermes.Entry{
{Key: "ID", Value: payload.MessageID.String()},
{Key: "From", Value: factory.formatPhoneNumber(payload.Owner)},
{Key: "To", Value: factory.formatPhoneNumber(payload.Contact)},
{Key: "Message", Value: payload.Content},
{Key: "Encrypted", Value: factory.formatBool(payload.Encrypted)},
},
Actions: []hermes.Action{
{
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",
Text: "VIEW MESSAGES",
Link: "https://httpsms.com/threads",
},
},
},
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email. You can disable this email notification on https://httpsms.com/settings/#email-notifications"),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: "📢 Your SMS message has expired on httpSMS",
HTML: html,
Text: text,
}, nil
}
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(payload.Contact), user.UserTimeString(time.Now())),
},
Dictionary: []hermes.Entry{
{Key: "ID", Value: payload.ID.String()},
{Key: "From", Value: factory.formatPhoneNumber(payload.Owner)},
{Key: "To", Value: factory.formatPhoneNumber(payload.Contact)},
{Key: "Message", Value: payload.Content},
{Key: "Encrypted", Value: factory.formatBool(payload.Encrypted)},
{Key: "Failure Reason", Value: payload.ErrorMessage},
},
Actions: []hermes.Action{
{
Instructions: "Check the default SMS messaging app on your phone to find out the exact reason why the message failed. Usually messages fail because the httpSMS app phone has been un-installed or it is not active. Logout and login again on the mobile app on your Android phone and retry sending the SMS.",
Button: hermes.Button{
Color: "#329ef4",
TextColor: "#FFFFFF",
Text: "VIEW MESSAGES",
Link: "https://httpsms.com/threads",
},
},
},
Signature: "Cheers",
Outros: []string{
fmt.Sprintf("Don't hesitate to contact us by replying to this email. You can disable this email notification on https://httpsms.com/settings/#email-notifications"),
},
},
}
html, err := factory.generator.GenerateHTML(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate html email")
}
text, err := factory.generator.GeneratePlainText(email)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot generate text email")
}
return &Email{
ToEmail: user.Email,
Subject: "📢 Your SMS message has failed on httpSMS",
HTML: html,
Text: text,
}, nil
}
|