MadelineProto/bot.php

47 lines
2.8 KiB
PHP
Raw Normal View History

2017-01-09 17:23:00 +01:00
#!/usr/bin/env php
<?php
2017-01-07 12:40:51 +01:00
require 'vendor/autoload.php';
$settings = [];
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('bot.madeline');
if (file_exists('token.php') && $MadelineProto === false) {
include_once 'token.php';
$MadelineProto = new \danog\MadelineProto\API($settings);
$authorization = $MadelineProto->bot_login($token);
\danog\MadelineProto\Logger::log($authorization);
}
$offset = 0;
while (true) {
$updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout
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
var_dump($update);
switch ($update['update']['_']) {
case 'updateNewMessage':
2017-01-07 12:40:51 +01:00
if ($update['update']['message']['out']) {
continue;
}
$res = json_encode($update, JSON_PRETTY_PRINT);
2017-01-07 12:40:51 +01:00
if ($res == '') {
$res = var_export($update, true);
}
try {
2017-01-07 12:40:51 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
try {
2017-01-09 17:23:00 +01:00
if (isset($update['update']['message']['media']) && $update['update']['message']['media'] == 'messageMediaPhoto' && $update['update']['message']['media'] == 'messageMediaDocument') {
$time = time();
$file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp');
2017-01-07 12:40:51 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $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()]);
}
}
}
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL;
}