Import chatterbot bot to Telegram

Asked

Viewed 302 times

3

I don’t know if that kind of question is welcome here, but come on:

I’m developing a chatbot on python and chose the library chatterbot because of the processamento de linguagem natural, by the ability to use more than one corpus and mainly by the ability of the bot to learn to converse with the user himself.

The bot made with the chatterbot wheel by CMD, but I’d like to put it on telegram.

I implemented the answer code below and as much as my chatbot is working on the computer, on Telegram it does not respond to messages. Could someone help me?

import os
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from telegram.ext import Updater, MessageHandler, Filters

# Define a função que responderá no Telegram
def response(update, context):
    bot_response = bot.get_response(update.message.text)
    update.message.reply_text(bot_response)

def main():
    # Inicia o bot do Telegram
    updater = Updater("o meu token ta aqui, eu juro")

    # Pega o disparador do Telegram
    dp = updater.dispatcher

    # Adiciona a função ao bot
    dp.add_handler(MessageHandler(Filters.text, response))

if __name__ == '__main__':
    main()

bot = ChatBot("Futaba")

trainer = ListTrainer(bot)

for arquivos in os.listdir('arquivos'):
    chats = open('arquivos/' + arquivos, 'r', encoding="utf8").readlines()
    trainer.train(chats)

print("Hey, meu nome é Futaba e você pode se sentir confortável para conversar comigo mesmo que conversar não seja lá a coisa mais confortável pra você.")
response = bot.get_response("Hey!")
print(response)

while True:
    resq = input('Você: ')
    resp = bot.get_response(resq)
    if float(response.confidence) > 0.5:
        print('Futaba: ' + str(resp))
    else:
        print('Como você tem se sentido?')
  • 2

    Of course I do. Chatterbot does not define how you will get the message to be handled, so you just need to integrate with the Telegram API and get the messages from it (and send the reply from it as well).

  • Aaaah, I got it! So I can continue using chatterbot to develop the bot, I don’t need to use the python-Telegram-bot framework, just use the same Telegram API directly.

  • But it’s obvious that he won’t answer Telegram, you didn’t even put the bot to read his messages. Have you read the documentation cited? Did you see that you need to call for other methods than what I quoted in the answer? By the way, with your edition you invalidated my reply, so much so that you removed the acceptance, this should be avoided here on the site and the editing should be reversed. If there is another problem that has not been addressed here you should open a new question.

  • Okay, man, it was just a question but I already solved it.

1 answer

3


Chatterbot doesn’t define how you will get the phrase to be processed, so you just need to integrate with the Telegram API to pick up the phrase you typed and answer for it as well.

For example:

from chatterbot import ChatBot
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# Cria uma instância do ChatBot
bot = ChatBot(
    ...
)

# Define a função que responderá no Telegram
def response(update, context):
    bot_response = bot.get_response(update.message.text)
    update.message.reply_text(bot_response)


def main():
    # Inicia o bot do Telegram
    updater = Updater("TOKEN", use_context=True)

    # Pega o disparador do Telegram
    dp = updater.dispatcher

    # Adiciona a função ao bot
    dp.add_handler(MessageHandler(Filters.text, response))

if __name__ == '__main__':
    main()

Codes (incomplete) based entirely on the examples of the Chatterbot and Telegram

So when the Telegram bot receives a message, the function response will be executed by passing the received phrase to the Chatterbot bot and sending back the received reply.

  • Hey, I implemented the same code by adding my chatbot and including my token, but my Telegram chatbot doesn’t respond to messages. No error message, no idea what it could be.

  • I’ll add the question the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.