Created
May 24, 2017 04:41
-
-
Save makmac213/78b0fc81ba6fc04e40d104acf948c0f1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import json | |
import requests | |
from bottle import debug, request, route, run | |
def build_service_url(service_url, cid): | |
"""build the service url""" | |
service_url = '{0}/v3/conversations/{1}/activities/'.format(service_url, cid) | |
return service_url | |
def build_text_message_payload(data, text): | |
"""Creates a text only message dict""" | |
payload = { | |
'type': 'message/text', | |
'from': { | |
'id': data['recipient']['id'], | |
'name': data['recipient']['name'], | |
}, | |
'recipient': { | |
'id': data['from']['id'], | |
'name': data['from']['name'], | |
}, | |
'text': text, | |
} | |
return payload | |
def send_to_conversation(service_url, payload): | |
headers = { | |
"Content-Type": "application/json", | |
} | |
payload = json.dumps(payload) | |
response = requests.post(service_url, data=payload, headers=headers) | |
return response | |
@route('/chat', method=["GET", "POST"]) | |
def chat(): | |
if request.method.lower() == "get": | |
return "Mabuhay" | |
else: | |
data = request.json | |
if data['type'] == 'conversationUpdate': | |
# calls when emulator tries to connect with the endpoint | |
# you may want to do something here | |
return '' | |
elif data['type'] == 'message': | |
# let's try to echo the message | |
payload = build_text_message_payload(data, data['text']) | |
# where we are going to send our request | |
service_url = build_service_url( | |
data['serviceUrl'], | |
data['conversation']['id'] | |
) | |
# let's send the message | |
response = send_to_conversation(service_url, payload) | |
# always return a response | |
return '' | |
# use below for | |
debug(True) | |
run(reloader=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment