D
D
Daniil Secrets2021-11-27 12:28:28
Python
Daniil Secrets, 2021-11-27 12:28:28

Forwarding all messages from a bot to a Python, Telegram, Aiogram channel?

Good afternoon! I have a telegram bot embedded in python with the AIogram library. When you write a command to it, it answers and this answer goes to my channel. Who can help make sure that only my messages that I wrote go to the telegram channel (For example, I write 'Any word' to him and it goes to my telegram channel) Thank you in advance for your attention! My code:

"""
Этот бот принимает сообщение и пересылает в телеграм канал
"""

import logging

from aiogram import Bot, Dispatcher, executor, types

API_TOKEN = 'Token_bot'
CHANNEL_ID = 'Token_canal'
PHOTOS_ID = []



# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)


@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
    """
    `/start` или `/help`
    """
    keyboard_markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    btns_text = ('Да!', 'Нет!')
    keyboard_markup.row(*(types.KeyboardButton(text) for text in btns_text))

    await message.reply("Привет!\nЯ бот пересылки в канал!")
    await message.answer("Ответь мне на вопрос, готов ли ты писать ботов? А? "
                       + '{user}!'.format(user=message.from_user.full_name), reply_markup=keyboard_markup)

@dp.message_handler(content_types=["photo"])
async def send_photo(message: types.Message):
    """Переслать фото в канал"""
    # photo_id = message.photo[-1].file_id

    # await bot.send_photo(message.from_user.id, photo_id, caption="Отправленное фото")
    # await bot.send_photo(CHANNEL_ID, photo_id, caption="Отправленное фото")


    # Группировка фото - в процессе.
    media = types.MediaGroup()

    PHOTOS_ID.append(message.photo[-1].file_id)
    print(PHOTOS_ID)

    for photo_id in PHOTOS_ID:
        media.attach_photo(photo_id)

    await message.answer_media_group(media=media)

@dp.message_handler()
async def all_msg_handler(message: types.Message):
    button_text = message.text
    logger.debug('The answer is %r', button_text)  # print the text we've got

    if button_text == 'Да!':
        reply_text = "Да, готов писать ботов!!!"
    elif button_text == 'Нет!':
        reply_text = "О неет, что это ???"
    else:
        reply_text = "Сохраняйте спокойствие ... Все в порядке"

    await message.reply(reply_text, reply_markup=types.ReplyKeyboardRemove())
    await bot.send_message(CHANNEL_ID, reply_text)

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question