smsbb/smsbb.go

49 lines
1.1 KiB
Go
Raw Normal View History

2024-01-08 00:09:38 +00:00
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/twilio/twilio-go"
twilioApi "github.com/twilio/twilio-go/rest/api/v2010"
)
func main () {
2024-01-08 00:24:04 +00:00
accountSid := os.Getenv("SMSBB_ACCT_SID")
2024-01-08 00:09:38 +00:00
authToken := os.Getenv("SMSBB_AUTH_TOKEN")
if (len(accountSid) == 0) {
2024-01-08 00:24:04 +00:00
fmt.Fprintf(os.Stderr, "Please set the SMSBB_ACCT_SID environment variable to your Twilio account SID\n")
2024-01-08 00:09:38 +00:00
os.Exit(1)
}
if (len(authToken) == 0) {
fmt.Fprintf(os.Stderr, "Please set the SMSBB_AUTH_TOKEN environment variable to your Twilio authentication token\n")
os.Exit(1)
}
if (len(os.Args) < 4) {
fmt.Fprintf(os.Stderr, "%s FROM-NUMBER TO-NUMBER MESSAGE-BODY\n", os.Args[0])
os.Exit(1)
}
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: accountSid,
Password: authToken,
})
params := &twilioApi.CreateMessageParams{}
params.SetFrom(os.Args[1])
params.SetTo(os.Args[2])
params.SetBody(os.Args[3])
resp, err := client.Api.CreateMessage(params)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending SMS message: %s", err.Error())
} else {
response, _ := json.Marshal(*resp)
fmt.Printf("Response from Twilio: %s", string(response))
}
}