example.rb in whatsapp_sdk-0.13.0 vs example.rb in whatsapp_sdk-1.0.0
- old
+ new
@@ -31,50 +31,46 @@
if ACCESS_TOKEN == "<TODO replace>"
puts "\n\n**** Please update the ACCESS_TOKEN constant in this file. ****\n\n"
exit
end
-puts "\n\n\n\n\n\n *************** Starting calling the Cloud API *************** \n"
+puts "\n\n\n\ ------------------ Starting calling the Cloud API -------------------\n"
################# Initialize Client #################
WhatsappSdk.configure do |config|
config.access_token = ACCESS_TOKEN
end
################# HELPERS ########################
-def print_message_sent(message_response)
- if message_response.ok?
- puts "Message sent to: #{message_response.data.contacts.first.input}"
- else
- puts "Error: #{message_response.error&.to_s}"
- end
+def print_message_sent(message_response, type = "")
+ puts "Message #{type} sent to: #{message_response.contacts.first.input}"
end
-def print_data_or_error(response, identifier)
- if response.error?
- return "Error: #{response.error&.to_s}"
+def run_and_catch_error(message, &block)
+ begin
+ yield
+ rescue WhatsappSdk::Api::Responses::HttpResponseError => e
+ puts "Error: #{e}"
end
-
- return identifier
end
##################################################
+client = WhatsappSdk::Api::Client.new
-medias_api = WhatsappSdk::Api::Medias.new
-messages_api = WhatsappSdk::Api::Messages.new
-phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
-business_profile_api = WhatsappSdk::Api::BusinessProfile.new
-templates_api = WhatsappSdk::Api::Templates.new
-############################## Templates API ##############################
-## Get list of templates
-templates = templates_api.templates(business_id: BUSINESS_ID)
-puts "GET Templates list : #{print_data_or_error(templates, templates.data&.templates.map { |r| r.template.name })}"
+
+# ############################## Templates API ##############################
+puts "\n\n ------------------ Testing Templates API ------------------------"
+
+# ## Get list of templates
+templates = client.templates.list(business_id: BUSINESS_ID)
+puts "GET Templates list : #{ templates.records.map(&:name) }"
+
## Get message templates namespace
-template_namespace = templates_api.get_message_template_namespace(business_id: BUSINESS_ID)
-puts "GET template by namespace: #{print_data_or_error(template_namespace, template_namespace.data&.id)}"
+template_namespace = client.templates.get_message_template_namespace(business_id: BUSINESS_ID)
+puts "GET template by namespace: #{template_namespace.id}"
# Create a template
components_json = [
{
"type": "BODY",
@@ -102,16 +98,20 @@
}
]
}
]
-new_template = templates_api.create(
- business_id: BUSINESS_ID, name: "seasonal_promotion", language: "ka", category: "MARKETING",
- components_json: components_json, allow_category_change: true
-)
-puts "GET template by namespace: #{print_data_or_error(template_namespace, template_namespace.data&.id)}"
+new_template = nil
+run_and_catch_error("Create a template") do
+ new_template = client.templates.create(
+ business_id: BUSINESS_ID, name: "seasonal_promotion_2", language: "ka", category: "MARKETING",
+ components_json: components_json, allow_category_change: true
+ )
+ puts "GET template by namespace: #{template_namespace.id}"
+end
+
# Update a template
components_json = [
{
"type" => "header",
"parameters" => [
@@ -122,164 +122,248 @@
}
}
]
}
]
-updated_template = templates_api.update(template_id: "1624560287967996", category: "UTILITY")
-puts "UPDATE template by id: #{print_data_or_error(updated_template, updated_template.data&.id)}"
+if new_template
+ updated_template = client.templates.update(template_id: new_template.id, category: "UTILITY")
+ puts "UPDATE template by id: #{updated_template.id}"
+end
+
## Delete a template
-delete_template = templates_api.delete(business_id: BUSINESS_ID, name: "seasonal_promotion") # delete by name
-puts "DELETE template by id: #{print_data_or_error(delete_template, delete_template.data&.id) }"
-# templates_api.delete(business_id: BUSINESS_ID, name: "name2", hsm_id: "243213188351928") # delete by name and id
+run_and_catch_error("Delete a template") do
+ delete_template = client.templates.delete(business_id: BUSINESS_ID, name: "seasonal_promotion") # delete by name
+ puts "Delete template by id: #{delete_template.id}"
+end
-############################## Business API ##############################
-business_profile = business_profile_api.details(SENDER_ID)
-puts "DELETE Business Profile by id: #{print_data_or_error(delete_template, business_profile.data&.about) }"
+# client.templates.delete(business_id: BUSINESS_ID, name: "name2", hsm_id: "243213188351928") # delete by name and id
-updated_bp = business_profile_api.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
-puts "UPDATE Business Profile by id: #{print_data_or_error(updated_bp, updated_bp.data&.success?) }"
+
+# ############################## Business API ##############################
+puts "\n\n\n ------------------ Testing Business API -----------------------"
+
+business_profile = client.business_profiles.get(SENDER_ID)
+puts "GET Business Profile by id: #{business_profile.about}"
+
+updated_bp = client.business_profiles.update(phone_number_id: SENDER_ID, params: { websites: ["www.ignaciochiazzo.com"] } )
+puts "UPDATE Business Profile by id: #{updated_bp} }"
+
+run_and_catch_error("Update business profile") do
+ # about can't be set
+ updated_bp = client.business_profiles.update(phone_number_id: SENDER_ID, params: { about: "A cool business" } )
+end
+
+
+
############################## Phone Numbers API ##############################
-registered_number = phone_numbers_api.registered_number(SENDER_ID)
-puts "GET Registered number: #{print_data_or_error(registered_number, registered_number.data&.id)}"
+puts "\n\n\n ------------------ Testing Phone Numbers API -----------------------"
-registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
-puts "GET Registered numbers: #{print_data_or_error(registered_number, registered_numbers.data&.phone_numbers.map(&:id))}"
+# Get phone numbers
+registered_number = client.phone_numbers.get(SENDER_ID)
+puts "GET Registered number: #{registered_number.id}"
-############################## Media API ##############################
+# Get phone number
+registered_numbers = client.phone_numbers.list(BUSINESS_ID)
+puts "GET Registered numbers: #{registered_numbers.records.map(&:id)}"
+# Deregister a phone number - I skip registering so that the number can upload media
+# run_and_catch_error("Deregister a phone number") do
+# deregister_number_result = client.phone_numbers.deregister_number(SENDER_ID)
+# puts "DEREGISTER number: #{deregister_number_result}"
+# end
+
+# Register a phone number
+run_and_catch_error("Register a phone number") do
+ register_number_result = client.phone_numbers.register_number(SENDER_ID, 123456)
+ puts "REGISTER number: #{register_number_result}"
+end
+
+# Register a fake number
+begin
+ fake_number = "1234567890"
+ client.phone_numbers.register_number(fake_number, 123456)
+rescue WhatsappSdk::Api::Responses::HttpResponseError => e
+ puts "Error: #{e}"
+end
+
+
+############################## Media API ##############################
+puts "\n\n\n ------------------ Testing Media API"
##### Image #####
# upload a Image
-uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
-puts "Uploaded media id: #{print_data_or_error(uploaded_media, uploaded_media.data&.id)}"
+run_and_catch_error("Upload a Image") do
+ uploaded_media = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/whatsapp.png", type: "image/png")
+ puts "Uploaded image id: #{uploaded_media&.id}"
+end
# get a media Image
-if uploaded_media.data&.id
- media = medias_api.media(media_id: uploaded_media.data&.id)
- puts "GET Media id: #{print_data_or_error(media, media.data&.id)}"
+if uploaded_media&.id
+ image = client.media.get(media_id: uploaded_media.id)
+ puts "GET image id: #{image.id}"
# download media Image
- download_image = medias_api.download(url: media.data.url, file_path: 'tmp/downloaded_image.png', media_type: "image/png")
- puts "Downloaded: #{print_data_or_error(download_image, download_image.data.success?)}"
+ download_image = client.media.download(url: image.url, file_path: 'test/fixtures/assets/downloaded_image.png', media_type: "image/png")
+ puts "Downloaded Image: #{download_image}"
+ uploaded_media = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/whatsapp.png", type: "image/png")
# delete a media
- deleted_media = medias_api.delete(media_id: media.data.id)
- puts "DELETE: #{print_data_or_error(deleted_media, deleted_media.data.success?)}"
+ deleted_media = client.media.delete(media_id: uploaded_media.id)
+ puts "Delete image: #{deleted_media.success?}"
+else
+ puts "No media to download and delete"
end
+#### Video ####
+# upload a video
+uploaded_video = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/riquelme.mp4", type: "video/mp4")
+puts "Uploaded video: #{uploaded_video.id}"
+
+video = client.media.get(media_id: uploaded_video.id)
+
+# upload a video
+uploaded_video = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/riquelme.mp4", type: "video/mp4")
+puts "Uploaded video id: #{uploaded_video.id}"
+
#### Audio ####
# upload an audio
-uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/downloaded_audio.ogg", type: "audio/ogg")
-puts "Uploaded media id: #{print_data_or_error(uploaded_media, uploaded_media.data&.id)}"
+audio_response = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/downloaded_audio.ogg", type: "audio/ogg")
+puts "Uploaded audio id: #{audio_response.id}"
-if uploaded_media.data&.id
-media_id = uploaded_media.data&.id
-media_id = uploaded_media.data&.id
-puts "Uploaded media id: #{media_id}"
- media_id = uploaded_media.data&.id
-puts "Uploaded media id: #{media_id}"
-
+if audio_response&.id
+ audio_id = audio_response&.id
# get a media audio
- media = medias_api.media(media_id: media_id)
- puts "GET Media id: #{print_data_or_error(media, media.data&.id)}"
+ audio = client.media.get(media_id: audio_id)
+ puts "GET Audio id: #{audio.id}"
# get a media audio
- audio_link = media.data.url
- download_image = medias_api.download(url: audio_link, file_path: 'tmp/downloaded_audio2.ogg', media_type: "audio/ogg")
- puts "Download Media Audio: #{print_data_or_error(download_image, download_image.data.success?)}"
+ audio_link = audio.url
+ download_image = client.media.download(url: audio_link, file_path: 'test/fixtures/assets/downloaded_audio2.ogg', media_type: "audio/ogg")
+ puts "Download Audio: #{download_image}"
end
+# upload a document
+document_response = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/document.pdf", type: "application/pdf")
+puts "Uploaded document id: #{document_response.id}"
+
+document = client.media.get(media_id: document_response.id)
+puts "GET document id: #{document.id}"
+
+# upload a sticker
+sticker_response = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/sticker.webp", type: "image/webp")
+puts "Uploaded sticker id: #{sticker_response.id}"
+
+sticker = client.media.get(media_id: sticker_response.id)
+puts "GET Sticker id: #{sticker.id}"
+
+
+
############################## Messages API ##############################
+puts "\n\n\n ------------------ Testing Messages API -----------------------"
######### SEND A TEXT MESSAGE
-message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "Hey there! it's Whatsapp Ruby SDK")
-print_message_sent(message_sent)
+message_sent = client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
+ message: "Hey there! it's Whatsapp Ruby SDK")
+print_message_sent(message_sent, "text")
######### React to a message
-message_id = message_sent.data.messages.first.id
-reaction_1_sent = messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "\u{1f550}")
-reaction_2_sent = messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "⛄️")
-puts "Message Reaction 1: #{print_data_or_error(reaction_1_sent, reaction_1_sent.data&.messages.first&.id)}"
-puts "Message Reaction 2: #{print_data_or_error(reaction_2_sent, reaction_2_sent.data&.messages.first&.id)}"
+message_id = message_sent&.messages.first.id
+reaction_1_sent = client.messages.send_reaction(
+ sender_id: SENDER_ID,
+ recipient_number: RECIPIENT_NUMBER,
+ message_id: message_id,
+ emoji: "\u{1f550}"
+) if message_id
+reaction_2_sent = client.messages.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
+ message_id: message_id, emoji: "⛄️") if message_id
+puts "Message Reaction 1: #{reaction_1_sent&.messages.first&.id}"
+puts "Message Reaction 2: #{reaction_2_sent&.messages.first&.id}"
+
######### Reply to a message
-message_to_reply_id = message_sent.data.messages.first.id
-reply = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "I'm a reply", message_id: message_to_reply_id)
-print_message_sent(reply)
+message_to_reply_id = message_sent.messages.first.id
+reply = client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "I'm a reply",
+ message_id: message_to_reply_id)
+print_message_sent(reply, "reply")
######### Send location
-location_sent = messages_api.send_location(
+
+location_sent = client.messages.send_location(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
)
-print_message_sent(location_sent)
+print_message_sent(location_sent, "location")
######### READ A MESSAGE
-# messages_api.read_message(sender_id: SENDER_ID, message_id: msg_id)
+# client.messages.read_message(sender_id: SENDER_ID, message_id: msg_id)
######### SEND AN IMAGE
# Send an image with a link
-if media.data&.id
- image_sent = messages_api.send_image(
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.data.url, caption: "Ignacio Chiazzo Profile"
+if image&.id
+ image_sent = client.messages.send_image(
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: image.url, caption: "Ignacio Chiazzo Profile"
)
- print_message_sent(image_sent)
+ print_message_sent(image_sent, "image via url")
# Send an image with an id
- messages_api.send_image(
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.data.id, caption: "Ignacio Chiazzo Profile"
+ client.messages.send_image(
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: image.id, caption: "Ignacio Chiazzo Profile"
)
- print_message_sent(image_sent)
+ print_message_sent(image_sent, "image via id")
end
######### SEND AUDIOS
## with a link
-audio_sent = messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
-print_message_sent(audio_sent)
+audio_sent = client.messages.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: audio.url)
+print_message_sent(audio_sent, "audio via url")
## with an audio id
-audio_sent = messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
-print_message_sent(audio_sent)
+audio_sent = client.messages.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: audio.id)
+print_message_sent(audio_sent, "audio via id")
######### SEND DOCUMENTS
## with a link
-document_sent = messages_api.send_document(
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
-)
-print_message_sent(document_sent)
+if document&.id
+ document_sent = client.messages.send_document(
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: document&.url, caption: "Ignacio Chiazzo"
+ )
+ print_message_sent(document_sent, "document via url")
-## with a document id
-document_sent = messages_api.send_document(
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
-)
-print_message_sent(document_sent)
+ ## with a document id
+ document_sent = client.messages.send_document(
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: document&.id, caption: "Ignacio Chiazzo"
+ ) # modify
+ print_message_sent(document_sent, "document via id")
+end
######### SEND STICKERS
-## with a link
-sticker_sent = messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
-print_message_sent(sticker_sent)
+if sticker&.id
+ ## with a link
+ sticker_sent = client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: sticker.url)
+ print_message_sent(sticker_sent, "sticker via url")
-## with a sticker_id
-sticker_sent = messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
-print_message_sent(sticker_sent)
+ ## with a sticker_id
+ sticker_sent = client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: sticker.id)
+ print_message_sent(sticker_sent, "sticker via id")
+end
######### SEND A TEMPLATE
# Note: The template must have been created previously.
# Send a template with no component
-response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
+response_with_object = client.messages.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
name: "hello_world", language: "en_US", components: [])
puts response_with_object
# Send a template with components (Remember to create the template first).
header_component = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::HEADER
)
-image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
-document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
-video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
+image = WhatsappSdk::Resource::MediaComponent.new(type: "image", link: "http(s)://URL", caption: "caption")
+document = WhatsappSdk::Resource::MediaComponent.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
+video = WhatsappSdk::Resource::MediaComponent.new(type: "video", id: "123")
location = WhatsappSdk::Resource::Location.new(
latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
)
parameter_image = WhatsappSdk::Resource::ParameterObject.new(
@@ -338,11 +422,11 @@
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::PAYLOAD,
payload: "payload")]
)
# Send a template with component_json
-response_with_json = messages_api.send_template(
+response_with_json = client.messages.send_template(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
components_json: [
{
"type" => "header",
"parameters" => [
@@ -372,11 +456,11 @@
interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
text: "I'm the footer!"
)
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
- type: WhatsappSdk::Resource::InteractiveAction::Type::REPLY_BUTTON,
+ type: WhatsappSdk::Resource::InteractiveAction::Type::REPLY_BUTTON
)
interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
title: "I'm a reply button 1",
id: "button_1"
@@ -395,9 +479,9 @@
body: interactive_body,
footer: interactive_footer,
action: interactive_action
)
-messages_api.send_interactive_reply_buttons(
+client.messages.send_interactive_reply_buttons(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
interactive: interactive_reply_buttons
)