tsk-7: Change name of repo (#8)
Closes #7 Reviewed-on: #8 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/twilio/twilio-go"
|
||||
twilioApi "github.com/twilio/twilio-go/rest/api/v2010"
|
||||
|
||||
"git.kundeng.us/phoenix/sender/models"
|
||||
)
|
||||
|
||||
func SendMessages(config models.TwiloDetails, numbers [](models.Number), message models.Message, secondInterval int) {
|
||||
fmt.Println("Sending message")
|
||||
|
||||
longSleepLimit := 5
|
||||
count := 0
|
||||
|
||||
client := initClient(config)
|
||||
|
||||
for _, number := range numbers {
|
||||
numberToSend := number
|
||||
|
||||
if numberToSend.IsEmpty() {
|
||||
fmt.Println("Empty number")
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(numberToSend)
|
||||
|
||||
params := &twilioApi.CreateMessageParams{}
|
||||
params.SetTo(numberToSend.PhoneNumber)
|
||||
params.SetFrom(config.Number)
|
||||
params.SetBody(message.Text)
|
||||
|
||||
fmt.Println("Sending to: " + numberToSend.PhoneNumber)
|
||||
|
||||
resp, err := client.Api.CreateMessage(params)
|
||||
if err != nil {
|
||||
fmt.Println("Error sending SMS message: " + err.Error())
|
||||
} else {
|
||||
response, _ := json.Marshal(*resp)
|
||||
fmt.Println("Response: " + string(response))
|
||||
}
|
||||
|
||||
if count%longSleepLimit == 0 {
|
||||
// sleep for X seconds
|
||||
tme := 5 * time.Second
|
||||
time.Sleep(tme)
|
||||
} else {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initClient(config models.TwiloDetails) *twilio.RestClient {
|
||||
return twilio.NewRestClientWithParams(twilio.ClientParams{
|
||||
Username: config.AccountSID,
|
||||
Password: config.AuthToken,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user