尝试使用 Python 在 Telegram 机器人上实现定期消息时出错

问题描述 投票:0回答:1

我正在尝试实现一个 Telegram Bot 功能,该功能允许我的帐户定期接收更新消息,但在执行该功能时收到此错误:

telebot.apihelper.ApiTelegramException:对 Telegram API 的请求 没有成功。错误代码:409。说明:冲突:已终止 通过其他 getUpdates 请求;确保只有一个机器人实例 跑步

这是我的代码。

import threading

import telebot
import Consts

from mypackage.ProcessManager import ProcessManager


bot = telebot.TeleBot(Consts.bot_token, threaded=True)
process_manager = ProcessManager()


@bot.message_handler(commands=['subscribe'])
def subscribe(message):
    chat_id = message.from_user.id
    if not process_manager.has_running_elements(chat_id):
        process_manager.start_process(chat_id=chat_id,
                                  target=send_message,
                                  arg2=chat_id, arg3="Prova")
        bot.reply_to(message, "Perfetto. Da ora riceverai aggiornamenti sulla tua assicurazione")
    else:
        bot.reply_to(message, "Sei già iscritto")


def send_message(chat_id, message: str):
    bot.send_message(chat_id=chat_id, text=message)


bot.infinity_polling()

ProcessManager 只是一个处理 multiprocessing.Process 对象的包装类。我还分享了 start_process 函数:

    def start_process(self, chat_id, target, **args):
        new_process = multiprocessing.Process(target=target,
                                          daemon=True,
                                          args=args)
        self.processes_dict[chat_id] = new_process
        new_process.start()

我做错了什么?

python bots telegram telegram-bot telebot
1个回答
0
投票

我使用 multithreading.Thread 而不是 multiprocessing.Process 解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.