1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 14:54:47 +01:00
MadelineProto/userbots/MadelineProto_bot.php

163 lines
8.9 KiB
PHP
Raw Normal View History

#!/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/>.
*/
2018-10-13 17:16:10 +02:00
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';
}
$settings = [];
$MadelineProto = false;
2017-08-13 18:52:32 +02:00
try {
$MadelineProto = new \danog\MadelineProto\API('MadelineProto_bot.madeline');
} catch (\danog\MadelineProto\Exception $e) {
$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-21 19:04:37 +01:00
if (file_exists('token.php') && $MadelineProto === false) {
include_once 'token.php';
$MadelineProto = new \danog\MadelineProto\API($settings);
2017-02-21 19:04:37 +01:00
$authorization = $MadelineProto->bot_login($MadelineProto_token);
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($authorization, \danog\MadelineProto\Logger::NOTICE);
}
$offset = 0;
2017-02-21 21:05:09 +01:00
$reply_markup = ['inline_keyboard' => [
2017-02-21 19:04:37 +01:00
[ // Row 1
['text' => 'Row 1 c1'],
['text' => 'Row 1 c2'],
['text' => 'Row 1 c3'],
2017-02-21 19:04:37 +01:00
],
[ // Row 2
['text' => 'Row 2 c1'],
['text' => 'Row 2 c2'],
['text' => 'Row 2 c3'],
2017-02-21 19:04:37 +01:00
],
[ // Row 3
['text' => 'Row 3 c1'],
['text' => 'Row 3 c2'],
['text' => 'Row 3 c3'],
2017-02-21 19:04:37 +01:00
],
2017-02-21 21:05:09 +01:00
],
2017-02-21 19:04:37 +01:00
];
2017-02-21 21:05:09 +01:00
$start = 'This bot can create inline text buttons.
To use it, simply type an inline query with the following syntax:
@MadelineProto_bot Text to show in message
Row 1 c1 | Row 1 c2 | Row 1 c3
Row 2 c1 | Row 2 c2 | Row 2 c3
Row 3 c1 | Row 3 c2 | Row 3 c3
This will create a keyboard exactly like the one used in this message (click the buttons ;D) with the phrase "Text to show in message" instead of this help message.
2017-02-21 21:05:09 +01:00
Created by [Daniil Gentili](mention:@danogentili) (@daniilgentili) using the [MadelineProto PHP MTProto client](daniil.it/MadelineProto).';
echo 'Bot started.'.PHP_EOL;
while (true) {
2018-03-04 17:42:48 +01: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
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':
if (isset($update['update']['message']['out']) && $update['update']['message']['out']) {
continue;
}
2017-08-13 18:52:32 +02:00
try {
2017-05-03 12:04:11 +02:00
if (isset($update['update']['message']['message']) && preg_match('|/start|', $update['update']['message']['message'])) {
2017-02-21 19:04:37 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id'], 'parse_mode' => 'markdown', 'reply_markup' => $reply_markup]);
}
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-05-03 12:03:51 +02:00
//$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
break;
case 'updateNewChannelMessage':
if (isset($update['update']['message']['out']) && $update['update']['message']['out']) {
continue;
}
2017-08-13 18:52:32 +02:00
try {
2017-02-11 15:18:18 +01:00
if (preg_match('|/start|', $update['update']['message']['message'])) {
2017-02-21 19:04:37 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $start, 'reply_to_msg_id' => $update['update']['message']['id'], 'parse_mode' => 'markdown', 'reply_markup' => $reply_markup]);
}
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-05-03 12:03:51 +02:00
//$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
} catch (\danog\MadelineProto\Exception $e) {
2017-05-03 12:03:51 +02:00
//$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}
break;
case 'updateBotInlineQuery':
try {
2017-02-11 15:18:18 +01:00
$sswitch = ['_' => 'inlineBotSwitchPM', 'text' => 'FAQ', 'start_param' => 'lel'];
if ($update['update']['query'] === '') {
$MadelineProto->messages->setInlineBotResults(['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'switch_pm' => $sswitch]);
} else {
$toset = ['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'private' => true];
$rows = explode("\n", $update['update']['query']);
$text = array_shift($rows);
if (empty($rows)) {
$MadelineProto->messages->setInlineBotResults(['query_id' => $update['update']['query_id'], 'results' => [], 'cache_time' => 0, 'switch_pm' => $sswitch]);
2017-02-11 15:18:18 +01:00
} else {
array_walk($rows, function (&$value, $key) {
$value = explode('|', $value);
2017-03-11 19:55:56 +01:00
array_walk($value, function (&$value, $key) {
$value = ['text' => trim($value), 'url' => 'https://yayponies.eu'];
2017-03-11 19:55:56 +01:00
});
});
$toset['results'] = [['_' => 'inputBotInlineResult', 'id' => (string) random_int(0, pow(2, 31) - 1), 'type' => 'article', 'title' => $text, 'description' => 'Your keyboard', 'send_message' => ['_' => 'inputBotInlineMessageText', 'message' => $text, 'reply_markup' => ['inline_keyboard' => $rows]]]];
$MadelineProto->messages->setInlineBotResults($toset);
2017-02-11 15:18:18 +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 {
2017-02-11 15:18:18 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-02-11 15:18:18 +01:00
} catch (\danog\MadelineProto\Exception $e) {
}
2017-08-13 18:52:32 +02:00
try {
2017-02-11 15:18:18 +01:00
$toset['switch_pm'] = $sswitch;
$MadelineProto->messages->setInlineBotResults($toset);
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-02-11 15:18:18 +01:00
} catch (\danog\MadelineProto\Exception $e) {
}
} catch (\danog\MadelineProto\Exception $e) {
try {
2017-02-11 15:18:18 +01:00
$MadelineProto->messages->sendMessage(['peer' => $update['update']['user_id'], 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-02-11 15:18:18 +01:00
} catch (\danog\MadelineProto\Exception $e) {
}
2017-08-13 18:52:32 +02:00
try {
2017-02-11 15:18:18 +01:00
$toset['switch_pm'] = $sswitch;
$MadelineProto->messages->setInlineBotResults($toset);
} catch (\danog\MadelineProto\RPCErrorException $e) {
2017-02-11 15:18:18 +01:00
} catch (\danog\MadelineProto\Exception $e) {
}
}
}
}
2017-02-21 19:04:37 +01:00
\danog\MadelineProto\Serialization::serialize('MadelineProto_bot.madeline', $MadelineProto);
}