-
Notifications
You must be signed in to change notification settings - Fork 1
/
messenger.py
executable file
·159 lines (135 loc) · 5.41 KB
/
messenger.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import requests
class Messenger:
def __init__(self, access_token):
self.token = access_token
self.url = "https://graph.facebook.com/v8.0/me"
def send_message(self, dest_id, message, prio=False):
"""
Cette fonction sert à envoyer une message texte
à l'utilisateur en vue de répondre à un message
"""
data_json = {
'recipient': {
"id": dest_id
},
'message': {
"text": message
}
}
if prio:
data_json["messaging_type"] = "MESSAGE_TAG"
data_json["tag"] = "ACCOUNT_UPDATE"
header = {'content-type': 'application/json; charset=utf-8'}
params = {"access_token": self.token}
return requests.post(self.url + '/messages', json=data_json, headers=header, params=params)
def send_action(self, dest_id, action):
"""
action doit etre un des suivants ['mark_seen', 'typing_on', 'typing_off']
"""
if action not in ['mark_seen', 'typing_on', 'typing_off']:
return None
data_json = {
'messaging_type': "RESPONSE",
'recipient': {
"id": dest_id
},
'sender_action': action
}
header = {'content-type': 'application/json; charset=utf-8'}
params = {"access_token": self.token}
return requests.post(self.url + '/messages', json=data_json, headers= header, params=params)
def send_quick_reply(self, dest_id, **kwargs):
if kwargs.get('MENU_PRINCIPALE'):
text = 'Quelle action voulez-vous faire?'
if kwargs.get('INSCRIPTION'):
quick_rep = [
{
"content_type": "text",
"title": "Nouveau Crush",
"payload": "_AJOUTER",
"image_url": "https://img.icons8.com/fluent/48/26e07f/heart-plus.png"
}
]
else:
quick_rep = [
{
"content_type": "text",
"title": "Inscription",
"payload": "_INSCRIPTION",
"image_url": "https://img.icons8.com/fluent/48/26e07f/inscription.png"
}
]
elif kwargs.get('MENU_INSCRIPTION'):
text = 'voulez-vous faire ...'
quick_rep = [
{
"content_type": "text",
"title": "Nouvelle inscription",
"payload": "_INSCRIPTION_NOUVEAU",
"image_url": "https://img.icons8.com/flat_round/64/26e07f/plus.png"
},
{
"content_type": "text",
"title": "Entrer Code",
"payload": "_INSCRIPTION_VALIDER",
"image_url": "https://img.icons8.com/wired/64/26e07f/check-all.png"
}
]
elif kwargs.get('LIEN_PROFIL'):
text = 'Avez-vous accepter notre demande?'
quick_rep = [
{
"content_type": "text",
"title": "Oui",
"payload": kwargs.get('LIEN_PROFIL'),
"image_url": "https://img.icons8.com/flat_round/64/26e07f/plus.png"
},
{
"content_type": "text",
"title": "Non",
"payload": "DEMANDE_REJETER",
"image_url": "https://img.icons8.com/wired/64/26e07f/check-all.png"
}
]
elif kwargs.get('TEXTE_PERSONNALISER') and kwargs.get('USERNAME') :
text = "Vous voulez un texte personnalisé."
quick_rep = [
{
"content_type": "text",
"title": "Non",
"payload": "_TEXTE_PERSONNALISER_OUI" + "***" + kwargs.get('TEXTE_PERSONNALISER') + "***" + kwargs.get('USERNAME'),
"image_url": "https://img.icons8.com/flat_round/64/26e07f/plus.png"
},
{
"content_type": "text",
"title": "Oui",
"payload": "_TEXTE_PERSONNALISER_OUI" + "***" + kwargs.get('TEXTE_PERSONNALISER')+"***"+ kwargs.get('USERNAME') ,
"image_url": "https://img.icons8.com/wired/64/26e07f/check-all.png"
}
]
else:
return
data_json = {
'messaging_type': "RESPONSE",
'recipient': {
"id": dest_id
},
'message': {
'text': text,
'quick_replies': quick_rep
}
}
header = {'content-type': 'application/json; charset=utf-8'}
params = {"access_token": self.token}
return requests.post(self.url + '/messages', json=data_json, headers=header, params=params)
def get_gender(self, user_id):
links = "https://graph.facebook.com/" + user_id + "?fields=gender&access_token=" + self.token
request = requests.get(links)
sex = request.json().get("gender")
if sex == "male":
gender = 1
elif sex == "female":
gender = 0
else:
gender = None
return gender