MadelineProto/bot.php

65 lines
4.4 KiB
PHP
Raw Normal View History

2017-01-09 17:23:00 +01:00
#!/usr/bin/env php
<?php
2017-02-11 15:30:37 +01:00
/*
Copyright 2016-2018 Daniil Gentili
2017-02-11 15:30:37 +01:00
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
2017-08-18 13:27:44 +02:00
set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/MadelineProto/'));
2017-01-07 12:40:51 +01:00
require 'vendor/autoload.php';
2018-02-19 13:23:53 +01:00
$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]; //, 'connection_settings' => ['all' => ['test_mode' => true]]];
2017-05-16 22:08:46 +02:00
2017-02-12 19:55:49 +01:00
try {
$MadelineProto = new \danog\MadelineProto\API('bot.madeline');
2017-02-12 19:55:49 +01:00
} catch (\danog\MadelineProto\Exception $e) {
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($e->getMessage());
2017-08-18 13:27:44 +02:00
$MadelineProto = new \danog\MadelineProto\API($settings);
$authorization = $MadelineProto->bot_login(readline('Enter a bot token: '));
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($authorization, \danog\MadelineProto\Logger::NOTICE);
2017-02-12 19:55:49 +01:00
}
2018-03-02 01:38:10 +01:00
//\danog\MadelineProto\Logger::log($MadelineProto->API->get_config([], ['datacenter' => $MadelineProto->API->datacenter->curdc]));
//\danog\MadelineProto\Logger::log($MadelineProto->API->settings['connection']);
2017-10-09 12:49:01 +02:00
$MadelineProto->session = 'bot.madeline';
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL;
$offset = 0;
while (true) {
2017-10-09 12:49:01 +02:00
$updates = $MadelineProto->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($updates);
foreach ($updates as $update) {
$offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id
switch ($update['update']['_']) {
case 'updateNewMessage':
2017-01-29 22:42:21 +01:00
case 'updateNewChannelMessage':
if (isset($update['update']['message']['out']) && $update['update']['message']['out']) {
2018-02-19 13:23:34 +01:00
continue;
2017-01-07 12:40:51 +01:00
}
$res = json_encode($update, JSON_PRETTY_PRINT);
2017-01-07 12:40:51 +01:00
if ($res == '') {
$res = var_export($update, true);
}
2017-08-13 18:52:32 +02:00
try {
2018-02-19 13:23:34 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['_'] === 'updateNewMessage' ? $update['update']['message']['from_id'] : $update['update']['message']['to_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
2017-01-07 12:40:51 +01:00
} catch (\danog\MadelineProto\RPCErrorException $e) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
2017-08-13 18:52:32 +02:00
try {
if (isset($update['update']['message']['media']) && ($update['update']['message']['media']['_'] == 'messageMediaPhoto' || $update['update']['message']['media']['_'] == 'messageMediaDocument')) {
2018-02-19 12:48:43 +01:00
$time = microtime(true);
$file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp');
2018-02-19 12:48:43 +01:00
$MadelineProto->messages->sendMessage(['peer' => isset($update['update']['message']['from_id']) ? $update['update']['message']['from_id'] : $update['update']['message']['to_id'], 'message' => 'Downloaded to '.$file.' in '.(microtime(true) - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
}
2017-01-07 12:40:51 +01:00
} catch (\danog\MadelineProto\RPCErrorException $e) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
}
}
}