When I want to send a file from telegram to bot, I don't get any response, additionally in docker I don't have any logs.
from telegram import Update
from telegram.ext import CallbackContext
from . import BaseCommand, command_with_logs, CommandManager
class UploadPhotoCommand(BaseCommand):
name = "photo"
description = "send photo"
def __init__(self, manager: CommandManager) -> None:
self.__manager = manager
@command_with_logs
async def callback(self, update, context):
try:
photos = await update.message.photo
await update.message.reply_text(f'Zdjęcia: {photos}')
except Exception as e:
await update.message.reply_text(f'Error: {e}')
Edit:
from abc import ABC, abstractmethod
from dataclasses import dataclass
from telegram import Update
from telegram.ext import CommandHandler, CallbackContext
from logs import logger
def command_with_logs(func):
async def wrapper(self, update: Update, context: CallbackContext):
logger.info(f'User {update.message.from_user.id} use command /{self.name}')
await func(self, update, context)
return wrapper
class BaseCommand(ABC):
name: str
description: str = None
def get_handler(self) -> CommandHandler:
return CommandHandler(self.name, self.callback)
@abstractmethod
async def callback(self, update: Update, context: CallbackContext):
pass
from typing import List, Dict
from telegram.ext import Application, CommandHandler
from . import BaseCommand
class CommandManager:
def __init__(self, app: Application) -> None:
self.handlers: Dict[str, CommandHandler] = dict()
self.descriptions: Dict[str, str] = {}
self.__app = app
def setup(self, commands: List[BaseCommand]):
for command in commands:
self.handlers[command.name] = command.get_handler()
self.descriptions[command.name] = command.description
self.__app.add_handler(self.handlers[command.name])
def add_command(self, command: BaseCommand):
handler = command.get_handler()
self.handlers[command.name] = handler
self.descriptions[command.name] = command.description
self.__app.add_handler(handler)
def remove_command(self, name: str):
self.__app.remove_handler(self.handlers[name])
self.handlers.pop(name)
self.descriptions.pop(name)
Test case:
/photo - get response
When use this command without file, bot respond correctly in logs
/photo + photo - don't get response
When use this command with any file, bot doesn't respond anything.
I don't know what is wrong.
When save a photo in FTP or local server, I want to use a /photo and send photo/file in the same message. Maybe can be a another method, but I don't have any idea, how to get this.
From this description I conclude that the message that you send to your bot looks like this:
This means that the text /photo
is in the photos caption, i.e. Message.text
is None
, while Message.caption
contains the text /photo
.
CommandHandler
does not consider captions, it only considers Message.text
. PTB currently does not include any handler or filter that allows to check for commands in the caption. You would have to either
CommandHandler
to your needsBaseHandler
- see also hereMessageHandler
Disclaimer: I'm currently the maintainer of python-telegram-bot