5

I want the code to send a photo once the user hits the /start command and it doesn't work can anyone help me. thank you

import emoji, telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup

token = "-----------"
bot = telegram.Bot(token=token)

pic = "./photo.png"
try:
    chat_id = bot.get_updates()[-1].message.chat_id
except IndexError:
    chat_id = 0

#general functions
def start(bot, update):
    bot.send_photo(chat_id, pic)
    update.message.reply_text(text="helo",
                              reply_markup=menu_keyboard())
roy
  • 53
  • 1
  • 1
  • 4
  • This library already have full document – Sean Wei Jul 07 '18 at 12:02
  • I lately posted the wrong answer (sending) on a question about reading: https://stackoverflow.com/questions/51113062/how-to-receive-images-from-telegram-bot/51113663#51113663 – hootnot Jul 07 '18 at 12:29

2 Answers2

4

Your problem is that the second argument of the send_photo(chat_id, pic) function should be a file id and not the name of the file as you are doing (cf the doc).

You could simply change your code:

 bot.send_photo(chat_id, pic)

by this:

bot.send_photo(chat_id, open(pic,'rb'))
Simon C.
  • 1,058
  • 15
  • 33
0

The bot.send_photo method accepts as photo parameter either a file object or an ID of the file on the Telegram's server, so you should pass something like open(pic, "rb") instead of simply pic.

Once the photo is sent for the first time, you can obtain the Telegram's file id of it from the response. Passing Telegram's file id instead instead of the file object is faster, because you just point Telegram to a file it already has and not reading and sending the same file over and over again.

You can lookup the documentation here

monomonedula
  • 606
  • 4
  • 17