V
V
Vladimir Z.2021-02-08 16:03:30
Python
Vladimir Z., 2021-02-08 16:03:30

How to send a photo with pyTelegramBotAPI bot?

Hello!

I understand Python and in parallel for practice I sculpt a bot for Telegram using pyTelegramBotAPI.
The idea is as follows: the bot polls the user with a bunch of questions, asks for a picture, then generates them all as a whole, sends them to me for approval and, if approved, sends them to the channel.

There are no problems with text messages, but there is no way to do this with an image. Below is a piece of code:

def photo_found(message):
    photo_dog = message.photo[-1].file_id
    bot.send_photo(message.from_user.id, photo_dog, caption='Вот ваша картинка')
    bot.send_message(chat_id=[Тут мой ID в Telegram],  text='Пользователь хочет разместить следующее объявление: ')


But as soon as I try to send myself a picture with:

bot.send_photo(chat_id=[Тут мой ID в Telegram], photo_dog)


So I get the error "Positional argument after keyword argument". I've looked all over the internet and can't find a solution.

I tried to do it through bot.forward_message, but in this case the caption that I need is lost and only the picture is forwarded. Where to dig? Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-02-08
@z_v_r_k

Positional argument after keyword argument

The translation clearly says what the problem is.
In python, the function has positional parameters (args) and named parameters (keyword args) (kwargs)
The error says that the positional argument follows the named ones, which is not correct.
Positional arguments should always come first (if there are any, of course) - then already named arguments.
So do either passing both arguments without keys
bot.send_photo([Тут мой ID в Telegram], photo_dog)
or like this passing both arguments with keys
bot.send_photo(chat_id=[Тут мой ID в Telegram], photo=photo_dog)

or so, passing first arguments without keys, then with keys
bot.send_photo([Тут мой ID в Telegram], photo=photo_dog)

https://habr.com/ru/company/ruvds/blog/482464/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question