Search code examples
pythontelegramtelegram-botpython-telegram-bot

How to send file from server using telebot?


I made a bot that sends files from an external server with a URL.

I want the bot to send files directly from your server.

What am I doing wrong? Why didn't the open() command work?

import telebot

bot = telebot.TeleBot("Token")

@bot.message_handler(commands=['start','help'])
def send_start_message(message):
    bot.reply_to(message, "Hi.")

@bot.message_handler(func=lambda m: True)
def echo_all(message):
    print(message.text)
    duck = open('duck.png','r')
    bot.send_photo(message.chat.id, duck)
    bot.send_message(message.chat.id, "hi")

bot.polling()

Solution

  • You'll need to tell to use binary mode;

    duck = open('duck.png', 'rb')
    

    Read more about open()


    Minimal working example to send local image:

    import telebot
    
    bot = telebot.TeleBot("<YOUR-TOKEN-HERE>")
    cid = <YOUR-ID-HERE>
    
    img = open('image.jpg', 'rb')
    bot.send_photo(cid, img)