2026-01-25 18:47:22 +00:00
|
|
|
package text
|
2026-01-21 03:30:03 +00:00
|
|
|
|
|
|
|
|
import (
|
2026-01-25 18:47:22 +00:00
|
|
|
"context"
|
2026-01-21 03:30:03 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
2026-01-25 18:47:22 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
2026-01-21 03:30:03 +00:00
|
|
|
"github.com/nyaruka/phonenumbers"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
"github.com/twilio/twilio-go"
|
|
|
|
|
twilioApi "github.com/twilio/twilio-go/rest/api/v2010"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type E164 = phonenumbers.PhoneNumber
|
|
|
|
|
|
|
|
|
|
func ParsePhoneNumber(input string) (*E164, error) {
|
|
|
|
|
return phonenumbers.Parse(input, "US")
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 20:29:04 +00:00
|
|
|
func sendText(ctx context.Context, source string, destination string, message string, origin enums.CommsTextorigin, is_welcome bool) error {
|
2026-01-26 16:10:30 +00:00
|
|
|
err := ensureInDB(ctx, destination)
|
2026-01-25 18:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
|
|
|
|
|
}
|
2026-01-26 20:29:04 +00:00
|
|
|
err = insertTextLog(ctx, message, destination, source, origin, is_welcome)
|
2026-01-25 18:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Failed to insert text message in the DB: %w", err)
|
|
|
|
|
}
|
2026-01-21 03:30:03 +00:00
|
|
|
client := twilio.NewRestClient()
|
|
|
|
|
|
|
|
|
|
params := &twilioApi.CreateMessageParams{}
|
|
|
|
|
params.SetMessagingServiceSid(config.TwilioMessagingServiceSID)
|
|
|
|
|
|
|
|
|
|
params.SetBody(message)
|
2026-01-26 16:10:30 +00:00
|
|
|
params.SetTo(destination)
|
2026-01-21 03:30:03 +00:00
|
|
|
resp, err := client.Api.CreateMessage(params)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2026-01-26 16:10:30 +00:00
|
|
|
return fmt.Errorf("Failed to create message to %s: %w", destination, err)
|
2026-01-21 03:30:03 +00:00
|
|
|
} else {
|
|
|
|
|
if resp.Body != nil {
|
2026-01-26 16:10:30 +00:00
|
|
|
log.Info().Str("dest", destination).Str("body", *resp.Body).Msg("Text message response")
|
2026-01-21 03:30:03 +00:00
|
|
|
} else {
|
2026-01-26 16:10:30 +00:00
|
|
|
log.Info().Str("dest", destination).Msg("Text message response is nil")
|
2026-01-21 03:30:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendSMS(destination, source, message string) error {
|
|
|
|
|
client := twilio.NewRestClientWithParams(twilio.ClientParams{
|
|
|
|
|
Username: config.TwilioAccountSID,
|
|
|
|
|
Password: config.TwilioAuthToken,
|
|
|
|
|
})
|
|
|
|
|
params := &twilioApi.CreateMessageParams{}
|
|
|
|
|
params.SetTo("+15558675309")
|
|
|
|
|
params.SetFrom("+15017250604")
|
|
|
|
|
params.SetBody("Hello from Go!")
|
|
|
|
|
|
|
|
|
|
resp, err := client.Api.CreateMessage(params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error sending SMS message: %w", err)
|
|
|
|
|
}
|
|
|
|
|
response, _ := json.Marshal(*resp)
|
|
|
|
|
log.Debug().Str("response", string(response)).Msg("Send SMS")
|
|
|
|
|
return nil
|
|
|
|
|
}
|