PyMessager เป็น Python API สำหรับ Facebook Messenger และโปรเจ็กต์ตัวอย่างเพื่อสาธิตวิธีพัฒนาแชทบอทบน Facebook Messenger
บทแนะนำสอนการใช้งานฉบับสมบูรณ์อยู่ในการพัฒนาบอท Facebook โดยใช้ Python และ Chatbot: ตั้งแต่ 0 ถึง 1 ซึ่งคุณสามารถค้นหาข้อมูลโดยละเอียดเพิ่มเติมเพื่อตั้งค่าและพัฒนาได้
หากต้องการติดตั้ง PyMessager เพียงเรียกใช้:
$ pip install pymessager
หรือติดตั้งจากพื้นที่เก็บข้อมูล:
$ git clone [email protected]:enginebai/PyMessager.git
$ cd PyMessager
$ pip install -r requirements.txt
from pymessager . message import Messager , ... # something else you need
คุณสามารถเริ่มต้นไคลเอนต์ข้อความผ่านโทเค็นการเข้าถึง Facebook จากคอนโซลนักพัฒนา:
from pymessager . message import Messager
client = Messager ( config . facebook_access_token )
รหัสต่อไปนี้ใช้ในการสร้างตัวรับข้อความ มีสามขั้นตอนหลักในการเตรียมบอทของคุณ:
@ app . route ( API_ROOT + FB_WEBHOOK , methods = [ "GET" ])
def fb_webhook ():
verification_code = 'I_AM_VERIFICIATION_CODE'
verify_token = request . args . get ( 'hub.verify_token' )
if verification_code == verify_token :
return request . args . get ( 'hub.challenge' )
@ app . route ( API_ROOT + FB_WEBHOOK , methods = [ 'POST' ])
def fb_receive_message ():
message_entries = json . loads ( request . data . decode ( 'utf8' ))[ 'entry' ]
for entry in message_entries :
for message in entry [ 'messaging' ]:
if message . get ( 'message' ):
print ( "{sender[id]} says {message[text]}" . format ( ** message ))
return "Hi"
if __name__ == '__main__' :
context = ( 'ssl/fullchain.pem' , 'ssl/privkey.pem' )
app . run ( host = '0.0.0.0' , debug = True , ssl_context = context )
ข้อความมีหลายประเภท: text
image
quick replies
button template
หรือ generic template
API มีคลาสที่แตกต่างกันเพื่อสร้างเทมเพลตข้อความ
ส่งข้อความหรือรูปภาพธรรมดาไปยังผู้รับ เพียงตรวจสอบให้แน่ใจว่า URL รูปภาพเป็นลิงก์ที่ถูกต้อง
client . send_text ( user_id , "Hello, I'm enginebai." )
client . send_image ( user_id , "http://image-url.jpg" )
คลาส QuickReply(title, payload, image_url, content_type)
กำหนดปุ่มปัจจุบันให้กับผู้ใช้เพื่อตอบสนองต่อข้อความ
พารามิเตอร์ | คำอธิบาย | ที่จำเป็น |
---|---|---|
title | ชื่อปุ่ม | ย |
payload | สตริงเพย์โหลดการคลิก | ย |
image_url | URL รูปภาพของไอคอน | เอ็น |
content_type | TEXT หรือ LOCATION | ย |
client . send_quick_replies ( user_id , "Help" , [
QuickReply ( "Projects" , Intent . PROJECT ),
QuickReply ( "Blog" , Intent . BLOG ),
QuickReply ( "Contact Me" , Intent . CONTACT_ME )
])
คลาส ActionButton(button_type, title, url, payload)
กำหนดเทมเพลตปุ่มซึ่งประกอบด้วยข้อความและปุ่มที่แนบมาเพื่อขออินพุตจากผู้ใช้
พารามิเตอร์ | คำอธิบาย | ที่จำเป็น |
---|---|---|
button_type | WEB_URL หรือ POSTBACK | ย |
title | ชื่อปุ่ม | ย |
url | ลิงค์ | เฉพาะในกรณีที่ button_type เป็น url |
payload | สตริงเพย์โหลดการคลิก | เฉพาะในกรณีที่ button_type เป็น POSTBACK |
client . send_buttons ( user_id , "You can find me with below" , [
ActionButton ( ButtonType . WEB_URL , "Blog" , "http://blog.enginebai.com" ),
ActionButton ( ButtonType . POSTBACK , "Email" , Intent . EMAIL )
])
คลาส GenericElement(title, subtitle, image_url, buttons)
กำหนดภาพหมุนแบบเลื่อนแนวนอนของรายการต่างๆ โดยแต่ละรายการประกอบด้วยไฟล์แนบรูปภาพ คำอธิบายสั้นๆ และปุ่มเพื่อขออินพุตจากผู้ใช้
พารามิเตอร์ | คำอธิบาย | ที่จำเป็น |
---|---|---|
title_text | ชื่อหลักของข้อความ | ย |
subtitle_text | คำบรรยายข้อความ ปล่อยว่างไว้หากคุณไม่ต้องการ | เอ็น |
button_list | รายการ ActionButton | ย |
project_list = []
for project_id , project in projects . items ():
project_list . append ( GenericElement (
project [ "title" ],
project [ "description" ],
config . api_root + project [ "image_url" ], [
ActionButton ( ButtonType . POSTBACK ,
self . _get_string ( "button_more" ),
# Payload use Intent for the beginning
payload = Intent . PROJECTS . name + project_id )
]))
client . send_generic ( user_id , project_list )
ก่อนที่แชทบอทของคุณจะเริ่มรับข้อความ คุณต้องสมัครสมาชิกแอปพลิเคชันในหน้าแชทบอทของคุณก่อน หากต้องการสมัครสมาชิกเพจ เพียงโทรไปที่:
client . subscribe_to_page ()
ข้อความทักทายจะแสดงในครั้งแรกที่คุณเปิดแชทบอทนี้บนมือถือเท่านั้น เพย์โหลดเป็นตัวกระตุ้นเมื่อผู้ใช้คลิกปุ่ม "เริ่มต้น"
client . set_greeting_text ( "Hi, this is Engine Bai. Nice to meet you!" )
client . set_get_started_button_payload ( "HELP" ) # Specify a payload string.
อย่าลังเลที่จะส่งรายงานข้อผิดพลาดหรือคำขอคุณสมบัติ และอย่าลืมอ่านแนวทางการสนับสนุนก่อนที่จะเปิดปัญหาใดๆ
feature
/ bug
)อ่านเพิ่มเติมเกี่ยวกับการบริจาค
The MIT License (MIT)
Copyright © 2017 Engine Bai.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.