Last active
April 12, 2016 07:32
-
-
Save boxdot/5903b3d972332ab56bcc023d367653ee to your computer and use it in GitHub Desktop.
A very simple and sometimes mean bot for telegram based on chatterbot.
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
import requests | |
from flask import Flask, request | |
from chatterbot import ChatBot | |
app = Flask(__name__) | |
TOKEN = "PUT YOUR TOKEN STRING HERE" | |
# cites = open('zitate.txt').read().split('\n') | |
wittgenstein = ChatBot('Wittgenstein', database="wittgenstein.db") | |
@app.route("/") | |
def hello(): | |
return "Hello from Wittgenstein!" | |
@app.route("/", methods=['POST']) | |
def webhook(): | |
print request.json | |
msg = request.json.get('message') | |
if msg and msg.get('text'): | |
text = msg['text'] | |
user = msg.get('from') and msg.get('from').get('first_name') | |
response = None | |
if text.lower().find("wittgenstein") != -1 and \ | |
text.lower().find("hallo") != -1: | |
response = "Hello from the Underworld, you sucker!" | |
elif user == 'Sarah': | |
response = 'Isso.' | |
else: | |
if text.lower().find("wittgenstein") != -1 or \ | |
(msg.get("chat") and msg['chat'].get('username')): | |
response = wittgenstein.get_response( | |
text.replace( | |
'wittgenstein', '').replace( | |
'Wittgenstein', ''), user_name=user) | |
else: | |
wittgenstein.get_response(text, user_name=user) | |
if response: | |
print "Sending response" | |
message = { | |
'chat_id': request.json['message']['chat']['id'], | |
'reply_to_message': request.json['message']['message_id'], | |
'text': response | |
} | |
requests.post( | |
'https://api.telegram.org/bot' + TOKEN + '/sendMessage', | |
data=message) | |
return 'ok' | |
if __name__ == "__main__": | |
app.debug = True | |
app.run(port=9768) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment