【问题标题】:Perl | Telegram Bot and sendPhoto method珀尔 | Telegram Bot 和 sendPhoto 方法
【发布时间】:2016-09-28 10:00:42
【问题描述】:

我需要 pure perl(没有 WWW::Telegram::BotAPI)实现 sendPhoto BotAPI 方法的帮助

我发送的简单文本没有问题

use LWP::UserAgent;
use HTTP::Request::Common;
use JSON::MaybeXS;
....
....
   my $ua = LWP::UserAgent->new;

   utf8::decode($message);
   my $p = {
            chat_id=>$groupid,
            parse_mode=>'HTML',
            text=>$message
    };

   my $response = $ua->request(
        POST 'https://api.telegram.org/bot'.$token.'/sendMessage',
        Content_Type    => 'application/json',
        Content         => JSON::MaybeXS::encode_json($p)
        );
...

但是使用 sendPhoto (https://core.telegram.org/bots/api#sendphoto) 我有问题。如果我想上传新图像,我必须将哪个 JSON 发送到服务器?

chat_id=>$groupid,
caption=>$message
photo=> { ? binary blob here ? }

【问题讨论】:

  • 嗯。文档说 InputFile 对象应该使用 multipart/formdata 发布。但这很奇怪。
  • 不使用 WWW::Telegram::BotAPI 不包括从 its source 借用。我建议你研究它是如何在那里完成的。看起来您需要切换内容类型。作者非常好,甚至在源代码中提供了如何使用 HTTP::Request::Common 的链接。总的来说,源代码很有趣,值得一读。
  • 请问您为什么不想使用该模块?
  • @Borodin 只是为了增加知识。我的机器人太简单了,发送消息并停止。仅用于服务通知,不适用于对话框。

标签: perl bots telegram


【解决方案1】:

我错了)

对于文件上传,我不需要 json!

错了!

my $p = [ chat_id => $groupid,
          caption => 'image caption',
          photo => ["/tmp/pdf.png"]
];

my $response = $ua->request(
POST 'https://api.telegram.org/bot'.$token.'/sendPhoto',
Content_Type    => 'form-data',
Content         => JSON::MaybeXS::encode_json($msg)
);

以下有效

my $p = [ chat_id => $groupid,
          caption => 'image caption',
          photo => ["/tmp/pdf.png"]
];

my $response = $ua->request(
POST 'https://api.telegram.org/bot'.$token.'/sendPhoto',
Content_Type    => 'form-data',
Content         => $msg
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-03
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多