Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SNS with ruby, simple sms message

I'm trying to send a simple sms from ruby directly to a phone number with the SNS service.

require 'aws-sdk'
sns = Aws::SNS::Client.new(region: 'my_region', access_key_id: 'my_id', secret_access_key: 'mykey')
sns.publish({phone_number: 'my_number', message: 'test message'})

but i get ArgumentError: unexpected value at params[:phone_number]

like image 417
Alex A Avatar asked Jul 14 '16 14:07

Alex A


2 Answers

The following example using a fictional UK mobile phone number in E.164 format works for me, are you sure you're passing phone_number in a string?

require 'aws-sdk'

sns = Aws::SNS::Client.new(...)
#=> #<Aws::SNS::Client>
sns.publish(phone_number: '+447911123456', message: 'test message')
#=> #<struct Aws::SNS::Types::PublishResponse...>
like image 109
Nabeel Avatar answered Nov 15 '22 05:11

Nabeel


The simplest way to send an SMS with AWS (assuming the from number is one you own in AWS Pinpoint).

    client = client = Aws::SNS::Client.new(
      region: ENV['AWS_SNS_REGION'],
      access_key_id: ENV['AWS_SNS_ACCESS_KEY'],
      secret_access_key: ENV['AWS_SNS_SECRET_KEY']
    )

    response = client.publish(
      phone_number: "+1<your phone>",
      message: "something something",
      message_attributes: {
        "AWS.SNS.SMS.SMSType" => {
          data_type: "String",
          string_value: "Promotional",
        },
        "AWS.MM.SMS.OriginationNumber" => {
          data_type: "String",
          string_value: "+1<AWS Pinpoint number>",
        },
      },
    )
like image 29
Bhargav Tarpara Avatar answered Nov 15 '22 04:11

Bhargav Tarpara