Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
144 lines (104 loc) · 4.7 KB
/
Copy pathapp.py
File metadata and controls
144 lines (104 loc) · 4.7 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
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
import os
#import sys
import json
#from datetime import datetime
import time
import requests
from flask import Flask, request
#from setway import set_persistent_menu
from getway import get_send_products
from outway import send_message, send_quick_reply, get_user_name
from util import logar
app = Flask(__name__)
@app.route('/', methods=['GET'])
def verify():
# when the endpoint is registered as a webhook, it must echo back
# the 'hub.challenge' value it receives in the query arguments
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello earth, it's alive!!!!", 200
@app.route('/', methods=['POST'])
def webhook():
# endpoint for processing incoming messaging events
data = request.get_json()
logar("_DATA_")
logar(data) # you may not want to log every incoming message in production, but it's good for testing
# VTOR
# if data["object"] == "page":
# for entry in data["entry"]:
# for messaging_event in entry["messaging"]:
# if messaging_event["message"].get("is_echo"):
# logar("WE GOT ECHO")
#
#strdata = str(data)
#strdata= json.dumps(data)
#if "standby" in strdata:
#logar("WE HAVE STANBY")
#
#time.sleep(500)
# original
if data["object"] == "page":
for entry in data["entry"]:
if entry.get("standby"):
for standby_event in entry["standby"]: # a message was sent in standby
logar("STANBY EVENT")
elif entry.get("messaging"):
for messaging_event in entry["messaging"]:
sender_id_pass = messaging_event["sender"]["id"]
if messaging_event.get("message"): # someone sent us a message
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID
if messaging_event["message"].get("attachments"):
logar("Bot got a sticker!")
else:
message_text = messaging_event["message"]["text"] # the message's text
if message_text == "produtos":
get_send_products(0, sender_id)
elif message_text =="qr":
send_quick_reply("Reply to this", 0, sender_id)
elif message_text == "name":
nome = get_user_name(sender_id)
send_message("You are {usna}".format(usna=nome), sender_id)
else:
send_message("BOT :D", sender_id)
pass
if messaging_event.get("request_thread_control"): # ADMIN requested control
logar("ADMIN REQUEST CONTROL sender_id={sendr}".format(sendr=sender_id_pass))
pass_thread_control(sender_id_pass)
if messaging_event.get("request_thread_control"): # ADMIN control passed
logar("PASS CONTROL TO ADMIN")
if messaging_event.get("delivery"): # delivery confirmation
pass
if messaging_event.get("optin"): # optin confirmation
pass
if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message
pass
else:
logar("DEVDO - No Standby, neither Messaging")
else:
logar("DEVDO - no PAGE")
return "ok", 200
def pass_thread_control(chatter_id):
passData = json.dumps({
"recipient": {
"id": chatter_id
},
"target_app_id": 263902037430900,
"metadata": "-X pass_thread_control X-"
})
passParams = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
passHeaders = {
"Content-Type": "application/json"
}
hj = requests.post("https://graph.facebook.com/v2.6/me/pass_thread_control", params=passParams, headers=passHeaders, data=passData)
if hj.status_code != 200:
logar(hj.status_code)
logar(hj.text)
else:
logar("HANDOVER")
if __name__ == '__main__':
app.run(debug=True)