MadelineProto/bot.php

56 lines
2.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
2018-03-23 16:39:58 +01:00
/*
* Various ways to load MadelineProto
*/
if (!file_exists(__DIR__.'/vendor/autoload.php')) {
echo 'You did not run composer update, using madeline.php'.PHP_EOL;
if (!file_exists('madeline.php')) {
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';
} else {
require_once 'vendor/autoload.php';
2017-02-12 19:55:49 +01:00
}
class EventHandler extends \danog\MadelineProto\EventHandler
{
public function onAny($update)
{
if (isset($update['message']['out']) && $update['message']['out']) {
return;
}
$res = json_encode($update, JSON_PRETTY_PRINT);
if ($res == '') {
$res = var_export($update, true);
}
2018-04-18 17:22:09 +02:00
try {
$this->messages->sendMessage(['peer' => $update, 'message' => $res, 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
2018-04-18 17:22:09 +02:00
\danog\MadelineProto\Logger::log((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR);
} catch (\danog\MadelineProto\Exception $e) {
2018-04-18 17:22:09 +02:00
\danog\MadelineProto\Logger::log((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR);
//$this->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
}
}
2018-05-08 21:08:09 +02:00
$MadelineProto = new \danog\MadelineProto\API('bot.madeline');
2018-03-29 13:53:27 +02:00
$MadelineProto->start();
$MadelineProto->setEventHandler('\EventHandler');
$MadelineProto->loop();