good afternoon!
I’m having a problem with this code, it doesn’t resend messages as it should resend, I run it and nothing happens, it doesn’t give any error message. not even chatgpt is knowing how to help me. Does anyone know what to do?
bot functions: Literally takes the last 10 messages from a user and channel on telegram and sends it to you and to two different channels.
I just run it and nothing happens
I tried this code and some variants of it
from telegram import Bot
from telegram.ext import Updater
TOKEN = ''
CHANNEL_ID = ''
GROUP_IDS = ['']
USER_ID = '' # ID do usuário específic`your text`o
def reencaminhar_mensagens_antigas(bot: Bot):
channel = bot.get_chat(chat_id=CHANNEL_ID)
members = bot.get_chat_member(chat_id=CHANNEL_ID, user_id=USER_ID)
if members.can_send_messages:
messages = bot.get_chat_member_messages(chat_id=CHANNEL_ID, user_id=USER_ID, limit=10)
for message in messages:
if message.photo or (message.video and message.caption):
legenda = message.caption
# Reencaminhar a foto ou vídeo com legenda para o canal 1
if message.photo:
photo = message.photo[-1].file_id
bot.send_photo(chat_id=CHANNEL_ID, photo=photo, caption=legenda)
elif message.video:
video = message.video.file_id
bot.send_video(chat_id=CHANNEL_ID, video=video, caption=legenda)
# Reencaminhar a foto ou vídeo com legenda para os canais 2
for group_id in GROUP_IDS:
if message.photo:
photo = message.photo[-1].file_id
bot.send_photo(chat_id=group_id, photo=photo, caption=legenda)
elif message.video:
video = message.video.file_id
bot.send_video(chat_id=group_id, video=video, caption=legenda)
def start_bot():
bot = Bot(token=TOKEN)
reencaminhar_mensagens_antigas(bot=bot)
# Inicie o bot
updater = Updater(bot=bot, use_context=True)
updater.start_polling()
updater.idle()
start_bot()
2
Answers
According to the documentation of the class
ChatMember
which is returned bybot.get_chat_member_messages
, the class has many features deprecated recently and it doesn’t currently containcan_send_messages
among the methods.https://docs.python-telegram-bot.org/en/stable/telegram.chatmember.html
Based on that, I can assume, that you bot can’t pass the condition
if members.can_send_messages:
However, I would recommend testing that. It helps to add logging or printing to your code to see where the bot is getting stuck. This is especially important when you have so many conditions for something to happen, as your bot does.
There is no method called
Bot.get_chat_member_messages
and there never was – for the simple reason that the Telegram Bot API does not provide such a method.Disclaimer: I’m currently the maintainer of
python-telegram-bot
.