-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from bottle import route, run, request, abort, static_file | ||
|
||
from fsm import TocMachine | ||
|
||
|
||
VERIFY_TOKEN = "richard" | ||
machine = TocMachine( | ||
states=[ | ||
'user', | ||
'state1', | ||
'state2' | ||
], | ||
transitions=[ | ||
{ | ||
'trigger': 'advance', | ||
'source': 'user', | ||
'dest': 'state1', | ||
'conditions': 'is_going_to_state1' | ||
}, | ||
{ | ||
'trigger': 'advance', | ||
'source': 'user', | ||
'dest': 'state2', | ||
'conditions': 'is_going_to_state2' | ||
}, | ||
{ | ||
'trigger': 'go_back', | ||
'source': [ | ||
'state1', | ||
'state2' | ||
], | ||
'dest': 'user' | ||
} | ||
], | ||
initial='user', | ||
auto_transitions=False, | ||
show_conditions=True, | ||
) | ||
|
||
|
||
@route("/webhook", method="GET") | ||
def setup_webhook(): | ||
mode = request.GET.get("hub.mode") | ||
token = request.GET.get("hub.verify_token") | ||
challenge = request.GET.get("hub.challenge") | ||
|
||
if mode == "subscribe" and token == VERIFY_TOKEN: | ||
print("WEBHOOK_VERIFIED") | ||
return challenge | ||
|
||
else: | ||
abort(403) | ||
|
||
|
||
@route("/webhook", method="POST") | ||
def webhook_handler(): | ||
body = request.json | ||
print('\nFSM STATE: ' + machine.state) | ||
print('REQUEST BODY: ') | ||
print(body) | ||
|
||
if body['object'] == "page": | ||
event = body['entry'][0]['messaging'][0] | ||
machine.advance(event) | ||
return 'OK' | ||
|
||
|
||
@route('/show-fsm', methods=['GET']) | ||
def show_fsm(): | ||
machine.get_graph().draw('fsm.png', prog='dot', format='png') | ||
return static_file('fsm.png', root='./', mimetype='image/png') | ||
|
||
|
||
if __name__ == "__main__": | ||
run(host="localhost", port=5000, debug=True, reloader=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from transitions.extensions import GraphMachine | ||
|
||
from utils import send_text_message | ||
|
||
|
||
class TocMachine(GraphMachine): | ||
def __init__(self, **machine_configs): | ||
self.machine = GraphMachine( | ||
model=self, | ||
**machine_configs | ||
) | ||
|
||
def is_going_to_state1(self, event): | ||
if event.get("message"): | ||
text = event['message']['text'] | ||
return text.lower() == 'go to state1' | ||
return False | ||
|
||
def is_going_to_state2(self, event): | ||
if event.get("message"): | ||
text = event['message']['text'] | ||
return text.lower() == 'go to state2' | ||
return False | ||
|
||
def on_enter_state1(self, event): | ||
print("I'm entering state1") | ||
|
||
sender_id = event['sender']['id'] | ||
responese = send_text_message(sender_id, "I'm entering state1") | ||
self.go_back() | ||
|
||
def on_exit_state1(self): | ||
print('Leaving state1') | ||
|
||
def on_enter_state2(self, event): | ||
print("I'm entering state2") | ||
|
||
sender_id = event['sender']['id'] | ||
send_text_message(sender_id, "I'm entering state2") | ||
self.go_back() | ||
|
||
def on_exit_state2(self): | ||
print('Leaving state2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from bottle import route, run | ||
|
||
@route('/message') | ||
def hello(): | ||
return "Hello World!" | ||
|
||
run(host='localhost', port=8080, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
a = os.environ.get("ACCESS_TOKEN") | ||
print(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import requests | ||
import os | ||
|
||
|
||
GRAPH_URL = "https://graph.facebook.com/v2.6" | ||
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN") | ||
|
||
|
||
def send_text_message(id, text): | ||
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN) | ||
payload = { | ||
"recipient": {"id": id}, | ||
"message": {"text": text} | ||
} | ||
response = requests.post(url, json=payload) | ||
|
||
if response.status_code != 200: | ||
print("Unable to send message: " + response.text) | ||
return response | ||
|
||
|
||
""" | ||
def send_image_url(id, img_url): | ||
pass | ||
def send_button_message(id, text, buttons): | ||
pass | ||
""" |