| 1 | package Mojo::Webqq::Plugin::PostQRcodeToTelegram; | ||
| 2 | our $PRIORITY = 0; | ||
| 3 | our $CALL_ON_LOAD = 1; | ||
| 4 | |||
| 5 | my @qrcode_message_ids; | ||
| 6 | sub call{ | ||
| 7 | my $client = shift; | ||
| 8 | my $data = shift; | ||
| 9 | $client->on(input_qrcode=>sub{ | ||
| 10 | my($client,$qrcode_path) = @_; | ||
| 11 | # Generate Telegram Bot API URL | ||
| 12 | my $telegram_api = 'https://api.telegram.org/bot' . $data->{api_key} .'/sendPhoto'; | ||
| 13 | my $response = $client->http_post($telegram_api,{json=>1},form=>{ | ||
| 14 | chat_id => $data->{chat_id}, | ||
| 15 | caption => 'QQ帐号' .(defined $client->uid?$client->uid:$client->account) .'登录二维码', | ||
| 16 | photo=>{file=>$qrcode_path} | ||
| 17 | }); | ||
| 18 | |||
| 19 | if(not defined $response){ | ||
| 20 | $client->warn("插件[".__PACKAGE__ . "]发送登录二维码失败,响应数据异常"); | ||
| 21 | return | ||
| 22 | } | ||
| 23 | |||
| 24 | if (not $response->{"ok"}) { | ||
| 25 | $client->warn("插件[".__PACKAGE__ . "]发送登录二维码失败,错误原因:". $response->{"description"}); | ||
| 26 | return | ||
| 27 | } | ||
| 28 | push @qrcode_message_ids, $response->{"result"}->{"message_id"}; | ||
| 29 | my $chat = $response->{"result"}->{"chat"}; | ||
| 30 | my $chat_type = $chat->{"type"}; | ||
| 31 | # Check response types: private, group, supergroup or channel | ||
| 32 | if ($chat_type eq "private") { | ||
| 33 | $client->info("插件[".__PACKAGE__ . "]二维码已发送给Telegram用户[ ". $chat->{"username"} . " ]"); | ||
| 34 | } | ||
| 35 | elsif ($chat_type eq "group" or $chat_type eq "supergroup") { | ||
| 36 | $client->info("插件[".__PACKAGE__ . "]二维码已发送至Telegram群组[ ". $chat->{"title"} . " ]"); | ||
| 37 | } | ||
| 38 | elsif ($chat_type eq "channel") { | ||
| 39 | $client->info("插件[".__PACKAGE__ . "]二维码已发送至Telegram频道[ ". $chat->{"title"} . " ]"); | ||
| 40 | } else { | ||
| 41 | $client->info("插件[".__PACKAGE__ . "]二维码已发送,目标未知"); | ||
| 42 | } | ||
| 43 | }); | ||
| 44 | $client->on(qrcode_expire=>sub{ | ||
| 45 | my $last_id = $qrcode_message_ids[-1]; | ||
| 46 | my $telegram_api = 'https://api.telegram.org/bot' . $data->{api_key}; | ||
| 47 | my $response = $client->http_post($telegram_api . '/editMessageCaption',{json=>1},form=>{ | ||
| 48 | chat_id => $data->{chat_id}, | ||
| 49 | message_id => $last_id, | ||
| 50 | caption => '二维码已过期', | ||
| 51 | }); | ||
| 52 | if(not defined $response){ | ||
| 53 | $client->warn("插件[".__PACKAGE__ . "]提示二维码过期失败,响应数据异常"); | ||
| 54 | return | ||
| 55 | } | ||
| 56 | |||
| 57 | if (not $response->{"ok"}) { | ||
| 58 | $client->warn("插件[".__PACKAGE__ . "]提示二维码过期失败,错误原因:". $response->{"description"}); | ||
| 59 | return | ||
| 60 | } | ||
| 61 | }); | ||
| 62 | $client->on(login=>sub{ | ||
| 63 | my $telegram_api = 'https://api.telegram.org/bot' . $data->{api_key}; | ||
| 64 | foreach my $message_id (@qrcode_message_ids) { | ||
| 65 | $client->http_post($telegram_api . '/deleteMessage',{json=>1},form=>{ | ||
| 66 | chat_id => $data->{chat_id}, | ||
| 67 | message_id => $message_id, | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | @qrcode_message_ids = (); | ||
| 71 | $client->http_post($telegram_api . '/sendMessage',{json=>1},form=>{ | ||
| 72 | chat_id => $data->{chat_id}, | ||
| 73 | text => "登录成功", | ||
| 74 | }); | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | 1; |