如何配置 Telegram 机器人来监听和响应 Telegram 频道中的消息?

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

我使用IDBot获取了Telegram频道ID,简化了识别过程。此外,我已将频道内的机器人提升为管理员状态,授予其完全管理权限

import telebot
from helper import TextHelper
import os
import contants
import time

bot = telebot.TeleBot(contants.BOT_ACCESS_TOKEN, parse_mode=None)
obj = TextHelper()

@bot.message_handler(commands=['test'])
def send_ans(message):
    try:
        text = message.text.split(' ')[1]
        stat, filex, captionx = obj.getText(text)
        if stat == True:
            bot.send_document(message.chat.id, open(filex, 'rb'), caption=captionx, reply_to_message_id=message.message_id)
            os.remove(filex)
        else:
            bot.reply_to(message, captionx)
    except Exception as e:
        bot.reply_to(message, str(e))

def polling_wrapper():
    while True:
        try:
            bot.remove_webhook()
            bot.infinity_polling()
        except Exception as e:
            print(f"An error occurred: {e}")
            time.sleep(5)

try:
    polling_wrapper()
except KeyboardInterrupt:
    pass
except Exception as e:
    print(f"An unexpected error occurred: {e}")
python telegram telegram-bot
1个回答
0
投票

我们可以利用

@bot.channel_post_handler()
它将监听频道消息并响应

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