PyMessager عبارة عن واجهة برمجة تطبيقات Python لـ Facebook Messenger ومشروع نموذجي لتوضيح كيفية تطوير chatbot على Facebook Messenger.
توجد دروس كاملة حول تطوير Facebook Bot باستخدام 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 Access Token من وحدة تحكم المطور:
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 )
قبل أن يبدأ برنامج chatbot الخاص بك في تلقي الرسائل، يجب عليك الاشتراك في التطبيق في صفحة chatbot الخاصة بك. للاشتراك في الصفحة، فقط اتصل بها:
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.