|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/aws/session" |
| 9 | + "github.com/aws/aws-sdk-go/service/sns" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + err := SendSMS("Phone Number", "Hi, I am from AWS SNS") |
| 14 | + if err != nil { |
| 15 | + log.Fatal(err) |
| 16 | + } |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +/* |
| 21 | +const ( |
| 22 | + // Replace AccessKeyID with your AccessKeyID key. |
| 23 | + AccessKeyID = "Access Key" |
| 24 | +
|
| 25 | + // Replace AccessKeyID with your AccessKeyID key. |
| 26 | + SecretAccessKey = "Access Secret" |
| 27 | +
|
| 28 | + // Replace us-west-2 with the AWS Region you're using for Amazon SNS. |
| 29 | + AwsRegion = "us-west-2" |
| 30 | +) |
| 31 | +*/ |
| 32 | + |
| 33 | +func SendSMS(phoneNumber string, message string) error { |
| 34 | + // Create Session and assign AccessKeyID and SecretAccessKey |
| 35 | + /* |
| 36 | + sess, err := session.NewSession(&aws.Config{ |
| 37 | + Region: aws.String(AwsRegion), |
| 38 | + Credentials: credentials.NewStaticCredentials(AccessKeyID, SecretAccessKey, ""), |
| 39 | + }, |
| 40 | + ) |
| 41 | + */ |
| 42 | + |
| 43 | + // |
| 44 | + sess := session.Must(session.NewSessionWithOptions(session.Options{ |
| 45 | + SharedConfigState: session.SharedConfigEnable, |
| 46 | + })) |
| 47 | + |
| 48 | + // Create SNS service |
| 49 | + svc := sns.New(sess) |
| 50 | + |
| 51 | + // Pass the phone number and message. |
| 52 | + params := &sns.PublishInput{ |
| 53 | + PhoneNumber: aws.String(phoneNumber), |
| 54 | + Message: aws.String(message), |
| 55 | + } |
| 56 | + |
| 57 | + // sends a text message (SMS message) directly to a phone number. |
| 58 | + resp, err := svc.Publish(params) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + log.Println(err.Error()) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Println(resp) // print the response data. |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
0 commit comments