SDK는 WhatsApp API를 사용하기위한 일련의 운영 및 클래스를 제공합니다. 스티커, 메시지, 오디오, 비디오, 위치, 메시지에 반응하여 메시지에 응답하거나 몇 단계 로이 라이브러리를 통해 전화 번호를 요청하십시오!
이 라이브러리를 사용하여 메시지를 보내는 Ruby on Rails WhatsApp 예제 저장소를 확인하십시오 .
이 라인을 응용 프로그램의 보석에 추가하십시오.
gem 'whatsapp_sdk'그런 다음 실행 :
$ bundle
또는 직접 설치하십시오.
$ gem install whatsapp_sdk
세 가지 주요 리소스, Messages , Media 및 PhoneNumbers 있습니다. Messages 통해 사용자는 모든 메시지 (텍스트, 오디오, 위치, 비디오, 이미지 등)를 보낼 수 있습니다. Media 사용하면 미디어를 관리 할 수 있으며 Phone Numbers 고객이 관련 전화 번호를 쿼리 할 수 있습니다.
Messages , Media 또는 PhoneNumbers 사용하려면 인증 정보가 포함 된 Client 를 초기화해야합니다. 그것을하는 방법에는 두 가지가 있습니다.
# config/initializers/whatsapp_sdk.rb
WhatsappSdk . configure do | config |
config . access_token = ACCESS_TOKEN
config . api_version = API_VERSION
config . logger = Logger . new ( STDOUT ) # optional, Faraday logger to attach
config . logger_options = { bodies : true } # optional, they are all valid logger_options for Faraday
end참고 :
Client 인스턴스 생성 : # without
client = WhatsappSdk :: Api :: Client . new ( "<ACCESS TOKEN>" ) # replace this with a valid access token
# OR optionally use a logger, api_version and
logger = Logger . new ( STDOUT )
logger_options = { bodies : true }
client = WhatsappSdk :: Api :: Client . new ( "<ACCESS TOKEN>" , "<API VERSION>" , logger , logger_options ) 


UI에서 휴대 전화로 메시지를 보내십시오.

예:
gem install whatsapp_sdk .irb 실행하여 IRB 터미널을 엽니 다require "whatsapp_sdk"ACCESS_TOKEN , SENDER_ID , BUSINESS_ID 및 RECIPIENT_NUMBER 설정하십시오. ACCESS_TOKEN = "EAAZAvvr0DZBs0BABRLF8zohP5Epc6pyNu"
BUSINESS_ID = 1213141516171819
SENDER_ID = 1234567891011
RECIPIENT_NUMBER = 12398765432 WhatsappSdk . configure do | config |
config . access_token = ACCESS_TOKEN
end전화 번호 API
registered_number = client . phone_numbers . registered_number ( SENDER_ID )메시지 API
message_sent = client . messages . send_text ( sender_id : SENDER_ID , recipient_number : RECIPIENT_NUMBER ,
message : "Hey there! it's Whatsapp Ruby SDK" )더 많은 예를 보려면 example.rb 파일을 확인하십시오.
client = WhatsappSdk :: Api :: Client . new ( "<ACCESS TOKEN>" ) # replace this with a valid access token
client . phone_numbers . register_number ( SENDER_ID , 123456 ) # register the phone number to uplaod media and send message from.
# send a text and a location
client . messages . send_text ( sender_id : SENDER_ID , recipient_number : RECIPIENT_NUMBER , message : "Hey there! it's Whatsapp Ruby SDK" )
client . messages . send_location ( sender_id : SENDER_ID , recipient_number : RECIPIENT_NUMBER , longitude : - 75.6898604 , latitude : 45.4192206 , name : "Ignacio" , address : "My house" )
# upload a photo and send it
image = client . media . upload ( sender_id : SENDER_ID , file_path : "test/fixtures/assets/whatsapp.png" , type : "image/png" ) image = client . media . get ( media_id : uploaded_media . id )
client . messages . send_image ( sender_id : SENDER_ID , recipient_number : RECIPIENT_NUMBER , image_id : image . id )
# upload a sticker and send it
sticker = client . media . upload ( sender_id : SENDER_ID , file_path : "test/fixtures/assets/sticker.webp" , type : "image/webp" )
client . messages . send_sticker ( sender_id : SENDER_ID , recipient_number : RECIPIENT_NUMBER , sticker_id : sticker . id ) # Get list of templates
client . templates . list ( business_id : BUSINESS_ID )
# Get Message Template Namespace
# The message template namespace is required to send messages using the message templates.
client . templates . get_message_template_namespace ( business_id : BUSINESS_ID )
# Create a template
client . templates . create (
business_id : BUSINESS_ID , name : "seasonal_promotion" , language : "en_US" , category : "MARKETING" ,
components_json : components_json , allow_category_change : true
)
# Delete a template
client . templates . delete ( business_id : BUSINESS_ID , name : "my_name" ) # delete by name # Get the details of your business
client . business_profiles . get ( phone_number_id )
# Update the details of your business
client . business_profiles . update ( phone_number_id : SENDER_ID , params : { about : "A very cool business" } ) # Get the list of phone numbers registered
client . phone_numbers . list ( business_id )
# Get the a phone number by id
client . phone_numbers . get ( phone_number_id )
# Register a phone number
client . phone_numbers . register_number ( phone_number_id , pin )
# Deregister a phone number
client . phone_numbers . deregister_number ( phone_number_id ) # Upload a media
client . media . upload ( sender_id : SENDER_ID , file_path : "tmp/whatsapp.png" , type : "image/png" )
# Get a media
media = client . media . get ( media_id : MEDIA_ID )
# Download media
client . media . download ( url : MEDIA_URL , file_path : 'tmp/downloaded_whatsapp.png' , media_type : "image/png" )
# Delete a media
client . media . delete ( media_id : MEDIA_ID ) # Send a text message
client . messages . send_text ( sender_id : 1234 , recipient_number : 112345678 , message : "hola" )
# Read a message
client . messages . read_message ( sender_id : 1234 , message_id : "wamid.HBgLMTM0M12345678910=" )
# Note: To get the `message_id` you can set up [Webhooks](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components) that will listen and fire an event when a message is received.
# Send a reaction to message
# To send a reaction to a message, you need to obtain the message id and look for the emoji's unicode you want to use.
client . messages . send_reaction ( sender_id : 123_123 , recipient_number : 56_789 , message_id : "12345" , emoji : " u{1f550} " )
client . messages . send_reaction ( sender_id : 123_123 , recipient_number : 56_789 , message_id : "12345" , emoji : "⛄️" )
# Reply to a message
# To reply to a message, just include the id of the message in the `client.messages` methods. For example, to reply to a text message include the following:
client . messages . send_text ( sender_id : 123_123 , recipient_number : 56_789 , message : "I'm a reply" , message_id : "wamid.1234" )
# Send a location message
client . messages . send_location (
sender_id : 123123 , recipient_number : 56789 ,
longitude : 45.4215 , latitude : 75.6972 , name : "nacho" , address : "141 cooper street"
)
# Send an image message
# It uses a link or an image_id.
# with a link
client . messages . send_image ( sender_id : 123123 , recipient_number : 56789 , link : "image_link" , caption : "Ignacio Chiazzo Profile" )
# with an image id
client . messages . send_image ( sender_id : 123123 , recipient_number : 56789 , image_id : "1234" , caption : "Ignacio Chiazzo Profile" )
# Send an audio message
# It uses a link or an audio_id.
# with a link
client . messages . send_audio ( sender_id : 123123 , recipient_number : 56789 , link : "audio_link" )
# with an audio id
client . messages . send_audio ( sender_id : 123123 , recipient_number : 56789 , audio_id : "1234" )
# Send a document message
# It uses a link or a document_id.
# with a link
client . messages . send_document ( sender_id : 123123 , recipient_number : 56789 , link : "document_link" , caption : "Ignacio Chiazzo" )
# with a document id
client . messages . send_document ( sender_id : 123123 , recipient_number : 56789 , document_id : "1234" , caption : "Ignacio Chiazzo" )
# Note, you can specify the filename via argument [`filename`](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages).
# Send a sticker message
# It could use a link or a sticker_id.
# with a link
client . messages . send_sticker ( sender_id : 123123 , recipient_number : 56789 , link : "link" )
# with a sticker_id
client . messages . send_sticker ( sender_id : 123123 , recipient_number : 56789 , sticker_id : "1234" )
# Send contacts message
# To send a contact, you need to create a Contact instance object that contain objects embedded like `addresses`, `birthday`, `emails`, `name`, `org`. See this [guide](/test/contact_helper.rb) to learn how to create contacts objects.
client . messages . send_contacts ( sender_id : 123123 , recipient_number : 56789 , contacts : [ create_contact ( params ) ] )
# Alternatively, you could pass a plain json like this:
client . messages . send_contacts ( sender_id : 123123 , recipient_number : 56789 , contacts_json : { ... } )
# Send a template message
# WhatsApp message templates are specific message formats that businesses use to send out notifications or customer care messages to people that have opted in to notifications. Messages can include appointment reminders, shipping information, issue resolution or payment updates.
# Before sending a message template, you need to create one. visit the [Official API Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates) currency = WhatsappSdk :: Resource :: Currency . new ( code : "USD" , amount : 1000 , fallback_value : "1000" )
date_time = WhatsappSdk :: Resource :: DateTime . new ( fallback_value : "2020-01-01T00:00:00Z" )
media_component = WhatsappSdk :: Resource :: MediaComponent . new ( type : "image" , link : "http(s)://URL" )
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 ( type : "image" , image : media_component )
parameter_text = WhatsappSdk :: Resource :: ParameterObject . new ( type : "text" , text : "TEXT_STRING" )
parameter_currency = WhatsappSdk :: Resource :: ParameterObject . new ( type : "currency" , currency : currency )
parameter_date_time = WhatsappSdk :: Resource :: ParameterObject . new ( type : "date_time" , date_time : date_time )
parameter_location = WhatsappSdk :: Resource :: ParameterObject . new ( type : "location" , location : location )
header_component = WhatsappSdk :: Resource :: Component . new ( type : "header" , parameters : [ parameter_image ] )
body_component = WhatsappSdk :: Resource :: Component . new (
type : "body" ,
parameters : [ parameter_text , parameter_currency , parameter_date_time ]
)
button_component1 = WhatsappSdk :: Resource :: Component . new (
type : "button" ,
index : 0 ,
sub_type : "quick_reply" ,
parameters : [
WhatsappSdk :: Resource :: ButtonParameter . new ( type : "payload" , payload : "PAYLOAD" )
]
)
button_component2 = WhatsappSdk :: Resource :: Component . new (
type : "button" ,
index : 1 ,
sub_type : "quick_reply" ,
parameters : [
WhatsappSdk :: Resource :: ButtonParameter . new ( type : "payload" , payload : "PAYLOAD" )
]
)
location_component = WhatsappSdk :: Resource :: Component . new ( type : "header" , parameters : [ parameter_location ] )
client . messages . send_template (
sender_id : 12_345 , recipient_number : 12345678 , name : "hello_world" , language : "en_US" , components : [ ... ]
)또는 다음과 같은 평범한 JSON을 통과 할 수 있습니다.
client . messages . send_template ( sender_id : 12_345 , recipient_number : 12345678 , name : "hello_world" , language : "en_US" , components_json : [ { ... } ] )대화식 메시지 보내기 공식 API 문서를 방문하십시오
interactive_header = WhatsappSdk :: Resource :: InteractiveHeader . new ( type : "text" , text : "I am the header!" )
interactive_body = WhatsappSdk :: Resource :: InteractiveBody . new ( text : "I am the body!" )
interactive_footer = WhatsappSdk :: Resource :: InteractiveFooter . new ( text : "I am the footer!" )
interactive_action = WhatsappSdk :: Resource :: InteractiveAction . new ( type : "list_message" )
interactive_action . button = "I am the button CTA"
interactive_section_1 = WhatsappSdk :: Resource :: InteractiveActionSection . new ( title : "I am the section 1" )
interactive_section_1_row_1 = WhatsappSdk :: Resource :: InteractiveActionSectionRow . new (
title : "I am the row 1 title" ,
id : "section_1_row_1" ,
description : "I am the optional section 1 row 1 description"
)
interactive_section_1 . add_row ( interactive_section_1_row_1 )
interactive_action . add_section ( interactive_section_1 )
interactive_list_messages = WhatsappSdk :: Resource :: Interactive . new (
type : "list" ,
header : interactive_header ,
body : interactive_body ,
footer : interactive_footer ,
action : interactive_action
)
client . messages . send_interactive_list_messages (
sender_id : SENDER_ID , recipient_number : RECIPIENT_NUMBER ,
interactive : interactive_list_messages
)또는 다음과 같은 평범한 JSON을 통과 할 수 있습니다.
client . messages . send_interactive_list_messages (
sender_id : 12_345 , recipient_number : 1234567890
interactive_json : { ... }
) interactive_header = WhatsappSdk :: Resource :: InteractiveHeader . new ( type : "text" , text : "I am the header!" )
interactive_body = WhatsappSdk :: Resource :: InteractiveBody . new ( text : "I am the body!" )
interactive_footer = WhatsappSdk :: Resource :: InteractiveFooter . new ( text : "I am the footer!" )
interactive_action = WhatsappSdk :: Resource :: InteractiveAction . new ( type : "reply_button" )
interactive_reply_button_1 = WhatsappSdk :: Resource :: InteractiveActionReplyButton . new (
title : "I am the reply button 1" ,
id : "button_1"
)
interactive_action . add_reply_button ( interactive_reply_button_1 )
interactive_reply_button_2 = WhatsappSdk :: Resource :: InteractiveActionReplyButton . new (
title : "I am the reply button 2" ,
id : "button_2"
)
interactive_action . add_reply_button ( interactive_reply_button_2 )
interactive_reply_buttons = WhatsappSdk :: Resource :: Interactive . new (
type : "reply_button" ,
header : interactive_header ,
body : interactive_body ,
footer : interactive_footer ,
action : interactive_action
)
client . messages . send_interactive_reply_buttons (
sender_id : 12_345 , recipient_number : 1234567890 ,
interactive : interactive_reply_buttons
)대안, 당신은 다음과 같은 평범한 JSON을 통과 할 수 있습니다.
client . messages . send_interactive_reply_buttons (
sender_id : 12_345 , recipient_number : 1234567890
interactive_json : { ... }
) API가 오류를 반환하면 WhatsappSdk::Api::Responses::HttpResponseError 제기됩니다. 객체에는 클라우드 API가 반환 한 정보가 포함되어 있습니다. 잠재적 오류에 대한 자세한 내용은 공식 문서를 확인하십시오.
단일 파일로 API를 호출하려면 예제가있는 예제 파일을 방문하십시오.
메타 제어판에서 메시지 템플릿을 작성하고 승인하지 않고 직접 문자 메시지를 보내려고하면 다른 사람들과 채팅을 시작할 수 없습니다. 그러나 이전에 메시지를 받으면 메시지를 보낼 수 있습니다.
API 응답이 여전히 success 했지만 메시지가 전달되지 않은 경우 :
v.14 보다 큰 API 버전을 사용하는지 확인하십시오.참고 : 때로는 메시지가 지연됩니다. 메타 문서를 참조하십시오.
레포를 확인한 후 bin/setup 실행하여 종속성을 설치하십시오. 그런 다음 rake test 실행하여 테스트를 실행하십시오.
이 보석을 로컬 컴퓨터에 설치하려면 '번들 exec rake install'을 실행하십시오. 새 버전을 해제하려면 version.rb 에서 버전 번호를 업데이트 한 다음 버전에 대한 GIT bundle exec rake release 를 생성하고 GIT 커밋 및 태그를 누르고 .gem 파일을 rubygems.org로 누릅니다.
rake test 실행하십시오srb tcbundle exec rubocop 클라우드 API 버전을 업데이트하려면 lib/whatsapp_sdk/api/api_configuration.rb 의 버전을 업데이트하십시오. API UDPATES의 Cloud API ChangeLog를 확인하십시오.
https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk (https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk)에 버그 보고서 및 풀 요청은 환영합니다.이 프로젝트는 협업을위한 안전하고 환영하는 공간이되기위한 것이며 기고자는 기고자 계약 규범을 준수해야합니다.
보석에서 기능을 구현하려면 문제를 열면 최대한 빨리 살펴 보겠습니다.
기여하고 싶고 어디서부터 시작 해야할지 확신하지 못합니까? 트위터에서 나를 핑하면 도와 드리겠습니다!
기여 파일을 확인하십시오.
보석은 MIT 라이센스의 조건에 따라 오픈 소스로 제공됩니다.