Initial Commit

This commit is contained in:
José Carlos Cuevas 2017-05-17 01:07:42 +02:00
commit 184b96d6ae
4 changed files with 142 additions and 0 deletions

13
.gitignore vendored Normal file
View file

@ -0,0 +1,13 @@
bin/
lib/
lib64/
lib64
include/
share/
*.pyc
*.swp
pip-selfcheck.json
config.py
.idea/*
database.db
.ropeproject

6
config.py.example Normal file
View file

@ -0,0 +1,6 @@
# Simple configuration example
config = {
"TOKEN": "Your telegram bot token goes here",
"database": "mongodb://localhost:27017"
}

105
main.py Normal file
View file

@ -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()

18
requirements.txt Normal file
View file

@ -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