Spaces:
Sleeping
Sleeping
imseldrith
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,42 @@
|
|
1 |
from flask import Flask, request, jsonify
|
|
|
2 |
|
3 |
app = Flask(__name__)
|
4 |
|
|
|
|
|
5 |
@app.route('/', methods=['GET'])
|
6 |
def home():
|
7 |
return "Welcome to the chatbot!"
|
8 |
|
9 |
-
@app.route('/webhook', methods=['POST'])
|
10 |
def webhook():
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
|
24 |
def send_message(recipient_id, message_text):
|
25 |
-
access_token = '
|
26 |
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={access_token}'
|
27 |
headers = {'Content-Type': 'application/json'}
|
28 |
payload = {
|
@@ -32,4 +46,4 @@ def send_message(recipient_id, message_text):
|
|
32 |
requests.post(url, headers=headers, json=payload)
|
33 |
|
34 |
if __name__ == '__main__':
|
35 |
-
app.run(port=7860, debug=True)
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
import requests
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
+
VERIFY_TOKEN = 'imseldrith' # Replace with your verification token
|
7 |
+
|
8 |
@app.route('/', methods=['GET'])
|
9 |
def home():
|
10 |
return "Welcome to the chatbot!"
|
11 |
|
12 |
+
@app.route('/webhook', methods=['GET', 'POST'])
|
13 |
def webhook():
|
14 |
+
if request.method == 'GET':
|
15 |
+
# Webhook verification
|
16 |
+
verify_token = request.args.get('hub.verify_token')
|
17 |
+
challenge = request.args.get('hub.challenge')
|
18 |
+
|
19 |
+
if verify_token == VERIFY_TOKEN:
|
20 |
+
return challenge
|
21 |
+
else:
|
22 |
+
return 'Error, wrong validation token'
|
23 |
+
|
24 |
+
elif request.method == 'POST':
|
25 |
+
data = request.get_json()
|
26 |
+
messaging_event = data['entry'][0]['messaging'][0]
|
27 |
|
28 |
+
if 'message' in messaging_event:
|
29 |
+
sender_id = messaging_event['sender']['id']
|
30 |
+
message_text = messaging_event['message'].get('text', '')
|
31 |
|
32 |
+
if message_text.lower() == 'hi':
|
33 |
+
response_text = 'hello'
|
34 |
+
send_message(sender_id, response_text)
|
35 |
|
36 |
+
return jsonify({'status': 'ok'})
|
37 |
|
38 |
def send_message(recipient_id, message_text):
|
39 |
+
access_token = 'YOUR_PAGE_ACCESS_TOKEN' # Replace with your Facebook Page Access Token
|
40 |
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={access_token}'
|
41 |
headers = {'Content-Type': 'application/json'}
|
42 |
payload = {
|
|
|
46 |
requests.post(url, headers=headers, json=payload)
|
47 |
|
48 |
if __name__ == '__main__':
|
49 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|