From 184b96d6aeabacc8b24f32bc9b25e52b29654f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Wed, 17 May 2017 01:07:42 +0200 Subject: [PATCH] Initial Commit --- .gitignore | 13 ++++++ config.py.example | 6 +++ main.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 18 ++++++++ 4 files changed, 142 insertions(+) create mode 100644 .gitignore create mode 100644 config.py.example create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32e4914 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +bin/ +lib/ +lib64/ +lib64 +include/ +share/ +*.pyc +*.swp +pip-selfcheck.json +config.py +.idea/* +database.db +.ropeproject diff --git a/config.py.example b/config.py.example new file mode 100644 index 0000000..6c723ed --- /dev/null +++ b/config.py.example @@ -0,0 +1,6 @@ +# Simple configuration example + +config = { + "TOKEN": "Your telegram bot token goes here", + "database": "mongodb://localhost:27017" +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..b1ed85f --- /dev/null +++ b/main.py @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- +# ! /usr/bin/env python + +import logging +# import requests +import sys +import random + +from telegram.ext import (Updater, CommandHandler, MessageHandler, + Filters) + +from config import config + +from chatterbot import ChatBot + + +# Enable logging +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +logger = logging.getLogger(__name__) + + +chatbot = None + + +# Define a few command handlers. These usually take the two arguments bot and +# update. Error handlers also receive the raised TelegramError object in error. +def start(bot, update): + bot.sendMessage(update.message.chat_id, text='¡¿te rieh?! Que estoy muy loco...') + + +def help(bot, update): + bot.sendMessage(update.message.chat_id, text="""Te Rieh Bot v1.0: +No tiene sentido lo que digo... iré endrogao... + """) + + +def error(bot, update, error): + logger.warn('Update "%s" caused error "%s"' % (update, error)) + + +def randomchat(bot, update): + global chatbot + msg = update.message.text.lower() + user_name = update.message.from_user.username.lower() + reply = chatbot.get_response(msg) + + if random.randint(0, 100) < 5: + reply = user_name + ": " + reply + bot.sendMessage(update.message.chat_id, text=reply) + + +def initial_training(bot, update): + chatbot.train("chatterbot.corpus.spanish") + + bot.sendMessage(update.message.chat_id, text="Entrenamiento inicial completado.") + + +def main(): + global chatbot + token = config.get('TOKEN') + + if token is None: + print("Please, configure your token first") + sys.exit(1) + + updater = Updater(token) + dispatcher = updater.dispatcher + + # on different commands - answer in Telegram + dispatcher.add_handler(CommandHandler("start", start)) + dispatcher.add_handler(CommandHandler("help", help)) + dispatcher.add_handler(CommandHandler("init", initial_training)) + + # on noncommand i.e message - echo the message on Telegram + dispatcher.add_handler(MessageHandler([Filters.text], randomchat)) + + # log all errors + dispatcher.add_error_handler(error) + + # Start the chat bot system + chatbot = ChatBot('Te Rieh', + storage_adapter='chatterbot.storage.MongoDatabaseAdapter', + logic_adapters=[ + 'chatterbot.logic.BestMatch' + ], + filters=[ + 'chatterbot.filters.RepetitiveResponseFilter' + ], + database='terieh', + database_uri=config.get('database')) + + # Start the Bot + updater.start_polling() + + # Run the bot until the you presses Ctrl-C or the process receives SIGINT, + # SIGTERM or SIGABRT. This should be used most of the time, since + # start_polling() is non-blocking and will stop the bot gracefully. + updater.idle() + + +if __name__ == "__main__": + print("Starting TeRiehBot") + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0a44a09 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,18 @@ +appdirs==1.4.3 +beautifulsoup4==4.4.1 +certifi==2016.2.28 +ChatterBot==0.6.0 +chatterbot-corpus==0.0.1 +future==0.16.0 +jsondatabase==0.1.7 +nltk==3.2.2 +oauthlib==2.0.2 +packaging==16.8 +pkg-resources==0.0.0 +pymongo==3.4.0 +python-telegram-bot==5.0.0 +python-twitter==3.2.1 +requests==2.14.2 +requests-oauthlib==0.8.0 +six==1.10.0 +urllib3==1.16