pyTelegramBotAPI message handlers can't see photo

809 Views Asked by At

I need my telegram bot to forward messages from the private chat to the customer care staff group.

I run this code:

@bot.message_handler(func=lambda message: message.chat.type=='private') 
def forwarder(message): 
 bot.forward_message(group, message.chat.id, message.id)
 bot.send_message(group, '#id'+str(message.chat.id))

It works smoothly with text messages, but does nothing with photos.

Even if I remove all previous message handlers, so there is no conflict with them to handle photos, it still keeps doing nothing.

If I use the get.updates() method I can check "manually" if the photo has arrived and I find it.

Edit: Even if i just run this code only

import telebot 
bot = telebot.TeleBot("MY TOKEN")

@bot.message_handler(func=lambda message: True) 
def trivial(message): 
 print('yes')

bot.polling()

I get 'yes' for text messages and absolutely nothing, not even raised exceptions for photos.

2

There are 2 best solutions below

1
hc_dev On

In Telegram Bot API there is a resource /sendphoto See: Sending message in telegram bot with images

Try to find the related method in the PyTelegramBotApi. Or implement the function to send a photo your own (e.g. using requests library in Python). Then you can use it in the message-handlers of your bot, to forward images.

0
Felix O On

If you add content_types as a parameter you will receive whatever is specified in the argument (which takes an array of strings). Probably the default value for that parameter is set to ['text']. Which will only listen for 'text' messages.

To get the results you're looking for

your test code will look like:

import telebot 
bot = telebot.TeleBot("MY TOKEN")

@bot.message_handler(func=lambda, message: True, content_types=['photo','text']) 
def trivial(message): 
 print('yes')

bot.polling()

And your working code:

@bot.message_handler(func=lambda, message: message.chat.type=='private', content_types=['photo','text']) 
def forwarder(message): 
 bot.forward_message(group, message.chat.id, message.id)
 bot.send_message(group, '#id'+str(message.chat.id))