-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessaging_API.py
More file actions
113 lines (92 loc) · 3.78 KB
/
messaging_API.py
File metadata and controls
113 lines (92 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
'''
messaging_API.py
Author: Liwei Jiang
Date: 24/07/2018
Usage:
Internal messaging API sending data to Facebook Messenger.
'''
import os
import json
import requests
import random
from time import strftime, localtime, sleep
from flask_mysqldb import MySQL
import template
from utils import pretty_print, log
import database as db
from constants import CHATBOT_ID
PRAMS = {"access_token": os.environ["PAGE_ACCESS_TOKEN"]}
HEADERS = {"Content-Type": "application/json"}
DELAY_TIME = 0.1 # time to sleep in send_typing_action
def send_data(data, data_type="messages"):
'''
Base function that sends data to Facebook API.
All other messaging_API functions call this one.
'''
r = requests.post("https://graph.facebook.com/v2.6/me/" +
data_type, params=PRAMS, headers=HEADERS, data=data)
if r.status_code != 200:
log(r.status_code)
log(r.text)
def send_typing_action(recipient_id):
'''
This function delays the reply and sends a typing action (...) to the specified recipient.
'''
sleep(DELAY_TIME)
data = template.create_typing_action_template_json(recipient_id)
send_data(data)
def send_image(mysql, recipient_id, image_data):
'''
This function sends an image to the specified recipient,
and saves the image_url to the conversation database.
'''
send_typing_action(recipient_id)
data = template.create_image_template_json(recipient_id, image_data)
send_data(data)
dialogue = image_data["image_url"][0]
timestamp = strftime("%Y-%m-%d %H:%M:%S", localtime())
db.insert_conversation(mysql, CHATBOT_ID, recipient_id,
dialogue, "BOT: image", timestamp)
def send_message(mysql, recipient_id, template_conversation, message_data):
'''
This function sends a text message, with a short delay and typing action at the beginning, to the specified recipient,
and saves the message text to the conversation database
'''
send_typing_action(recipient_id)
data = template.create_message_template_json(
recipient_id, template_conversation, message_data)
send_data(data)
dialogue = json.loads(data)["message"]["text"]
timestamp = strftime("%Y-%m-%d %H:%M:%S", localtime())
db.insert_conversation(mysql, CHATBOT_ID, recipient_id,
dialogue, "BOT: message", timestamp)
def send_quick_reply(mysql, recipient_id, template_conversation, quick_reply_data, message_data=""):
'''
This function sends a set of quick reply buttons along with a piece of message text to the specified recipient.
Args:
message_data: if "" then sends the message text included in quick_reply_data; if not "" then use message_data as the message text.
Returns:
uid: timestamp and uid so that we can log the information in the [user_history] dataset when the bot sends a question
'''
send_typing_action(recipient_id)
data = template.create_quick_reply_template_json(
recipient_id, template_conversation, quick_reply_data, message_data)
send_data(data)
dialogue = json.loads(data)["message"]["text"]
timestamp = strftime("%Y-%m-%d %H:%M:%S", localtime())
uid = db.insert_conversation(
mysql, CHATBOT_ID, recipient_id, dialogue, "BOT: quick reply", timestamp)
return uid
def send_persistent_menu(persistent_menu_data):
'''
This function sets up the persistence menu.
'''
data = template.create_persistent_menu_json(persistent_menu_data)
send_data(data, "messenger_profile")
def send_get_started(get_started_data):
'''
This function initializes the payloads.
'''
data = template.create_get_started_json(get_started_data)
send_data(data, "messenger_profile")