D
D
Danil Kazakov2020-06-17 10:18:58
Python
Danil Kazakov, 2020-06-17 10:18:58

Reply sending files to Telebot?

I am writing a Telegram bot in Python using the PyTelegramBotApi library, or Telebot. I need to implement an echo bot, but only with files or photos, for example:
/start command, upload a photo, the bot sends it back to you. Is it possible to do so? I didn't find it in the documentation. Here's everything there is so far:

import telebot

bot = telebot.TeleBot("...")

@bot.message_handler(commands=["start"])
def send(message):
  bot.send_message(message.chat.id, "Текст, документ или фото для отправки.")

  @bot.message_handler(content_types=["text", "document", "photo"])
  def send_to_channel(message):
    try:
      bot.send_message(message.chat.id, message.text)
    except:
      try:
        bot.send_photo(message.cht.id, #отправка фото)
      except:
        bot.send_document(message.chat.id, #отправка документа)
  

bot.polling(none_stop=True)


Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
Ulukman Amangeldiev, 2020-06-17
@Atageldiev

I'll leave a link to stackoverflow. It tells how to save the received photo
https://ru.stackoverflow.com/questions/615733/Save...
And in order to send a photo, you must first open it, and then send it via bot.send_photo():

with open("путь к файлу", "rb") as photo:
    bot.send_photo(message.chat.id, photo)

Example:
with open("addQuestion.png", "rb") as photo:
    bot.send_photo(message.chat.id, photo)

I don't understand why this might be necessary, but I hope it helps

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question