example.rb in whatsapp_sdk-0.3.2 vs example.rb in whatsapp_sdk-0.4.0
- old
+ new
@@ -9,28 +9,26 @@
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
- gem "whatsapp_sdk", path: "/Users/ignaciochiazzo/src/whatsapp_sdk"
+ gem "whatsapp_sdk"
gem "pry"
gem "pry-nav"
end
require 'whatsapp_sdk'
require "pry"
require "pry-nav"
################# UPDATE CONSTANTS #################
-ACCESS_TOKEN = "EAAHwuHDNkqoBAOtjFZAw1MUOXsFOv4EMP8Hi2mtzvcHSPbYWD4HLLLRapjPIDQWagke3cF2IIZCbtlhMZCluO1sH7C594q0q9UqZAXwufTqBtebRlssZCLwKeC69l6Emdmt2gRYGDnhTJH0mrM7L5ivZAWezhjOvtzdnKJ7xBZALiQRKB6JrEAgAZBcnBMfBZC75xR0ZACXQUPoplhjN1a5FTA" # replace this with a valid access_token # TODO replace
-SENDER_ID = 107_878_721_936_019
-RECIPIENT_NUMBER = 13_437_772_910
-BUSINESS_ID = 114_503_234_599_312
-IMAGE_LINK = "https://ignaciochiazzo.com/static/4c403819b9750c8ad8b20a75308f2a8a/876d5/profile-pic.avif"
-binding.pry
-# message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "holis")
+ACCESS_TOKEN = "<TODO replace>"
+SENDER_ID = "<TODO replace>"
+RECIPIENT_NUMBER = "<TODO replace>"
+BUSINESS_ID = "<TODO replace>"
+IMAGE_LINK = "<TODO replace>"
################# Initialize Client #################
WhatsappSdk.configure do |config|
config.access_token = ACCESS_TOKEN
end
@@ -48,30 +46,30 @@
medias_api = WhatsappSdk::Api::Medias.new
messages_api = WhatsappSdk::Api::Messages.new
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
############################## Phone Numbers API ##############################
-phone_numbers_api.registered_number(SENDER_ID)
-phone_numbers_api.registered_numbers(BUSINESS_ID)
+registered_number = phone_numbers_api.registered_number(SENDER_ID)
+registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
############################## Media API ##############################
# 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}"
+media_id = uploaded_media.data&.id
+puts "Uploaded media id: #{media_id}"
# get a media
-uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
-media = medias_api.media(media_id: uploaded_media.data&.id).data
+media = medias_api.media(media_id: media_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?}"
+# 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,
@@ -132,16 +130,16 @@
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
+ 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)
+video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
parameter_image = WhatsappSdk::Resource::ParameterObject.new(
type: "image",
image: image
)
@@ -166,31 +164,33 @@
header_component.add_parameter(parameter_video)
header_component.add_parameter(parameter_document)
header_component.to_json
body_component = WhatsappSdk::Resource::Component.new(
- type: WhatsappSdk::Resource::Component::Type::BODY
+ 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_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")]
-# )
+button_component_1 = WhatsappSdk::Resource::Component.new(
+ type: WhatsappSdk::Resource::Component::Type::Button,
+ index: 0,
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
+ parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
+ payload: "payload")]
+)
-# 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_2 = WhatsappSdk::Resource::Component.new(
+ type: WhatsappSdk::Resource::Component::Type::Button,
+ index: 1,
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
+ 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(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
components_json: [
@@ -206,10 +206,5 @@
]
}
]
)
puts response_with_json
-
-
-# # delete a media
-deleted_media = medias_api.delete(media_id: media&.id)
-puts "Deleted: #{deleted_media.data.success?}"
\ No newline at end of file