| 1 | package Mojo::Weixin::Plugin::PostQRcodeToTelegram; | ||
| 2 | our $PRIORITY = 0; | ||
| 3 | our $CALL_ON_LOAD = 1; | ||
| 4 | |||
| 5 | sub call{ | ||
| 6 | my $client = shift; | ||
| 7 | my $data = shift; | ||
| 8 | $client->on(input_qrcode=>sub{ | ||
| 9 | my($client,$qrcode_path) = @_; | ||
| 10 | # Generate Telegram Bot API URL | ||
| 11 | my $telegram_api = 'https://api.telegram.org/bot' . $data->{api_key} .'/sendPhoto'; | ||
| 12 | my $response = $client->http_post($telegram_api,{json=>1},form=>{ | ||
| 13 | chat_id => $data->{chat_id}, | ||
| 14 | caption => '微信登录二维码', | ||
| 15 | photo=>{file=>$qrcode_path} | ||
| 16 | }); | ||
| 17 | |||
| 18 | if(not defined $response){ | ||
| 19 | $client->warn("插件[".__PACKAGE__ . "]发送登录二维码失败,响应数据异常"); | ||
| 20 | return | ||
| 21 | } | ||
| 22 | |||
| 23 | if (not $response->{"ok"}) { | ||
| 24 | $client->warn("插件[".__PACKAGE__ . "]发送登录二维码失败"); | ||
| 25 | return | ||
| 26 | } | ||
| 27 | my $chat = $response->{"result"}->{"chat"}; | ||
| 28 | my $chat_type = $chat->{"type"}; | ||
| 29 | # Check response types: private, group, supergroup or channel | ||
| 30 | if ($chat_type eq "private") { | ||
| 31 | $client->info("插件[".__PACKAGE__ . "]二维码已发送给Telegram用户[ ". $chat->{"username"} . " ]"); | ||
| 32 | } | ||
| 33 | elsif ($chat_type eq "group" or $chat_type eq "supergroup") { | ||
| 34 | $client->info("插件[".__PACKAGE__ . "]二维码已发送至Telegram群组[ ". $chat->{"title"} . " ]"); | ||
| 35 | } | ||
| 36 | elsif ($chat_type eq "channel") { | ||
| 37 | $client->info("插件[".__PACKAGE__ . "]二维码已发送至Telegram频道[ ". $chat->{"title"} . " ]"); | ||
| 38 | } else { | ||
| 39 | $client->info("插件[".__PACKAGE__ . "]二维码已发送,目标未知"); | ||
| 40 | } | ||
| 41 | }); | ||
| 42 | } | ||
| 43 | 1; |