0

I am developing a Telegram bot through which a user can send their question and a trained Rasa model will return the answer. I have encountered a problem that messages sent to the bot are not sent to the Rasa server in any way. What can be the problem?

**main.py**

import logging
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from request_processing import process_rasa_request, run_rasa_server
import asyncio



BOT_TOKEN = "TOKEN"


bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot)
dp.middleware.setup(LoggingMiddleware())

@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
    await message.reply("Hello! What can I do for you?")

@dp.message_handler(content_types=types.ContentType.TEXT)
async def handle_text(message: types.Message):
    user_message = message.text

    bot_response = process_rasa_request(user_message)
    await message.reply(bot_response)

async def main():
    await run_rasa_server()
    try:
        await dp.start_polling()
        print("BOT ACTIVATED")
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())
    print("SERVER AND BOT ACTIVATED")

request_processing.py

import subprocess
import time
import requests
import asyncio


RASA_SERVER_COMMAND = "rasa run -m models --enable-api --cors \"*\" --debug"

def process_rasa_request(user_message):
    response = requests.post("http://localhost:5005/webhooks/rest/webhook", json={"message": user_message}).json()
    if response:
        bot_response = "\n".join([r['text'] for r in response])
        return bot_response

async def run_rasa_server():
    rasa_process = subprocess.Popen(RASA_SERVER_COMMAND, shell=True)
    time.sleep(10)
    while True:
        if rasa_process.poll() is not None:
            break
        await asyncio.sleep(1)
    rasa_process.terminate()
    rasa_process.wait()
agfn
  • 29
  • 2

1 Answers1

0

make sure you have added the REST channel to your credentials.yml

post message with the following format:

{
  "sender": "test_user", 
  "message": "Hi there!"
}
lyirs
  • 1