0

I have a Python bot for forwarding messages (formatted text, photos with captions, videos, stickers) to groups where the bot is added. However, I'm experiencing issues with forwarding photos, stickers, and similar content. How can I fix this?

import telebot
from telebot.types import Message

bot_token = '(I hide it)'
ALLOWED_USER_ID = 746422763

bot = telebot.TeleBot(bot_token)


def is_bot_in_group(chat_id):
    with open('groups.txt', 'r') as file:
        existing_chat_ids = [line.strip() for line in file.readlines()]
    return str(chat_id) in existing_chat_ids


def add_group(chat_id):
    with open('groups.txt', 'a') as file:
        file.write(str(chat_id) + '\n')


existing_chat_ids = []


@bot.message_handler(func=lambda message: message.new_chat_members and bot.get_me().id in [user.id for user in message.new_chat_members])
def on_bot_added_to_group(message):
    chat_id = message.chat.id
    if not is_bot_in_group(chat_id):
        add_group(chat_id)


@bot.message_handler(commands=['start'])
def handle_start_command(message):
    user_id = message.from_user.id
    response = f"Бот запущен.\n\nТвой Telegram айди: {user_id}"
    bot.send_message(message.chat.id, response)


@bot.message_handler(commands=['getgroups'])
def handle_getgroups_command(message):
    user_id = message.from_user.id
    response = "Список групп, в которых находится бот:\n"
    chat_ids = get_existing_chat_ids()
    for chat_id in chat_ids:
        response += f"- Группа с айди {chat_id}\n"
    bot.send_message(user_id, response)


@bot.message_handler(func=lambda message: message.chat.type == 'group')
def handle_group_message(message):
    chat_id = message.chat.id
    if not is_bot_in_group(chat_id):
        add_group(chat_id)
    else:
        forward_message_to_groups(chat_id, message)


@bot.message_handler(func=lambda message: message.chat.type == 'private')
def handle_private_message(message):
    chat_ids = get_existing_chat_ids()
    for group_id in chat_ids:
        forward_message(group_id, message)


def forward_message_to_groups(source_group_id, message):
    chat_ids = get_existing_chat_ids()
    for group_id in chat_ids:
        if group_id != str(source_group_id):
            forward_message(group_id, message)


def forward_message(chat_id, message):
    if message.text:
        bot.send_message(chat_id, message.text, parse_mode='HTML')
    elif message.photo:
        for photo in message.photo:
            bot.send_photo(chat_id, photo.file_id)
    elif message.caption:
        for photo in message.photo:
            bot.send_photo(chat_id, photo.file_id, caption=message.caption)
    elif message.document:
        bot.send_document(chat_id, message.document.file_id)
    elif message.audio:
        bot.send_audio(chat_id, message.audio.file_id)
    elif message.voice:
        bot.send_voice(chat_id, message.voice.file_id)
    elif message.video:
        bot.send_video(chat_id, message.video.file_id)
    elif message.video_note:
        bot.send_video_note(chat_id, message.video_note.file_id)
    elif message.sticker:
        bot.send_sticker(chat_id, message.sticker.file_id)


def get_existing_chat_ids():
    with open('groups.txt', 'r') as file:
        existing_chat_ids = [line.strip() for line in file.readlines()]
    return existing_chat_ids


def start_bot_in_groups():
    updates = bot.get_updates()
    for update in updates:
        chat_id = update.message.chat.id
        chat_type = update.message.chat.type
        if chat_type == 'group' and not is_bot_in_group(chat_id):
            add_group(chat_id)
    bot.infinity_polling()


bot.add_message_handler(on_bot_added_to_group)
bot.add_message_handler(handle_start_command)
bot.add_message_handler(handle_getgroups_command)
bot.add_message_handler(handle_group_message)
bot.add_message_handler(handle_private_message)
start_bot_in_groups()

bot.polling(none_stop=False, interval=0)

I've tried everything that I know and even asked chatgpt many times but no results

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
S1ngle
  • 13
  • 4
  • 2
    You left your **Private Bot Token** in your code. You should revoke it **ASAP** by talking to the [@BotFather](https://t.me/BotFather). People might abuse the token. – 0stone0 Jul 18 '23 at 20:16
  • Please explain what issues you are facing. Otherwise it's hard to help you. Please also read [this article](https://stackoverflow.com/help/how-to-ask) on how to ask good questions. – CallMeStag Jul 19 '23 at 19:24

0 Answers0