无法通过电报Bot返回并发送SendPhoto

时间:2020-06-23 13:05:01

标签: java telegram-bot

我对用户的回复服务

public class ReplyMessageService {

    public SendMessage getTextMessage(Long chatId, String text) {
        return new SendMessage()
                .enableMarkdown(false)
                .setChatId(chatId)
                .setText(text);
    }

    ...

    public SendPhoto getMessageWithPicture(Long chatId, GooglePlayGame game){
        return new SendPhoto()
                .setChatId(chatId)
                .setPhoto(game.getPictureURL())
                .setCaption(game.toString());
    }

}

从处理程序中,我将调用这些方法之一并返回SendMessage或SendPhoto。像这样:

public class HelpMessageHandler implements MessageHandler {

    private ReplyMessageService replyMessageService;

    public HelpMessageHandler(ReplyMessageService replyMessageService) {
        this.replyMessageService = replyMessageService;
    }

    @Override
    public SendMessage handle(Update update){
        ...
        return replyMessageService.getTextMessage(chatId, "I'll help you!);
    } 

并且:

public class BlaBlaBlaHandler {

    private ReplyMessageService replyMessageService;

    public HelpMessageHandler(ReplyMessageService replyMessageService) {
        this.replyMessageService = replyMessageService;
    }

    @Override
    public PartialBotApiMethod handle(Update update){
      ...
      switch(update.getMessage().getText()) {
         case "/size":
          return replyMessageService.getTextMessage(callBackId, game.getSize());
         case "/all":
          return replyMessageService.getMessageWithPicture(Long chatId, Game game);
    }

如您所见,SendPhoto和SendMessage只有一个(我想)通用类:PartialBotApiMethod,所以我将返回这种类型。

结果将进入主类并执行:

@Override
    public void onUpdateReceived(Update update) {
        PartialBotApiMethod<?> responseToUser = updateReceiver.handleUpdate(update);
        try {
            execute(responseToUser); //error
        }
        catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

但是我不能执行PartialBotApiMethod对象,只能执行BotApiMethod(Partial的子类)。因此,SendMessage扩展了BotApi,SendPhoto扩展了PartialBotApi。我可以执行SendPhoto,SendMessage,BotApi,但不能执行PartialBotApi。但是我必须返回不同类型的消息(照片,音频,消息等),这就是为什么我需要BotApiMethod / PartialBotApiMethod作为返回类型来处理所有这些类型的原因。

铸造不起作用:

PartialBotApiMethod<?> responseToUser = updateReceiver.handleUpdate(update);
  try {
       execute((BotApiMethod)responseToUser);
      }
...

因为SendPhoto没有扩展BotApiMethod,我会遇到运行时错误:

org.telegram.telegrambots.meta.api.methods.send.SendPhoto类无法转换为org.telegram.telegrambots.meta.api.methods.BotApiMethod类

我很困。现在,我只有一个选择:不发送照片,但是此功能对我的机器人来说非常重要。还是完全重新设计所有应用程序?还是写我自己的SendPhoto实现,它将扩展BotApiMethod?有提示吗?

0 个答案:

没有答案
相关问题