Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuMon committed Nov 29, 2018
1 parent 397fb9d commit f54b178
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
75 changes: 75 additions & 0 deletions app.py
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)
43 changes: 43 additions & 0 deletions fsm.py
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')
7 changes: 7 additions & 0 deletions hello.py
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)
Binary file added ngrok
Binary file not shown.
3 changes: 3 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os
a = os.environ.get("ACCESS_TOKEN")
print(a)
28 changes: 28 additions & 0 deletions utils.py
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
"""

0 comments on commit f54b178

Please sign in to comment.