2

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.

carcaduno
  • 31
  • 3
  • 2
    The user Omid N has the answer. And with a simple example: https://stackoverflow.com/a/46242244/11002771 – Felix O Aug 12 '22 at 00:27

2 Answers2

3

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))
Felix O
  • 61
  • 4
0

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.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • Thank you but the problem is not in the forward_message method because if i run it typing the message ID it does its job even with pics. As i implied in the 'Edit', the problem is apparently that any message_handler completely miss photos in my code – carcaduno Aug 10 '21 at 05:48