Perl | Telegram Bot和sendPhoto方法

时间:2016-09-28 10:00:42

标签: perl bots telegram

我需要帮助纯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 ? }

1 个答案:

答案 0 :(得分:0)

我犯了一个错误)

用于文件上传,我不需要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
);