发送照片后机器人没有反应

问题描述 投票:0回答:1

当我想从电报发送文件到机器人时,我没有得到任何响应,另外在 docker 中我没有任何日志。

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}')

编辑:

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)

测试用例:
/照片 - 得到回应
当在没有文件的情况下使用此命令时,机器人会在日志中正确响应
/照片 + 照片 - 没有得到回复
当对任何文件使用此命令时,机器人不会响应任何内容。
我不知道出了什么问题。

python-3.x python-telegram-bot
1个回答
0
投票

将照片保存在 FTP 或本地服务器中时,我想使用 /photo 并在同一条消息中发送照片/文件。也许可以是另一种方法,但我不知道如何得到这个。

根据此描述,我得出结论,您发送给机器人的消息如下所示:

这意味着文本

/photo
在照片caption中,即
Message.text
None
,而
Message.caption
包含文本
/photo

CommandHandler
不考虑字幕,它只考虑
Message.text
。 PTB 当前不包含任何允许检查标题中命令的处理程序或过滤器。你必须要么


免责声明:我目前是

python-telegram-bot

的维护者
© www.soinside.com 2019 - 2024. All rights reserved.