Last active
February 19, 2017 10:58
-
-
Save marianabocoi/210532e6fb2855780dbd8a706ae9fa19 to your computer and use it in GitHub Desktop.
Hello World - Chat Bot - Bonus 4
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
from flask import request, Response | |
from kik.messages import messages_from_json, TextMessage, SuggestedResponseKeyboard, TextResponse | |
from app import application, kik | |
""" | |
Detta ar ett enkelt exempel pa hur man kan fa Kik-boten att fraga en enkel fraga. | |
For Telegram sa kan du titta har: | |
https://core.telegram.org/bots#keyboards | |
This is a simple example of how to make the Kik bot ask a simple question. | |
For telegram you can follow this tutorial: | |
https://core.telegram.org/bots#keyboards | |
""" | |
@application.route('/') | |
def index(): | |
return "Hej" | |
@application.route('/incoming', methods=['POST']) | |
def incoming(): | |
if not kik.verify_signature(request.headers.get('X-Kik-Signature'), request.get_data()): | |
return Response(status=403) | |
messages = messages_from_json(request.json['messages']) | |
for message in messages: | |
if isinstance(message, TextMessage): | |
print "chat_ID", message.chat_id | |
print "from_user", message.from_user | |
print "body", message.body | |
new_message = TextMessage( | |
to=message.from_user, | |
chat_id=message.chat_id | |
) | |
if message.body == 'D:25 years': | |
new_message.body = "Correct!" | |
else: | |
if message.body == 'A:2 years' or \ | |
message.body == 'B:8 years' or \ | |
message.body == 'C:12 years': | |
new_message.body = "Try again!" | |
else: | |
user = kik.get_user(message.from_user) | |
new_message.body = "Hi {}, let's do a quick quiz! How old is python?".format(user.first_name) | |
new_message.keyboards.append( | |
SuggestedResponseKeyboard(to=message.from_user, | |
hidden=False, | |
responses=[TextResponse('A:2 years'), TextResponse('B:8 years'), | |
TextResponse('C:12 years'), TextResponse('D:25 years')] | |
) | |
) | |
kik.send_messages([new_message]) | |
return Response(status=200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment