example.rb in whatsapp_sdk-0.1.0 vs example.rb in whatsapp_sdk-0.2.0

- old
+ new

@@ -9,152 +9,192 @@ gemfile(true) do source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } - gem "whatsapp_sdk" + gem "whatsapp_sdk", github: "ignacio-chiazzo/whatsapp_sdk", branch: "main" gem "pry" gem "pry-nav" end require 'whatsapp_sdk' require "pry" require "pry-nav" -ACCESS_TOKEN = "foo" # replace this with a valid access_token -SENDER_ID = 123 -RECIPIENT_NUMBER = "456" +################# UPDATE CONSTANTS ################# -client = WhatsappSdk::Api::Client.new(ACCESS_TOKEN) # replace this with a valid access_token +ACCESS_TOKEN = "<TODO replace>" +SENDER_ID = "<TODO replace>" +RECIPIENT_NUMBER = "<TODO replace>" +BUSINESS_ID = "<TODO replace>" +IMAGE_LINK = "<TODO replace>" + +################# 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 +end +################################################## + +client = WhatsappSdk::Api::Client.new(ACCESS_TOKEN) +medias_api = WhatsappSdk::Api::Medias.new(client) messages_api = WhatsappSdk::Api::Messages.new(client) phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new(client) -phone_numbers_api.registered_number("123") -phone_numbers_api.registered_numbers("457") +############################## Phone Numbers API ############################## +phone_numbers_api.registered_number(SENDER_ID) +phone_numbers_api.registered_numbers(BUSINESS_ID) +############################## Media API ############################## -messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "hola") -messages_api.send_location( +# upload a media +uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png") +puts "Uploaded media id: #{uploaded_media.data&.id}" + +# get a media +media = medias_api.media(media_id: uploaded_media.data&.id).data +puts "Media info: #{media.raw_data_response}" + +# download media +download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png') +puts "Downloaded: #{download_image.data.success?}" + +# delete a media +deleted_media = medias_api.delete(media_id: media&.id) +puts "Deleted: #{deleted_media.data.success?}" + +############################## 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) + +location_sent = messages_api.send_location( sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, - longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street" + longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house" ) +print_message_sent(location_sent) -# Send images - -## with a link -messages_api.send_image( - sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "image_link", caption: "Ignacio Chiazzo Profile" +######### SEND AN IMAGE +# Send an image with a link +image_sent = messages_api.send_image( + sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.url, caption: "Ignacio Chiazzo Profile" ) +print_message_sent(image_sent) -## with an image id +# Send an image with an id messages_api.send_image( - sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: "1234", caption: "Ignacio Chiazzo Profile" + sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.id, caption: "Ignacio Chiazzo Profile" ) -# Send audios +######### SEND AUDIOS ## with a link -messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link") +# messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link") ## with an audio id -messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234") +# messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234") -# Send documents +######### SEND DOCUMENTS ## with a link -messages_api.send_document( - sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo" -) +# messages_api.send_document( +# sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo" +# ) ## with a document id -messages_api.send_document( - sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo" -) +# messages_api.send_document( +# sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo" +# ) -# send stickers +######### SEND STICKERS ## with a link -messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link") +# messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link") ## with a sticker_id -messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234") +# messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234") -# Send a template. +######### SEND A TEMPLATE # Note: The template must have been created previously. -header_component = WhatsappSdk::Resource::Component.new( - type: WhatsappSdk::Resource::Component::Type::HEADER -) +# Send a template with no component +response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, + name: "hello_world", language: "en_US", components: []) +puts response_with_object -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) +# Send a template with components. Remember to create the template first. +# header_component = WhatsappSdk::Resource::Component.new( +# type: WhatsappSdk::Resource::Component::Type::HEADER +# ) -parameter_image = WhatsappSdk::Resource::ParameterObject.new( - type: "image", - image: image -) +# 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) -parameter_document = WhatsappSdk::Resource::ParameterObject.new( - type: "document", - document: document -) +# parameter_image = WhatsappSdk::Resource::ParameterObject.new( +# type: "image", +# image: image +# ) -parameter_video = WhatsappSdk::Resource::ParameterObject.new( - type: "video", - video: video -) +# parameter_document = WhatsappSdk::Resource::ParameterObject.new( +# type: "document", +# document: document +# ) -parameter_text = WhatsappSdk::Resource::ParameterObject.new( - type: "text", - text: "I am a text" -) +# parameter_video = WhatsappSdk::Resource::ParameterObject.new( +# type: "video", +# video: video +# ) -header_component.add_parameter(parameter_text) -header_component.add_parameter(parameter_image) -header_component.add_parameter(parameter_video) -header_component.add_parameter(parameter_document) -header_component.to_json +# parameter_text = WhatsappSdk::Resource::ParameterObject.new( +# type: "text", +# text: "I am a text" +# ) -body_component = WhatsappSdk::Resource::Component.new( - type: WhatsappSdk::Resource::Component::Type::BODY -) -body_component.add_parameter(parameter_text) -body_component.add_parameter(parameter_image) -body_component.add_parameter(parameter_video) -body_component.add_parameter(parameter_document) -body_component.to_json +# header_component.add_parameter(parameter_text) +# header_component.add_parameter(parameter_image) +# header_component.add_parameter(parameter_video) +# header_component.add_parameter(parameter_document) +# header_component.to_json -button_component_1 = WhatsappSdk::Resource::Component.new( - type: WhatsappSdk::Resource::Component::Type::BUTTON, - index: 0, - sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY, - parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")] -) +# body_component = WhatsappSdk::Resource::Component.new( +# type: WhatsappSdk::Resource::Component::Type::BODY +# ) +# body_component.add_parameter(parameter_text) +# body_component.add_parameter(parameter_image) +# body_component.add_parameter(parameter_video) +# body_component.add_parameter(parameter_document) +# body_component.to_json -button_component_2 = WhatsappSdk::Resource::Component.new( - type: WhatsappSdk::Resource::Component::Type::BUTTON, - index: 1, - sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY, - parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")] -) +# button_component_1 = WhatsappSdk::Resource::Component.new( +# type: WhatsappSdk::Resource::Component::Type::BUTTON, +# index: 0, +# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY, +# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")] +# ) -# make sure that the template was created -response_with_json = messages_api.send_template( - sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US", - components_json: [ - { - "type": "header", - "parameters": [ - { - "type": "image", - "image": { - "link": "https://www.google.com/imgres?imgurl=https%3A%2F%2Fqph.cf2.quoracdn.net%2Fmain-qimg-6d977408fdd90a09a1fee7ba9e2f777c-lq&imgrefurl=https%3A%2F%2Fwww.quora.com%2FHow-can-I-find-my-WhatsApp-ID&tbnid=lDAx1vzXwqCakM&vet=12ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ..i&docid=s-DNQVCrZmhJYM&w=602&h=339&q=example%20whatsapp%20image%20id&ved=2ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ" - } - } - ] - } - ] -) -puts response_with_json +# button_component_2 = WhatsappSdk::Resource::Component.new( +# type: WhatsappSdk::Resource::Component::Type::BUTTON, +# index: 1, +# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY, +# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")] +# ) -response_with_object = messages_api.send_template( - sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US", - components: [header_component, body_component, button_component_1, button_component_2] -) -puts response_with_object +# # Send a template with component_json +# response_with_json = messages_api.send_template( +# sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US", +# components_json: [ +# { +# "type" => "header", +# "parameters" => [ +# { +# "type" => "image", +# "image" => { +# "link" => "https://www.google.com/imgres?imgurl=https%3A%2F%2Fqph.cf2.quoracdn.net%2Fmain-qimg-6d977408fdd90a09a1fee7ba9e2f777c-lq&imgrefurl=https%3A%2F%2Fwww.quora.com%2FHow-can-I-find-my-WhatsApp-ID&tbnid=lDAx1vzXwqCakM&vet=12ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ..i&docid=s-DNQVCrZmhJYM&w=602&h=339&q=example%20whatsapp%20image%20id&ved=2ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ" +# } +# } +# ] +# } +# ] +# ) +# puts response_with_json