You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram机器人消息无法传递至Rasa服务器问题求助

问题诊断:Telegram消息无法传递到Rasa服务器的原因

1. Rasa启动函数完全阻塞了事件循环,机器人根本没启动

run_rasa_server是异步函数,但内部用了同步的time.sleep(10),直接卡住整个asyncio事件循环。再加上函数里的while True死循环,会一直运行到Rasa进程退出——这意味着main()里await run_rasa_server()之后的dp.start_polling()永远不会执行。机器人没启动,自然接收不到任何用户消息,更别说传给Rasa了。

2. 同步请求会拖慢/阻塞事件循环(机器人启动后仍需解决)

process_rasa_request用同步的requests.post发请求,在异步的handle_text回调里调用这种阻塞函数,会导致事件循环卡顿,甚至无法处理后续消息。

3. Rasa启动状态未验证,依赖固定等待时间不可靠

当前仅靠time.sleep(10)等Rasa启动,若Rasa加载模型耗时超过10秒,后续请求会直接失败,根本连不上服务器。


修复方案

1. 重构Rasa启动函数,让它后台运行不阻塞机器人

修改request_processing.py的run_rasa_server:

import subprocess
import asyncio
import requests

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

async def run_rasa_server():
    # 后台启动Rasa进程
    rasa_process = subprocess.Popen(RASA_SERVER_COMMAND, shell=True)
    
    # 异步轮询验证Rasa是否就绪,替代固定sleep
    while True:
        try:
            # 用to_thread避免同步请求阻塞事件循环
            response = await asyncio.to_thread(requests.get, "http://localhost:5005/health")
            if response.status_code == 200:
                print("Rasa服务器已就绪")
                break
        except requests.exceptions.ConnectionError:
            pass
        await asyncio.sleep(1)
    
    # 监控Rasa进程,意外退出时自动重启(可选)
    while True:
        if rasa_process.poll() is not None:
            print("Rasa进程意外退出,正在重启...")
            rasa_process = subprocess.Popen(RASA_SERVER_COMMAND, shell=True)
            # 重新等待就绪
            while True:
                try:
                    response = await asyncio.to_thread(requests.get, "http://localhost:5005/health")
                    if response.status_code == 200:
                        print("Rasa服务器已重启")
                        break
                except requests.exceptions.ConnectionError:
                    pass
                await asyncio.sleep(1)
        await asyncio.sleep(5)

2. 把Rasa请求改成异步,避免阻塞事件循环

修改request_processing.py的process_rasa_request:

import aiohttp

async def process_rasa_request(user_message):
    async with aiohttp.ClientSession() as session:
        async with session.post(
            "http://localhost:5005/webhooks/rest/webhook",
            json={"message": user_message}
        ) as response:
            if response.status == 200:
                rasa_response = await response.json()
                if rasa_response:
                    return "\n".join([r['text'] for r in rasa_response])
            return "抱歉,暂时无法获取回复。"

3. 修改main函数,并发运行Rasa和机器人

更新main.py的main函数:

async def main():
    # 同时启动Rasa服务器和机器人,互不阻塞
    await asyncio.gather(
        run_rasa_server(),
        dp.start_polling()
    )

4. 其他检查项

  • 确认models目录存在且有训练好的Rasa模型,启动Rasa时无报错
  • 检查5005端口是否被其他进程占用(Linux/macOS用lsof -i :5005,Windows用netstat -ano | findstr :5005)
  • 验证Telegram机器人TOKEN正确,且机器人已加入目标聊天

内容的提问来源于stack exchange,提问作者agfn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:45:12