A
A
Anarh1st2021-01-08 18:48:02
Bots
Anarh1st, 2021-01-08 18:48:02

Telegram Bot | Refining Python code?

Hello.
I decided to write a simple telegram bot using the TeleBot library, in which users will write text, and it will automatically send it to me. It came out like this:

import telebot

token = ''
bot = telebot.TeleBot(token)

my_id = '


@bot.message_handler(commands=['start'])
def welcome(message):
  bot.send_message(message.chat.id, 'Добро пожаловать ' + str(message.from_user.username) + '!')



@bot.message_handler(content_types=['text'])
def re_send_messages(message):
  bot.forward_message(my_id, message.chat.id, message.message_id)
  bot.send_message(message.chat.id, "Сообщение отправлено!")









bot.polling(none_stop=True)

The bot is the simplest, without any problems and chips. But a number of questions arose:
1. The bot does not accept photos (and text with photos, respectively, too). What needs to be added to the code so that it also starts the photo and not just the text?
2. How can I configure so that messages are sent to a number of users (in my case, two), and not just one?
If anyone can suggest something, please write. Thank you all in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d2ms2nk, 2021-01-09
@Anarh1st

Hey!
Try using the aiogram library.
Here is a simple example of what you need

import aiogram

token = 'your token'

# Вставь свой токен, запусти этот файл один раз и отправь боту сообщение /start
# Он в ответ отправит тебе твой chat id
# Заверши скрипт и затем вставь полученный chat id сюда.
my_chat_id = 12345

bot = aiogram.Bot(token = token)
dispatcher = aiogram.dispatcher.Dispatcher(bot  = bot)

@dispatcher.message_handler(commands = ['start'])
async def answer_start_message(message):
    await message.answer(f'Добро пожаловать! Ваш chat id : {message.chat.id}')

@dispatcher.message_handler(content_types = ['text'])
async def forward_message(message):
   # Тут список chat ids кому пересылать сообщения.
   # Они обязательно должны отправить боту команду /start.
   # Например chat_ids = [my_chat_id, 12345, 2345]
   chat_ids = [my_chat_id]
   for chat_id in chat_ids:
        await bot.send_message(chat_id, message.text)
   await message.answer("Ваше сообщение было отправлено успешно!")

@dispatcher.message_handler(content_types = ['photo'])
async def handle_photo(message):
    await message.photo[-1].download('test.jpg')
    await message.answer("Я получил и скачал твое фото!")


if __name__ ==  "__main__":
    aiogram.executor.start_polling(dispatcher, skip_updates = True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question