smsbb/smsbb.go

49 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
"os"
"github.com/twilio/twilio-go"
twilioApi "github.com/twilio/twilio-go/rest/api/v2010"
)
func main () {
accountSid := os.Getenv("SMSBB_ACCT_SID")
authToken := os.Getenv("SMSBB_AUTH_TOKEN")
if (len(accountSid) == 0) {
fmt.Fprintf(os.Stderr, "Please set the SMSBB_ACCT_SID environment variable to your Twilio account SID\n")
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))
}
}