Last active
November 10, 2022 05:14
-
-
Save BizarroDavid/40f644de19a93039de5e67439de704b4 to your computer and use it in GitHub Desktop.
Sending an SMS Text Message using AWS SNS service in GoLang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//assumes you have the following environment variables setup for AWS session creation | |
// AWS_SDK_LOAD_CONFIG=1 | |
// AWS_ACCESS_KEY_ID=XXXXXXXXXX | |
// AWS_SECRET_ACCESS_KEY=XXXXXXXX | |
// AWS_REGION=us-west-2( or AWS_DEFAULT_REGION=us-east-1 if you are having trouble) | |
package main | |
import ( | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/sns" | |
) | |
func main() { | |
fmt.Println("creating session") | |
sess := session.Must(session.NewSession()) | |
fmt.Println("session created") | |
svc := sns.New(sess) | |
fmt.Println("service created") | |
params := &sns.PublishInput{ | |
Message: aws.String("testing 123"), | |
PhoneNumber: aws.String("+14445556666"), | |
} | |
resp, err := svc.Publish(params) | |
if err != nil { | |
// Print the error, cast err to awserr.Error to get the Code and | |
// Message from an error. | |
fmt.Println(err.Error()) | |
return | |
} | |
// Pretty-print the response data. | |
fmt.Println(resp) | |
} |
@BizarroDavid
This code was very helpful.
Did you know how to see where it came from?
If it is as it is, it will be displayed as NOTICE
@yuzujoe - You might need to add "AWS.SNS.SMS.SenderID" in MessageAttributes
params := &sns.PublishInput{
Message: aws.String(<sms text>),
PhoneNumber: aws.String(<phone number>),
MessageAttributes: map[string]*sns.MessageAttributeValue{
"AWS.SNS.SMS.SenderID": &sns.MessageAttributeValue{StringValue: aws.String(<sender id>), DataType: aws.String("String")},
"AWS.SNS.SMS.SMSType": &sns.MessageAttributeValue{StringValue: aws.String("Transactional"), DataType: aws.String("String")},
},
}
Hi, thanks for sharing this code, help me to get my own solution:
here is the aws-v2 code:
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sns"
)
func main() {
config, err := config.LoadDefaultConfig(context.TODO()) //Get local credentias ~/.aws/credentials
if err != nil {
panic("configuration error, " + err.Error())
}
client := sns.NewFromConfig(config)
params := &sns.PublishInput{
Message: aws.String("Testing..."),
PhoneNumber: aws.String("+{yournumber}"),
}
resp, err := client.Publish(context.TODO(), params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
}
@yuzujoe - You might need to add "AWS.SNS.SMS.SenderID" in MessageAttributes
params := &sns.PublishInput{ Message: aws.String(<sms text>), PhoneNumber: aws.String(<phone number>), MessageAttributes: map[string]*sns.MessageAttributeValue{ "AWS.SNS.SMS.SenderID": &sns.MessageAttributeValue{StringValue: aws.String(<sender id>), DataType: aws.String("String")}, "AWS.SNS.SMS.SMSType": &sns.MessageAttributeValue{StringValue: aws.String("Transactional"), DataType: aws.String("String")}, }, }
hi i'm using above code but i'm getting like this
sion created
service created
ParameterValueInvalid: The SMS message type provided 'SMS' is not a valid message type
status code: 400, request id: f2a23e6a-0d95-5fc4-9fb7-674aa2057dd9
i am using above code but here sender id not working please help us
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks mkinney. I updated the gist.