Browse Source

feat: base framework of tgbot

master
Dnomd343 3 years ago
commit
1ed8a3a50b
  1. 2
      .env
  2. 23
      cmdRoute.php
  3. 69
      main.php
  4. 60
      tgInterface.php

2
.env

@ -0,0 +1,2 @@
BOT_NAME={Bot Username}
BOT_TOKEN={Bot Api_token}

23
cmdRoute.php

@ -0,0 +1,23 @@
<?php
$cmds = array( // 命令列表
'test'
);
function route($cmd, $rawParam) { // 命令请求路由
switch ($cmd) {
case 'test':
test($rawParam);
break;
}
}
function routeCallback($cmd, $rawParam) { // 回调请求路由
switch ($cmd) {
case 'test':
testCallback($rawParam);
break;
}
}
?>

69
main.php

@ -0,0 +1,69 @@
<?php
require_once 'cmdRoute.php';
require_once 'tgInterface.php';
$env = loadEnv();
$apiToken = $env['BOT_TOKEN'];
$botAccount = $env['BOT_NAME'];
$apiPath = 'https://api.telegram.org/bot' . $apiToken; // Telegram API接口
$webhook = json_decode(file_get_contents("php://input"), TRUE); // Webhook接受信息
$isCallback = isset($webhook['callback_query']) ? true : false;
if ($isCallback) {
$message = $webhook['callback_query']['message'];
$messageText = $webhook['callback_query']['data'];
$messageFrom = $webhook['callback_query']['from'];
} else {
$message = $webhook['message'];
$messageText = $webhook['message']['text'];
$messageFrom = $webhook['message']['from'];
}
$chat = $message['chat'];
$chatId = $chat['id'];
$messageId = $message['message_id'];
$isGroup = ($chat['type'] === 'group') ? true : false;
$userId = $messageFrom['id'];
$userName = $messageFrom['first_name'];
$userAccount = $messageFrom['username'];
$userLanguage = $messageFrom['language_code'];
foreach ($cmds as $cmd) {
if (strpos($messageText, '/' . $cmd) === 0) { // 判断请求开头
$rawParam = substr($messageText, strlen($cmd) + 1); // 获取请求参数
if ($isGroup && strpos($rawParam, '@' . $botAccount) === 0) {
$rawParam = substr($rawParam, strlen($botAccount) + 1); // 去除群组中@机器人
}
$rawParam = trim($rawParam); // 消除命令参数前后空格
if ($isCallback) {
routeCallback($cmd, $rawParam);
} else {
route($cmd, $rawParam);
}
}
}
function test($rawParam) {
debugDump();
}
function testCallback($rawParam) {
debugDump();
}
function loadEnv() { // 载入环境变量
$file = fopen('.env', 'r');
$data = array();
while (!feof($file)) { // 逐行读入文件
$record = explode('=', trim(fgets($file)));
if (count($record) === 2) { // 合法记录
$data[$record[0]] = $record[1];
}
}
fclose($file);
return $data;
}
?>

60
tgInterface.php

@ -0,0 +1,60 @@
<?php
function sendPayload($payload) { // 发送API请求
global $apiPath;
$url = $apiPath . '/' . $payload['method'] . '?';
foreach ($payload as $param => $content) {
$url .= '&' . $param . '=' . urlencode($content);
}
return file_get_contents($url);
}
function sendMessage($chatId, $params) { // 发送消息
$params += array (
'method' => 'sendMessage',
'chat_id' => $chatId
);
return sendPayload($params);
}
function sendDocument($chatId, $params) { // 发送文件
$params += array (
'method' => 'sendDocument',
'chat_id' => $chatId
);
return sendPayload($params);
}
function sendText($chatId, $msg) { // 发送纯文本
return sendMessage($chatId, array(
'text' => $msg
));
}
function sendAuto($chatId, $content) { // 自动判别发送类型
if (isset($content['document'])) { // 以文件类型发送
sendDocument($chatId, $content);
} else {
sendMessage($chatId, $content);
}
}
function debugDump() { // 调试接口
global $webhook;
global $isCallback, $isGroup;
global $messageId, $chatId, $userId;
global $messageText, $userName, $userAccount, $userLanguage;
$msg .= 'isCallback: ' . ($isCallback ? 'true' : 'false') . PHP_EOL;
$msg .= 'isGroup: ' . ($isGroup ? 'true' : 'false') . PHP_EOL;
$msg .= 'messageText: ' . $messageText . PHP_EOL;
$msg .= 'messageId: ' . $messageId . PHP_EOL;
$msg .= 'chatId: ' . $chatId . PHP_EOL;
$msg .= 'userId: ' . $userId . PHP_EOL;
$msg .= 'userName: ' . $userName . PHP_EOL;
$msg .= 'userAccount: ' . $userAccount . PHP_EOL;
$msg .= 'userLanguage: ' . $userLanguage . PHP_EOL;
sendText($chatId, $msg);
sendText($chatId, json_encode($webhook));
}
?>
Loading…
Cancel
Save