Skip to content

Commit

Permalink
Adding Messages Sandbox WhatsApp snippet (#104)
Browse files Browse the repository at this point in the history
* Adding Messages Sandbox examples
  • Loading branch information
superchilled authored Aug 30, 2024
1 parent 1e85f18 commit 977d149
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
18 changes: 14 additions & 4 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ VONAGE_APPLICATION_PRIVATE_KEY_PATH=private.key
TO_NUMBER=
VONAGE_NUMBER=

# For Facebook messaging
# Vonage Messages API variables

## For Messages Sandbox
MESSAGES_SANDBOX_URL=
MESSAGES_SANDBOX_WHATSAPP_NUMBER=
MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER=
MESSAGES_SANDBOX_FB_ID=
MESSAGES_SANDBOX_ALLOW_LISTED_FB_RECIPIENT_ID=
MESSAGES_SANDBOX_VIBER_SERVICE_ID=

## For Facebook messaging
FB_SENDER_ID=
FB_RECIPIENT_ID=

# For Viber messaging
## For Viber messaging
VIBER_SERVICE_MESSAGE_ID=

# For WhatsApp messaging
## For WhatsApp messaging
WHATSAPP_NUMBER=
WHATSAPP_TEMPLATE_NAMESPACE=
WHATSAPP_TEMPLATE_NAME=
WHATSAPP_AUTH_TEMPLATE_NAME=
WHATSAPP_TEMPLATE_REPLACEMENT_TEXT=

# For media messaging (WhatsApp, Messenger, Viber)
## For media messaging (WhatsApp, Messenger, Viber)
IMAGE_URL=
IMAGE_CAPTION=
FILE_URL=
Expand Down
25 changes: 25 additions & 0 deletions messages/sandbox/messenger/send-text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'dotenv/load'
require 'vonage'

VONAGE_APPLICATION_ID = ENV['VONAGE_APPLICATION_ID']
VONAGE_APPLICATION_PRIVATE_KEY_PATH = ENV['VONAGE_APPLICATION_PRIVATE_KEY_PATH']
MESSAGES_SANDBOX_URL= ENV['MESSAGES_SANDBOX_URL']
MESSAGES_SANDBOX_FB_ID = ENV['MESSAGES_SANDBOX_FB_ID']
MESSAGES_SANDBOX_ALLOW_LISTED_FB_RECIPIENT_ID = ENV['MESSAGES_SANDBOX_ALLOW_LISTED_FB_RECIPIENT_ID']

client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
api_host: MESSAGES_SANDBOX_URL
)

message = Vonage::Messaging::Message.messenger(
type: 'text',
message: "This is a Facebook Messenger text message sent using the Messages API via the Messages Sandbox"
)

client.messaging.send(
from: MESSAGES_SANDBOX_FB_ID,
to: MESSAGES_SANDBOX_ALLOW_LISTED_FB_RECIPIENT_ID,
**message
)
25 changes: 25 additions & 0 deletions messages/sandbox/viber/send-text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'dotenv/load'
require 'vonage'

VONAGE_APPLICATION_ID = ENV['VONAGE_APPLICATION_ID']
VONAGE_APPLICATION_PRIVATE_KEY_PATH = ENV['VONAGE_APPLICATION_PRIVATE_KEY_PATH']
MESSAGES_SANDBOX_URL= ENV['MESSAGES_SANDBOX_URL']
MESSAGES_SANDBOX_VIBER_SERVICE_ID = ENV['MESSAGES_SANDBOX_VIBER_SERVICE_ID']
MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER = ENV['MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER']

client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
api_host: MESSAGES_SANDBOX_URL
)

message = Vonage::Messaging::Message.viber(
type: 'text',
message: "This is a Viber Service Message text message sent using the Messages API via the Messages Sandbox"
)

client.messaging.send(
from: MESSAGES_SANDBOX_VIBER_SERVICE_ID,
to: MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER,
**message
)
25 changes: 25 additions & 0 deletions messages/sandbox/whatsapp/send-text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'dotenv/load'
require 'vonage'

VONAGE_APPLICATION_ID = ENV['VONAGE_APPLICATION_ID']
VONAGE_APPLICATION_PRIVATE_KEY_PATH = ENV['VONAGE_APPLICATION_PRIVATE_KEY_PATH']
MESSAGES_SANDBOX_URL= ENV['MESSAGES_SANDBOX_URL']
MESSAGES_SANDBOX_WHATSAPP_NUMBER = ENV['MESSAGES_SANDBOX_WHATSAPP_NUMBER']
MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER = ENV['MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER']

client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
api_host: MESSAGES_SANDBOX_URL
)

message = Vonage::Messaging::Message.whatsapp(
type: 'text',
message: "This is a WhatsApp Message text message sent using the Messages API via the Messages Sandbox"
)

client.messaging.send(
from: MESSAGES_SANDBOX_WHATSAPP_NUMBER,
to: MESSAGES_SANDBOX_ALLOW_LISTED_TO_NUMBER,
**message
)

0 comments on commit 977d149

Please sign in to comment.