Skip to content

Commit

Permalink
update for env
Browse files Browse the repository at this point in the history
  • Loading branch information
anhao committed Feb 9, 2021
1 parent 9f1fe55 commit faed795
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 61 deletions.
81 changes: 81 additions & 0 deletions api/Bot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* 简单封装 tg bot 请求
* Class Bot
* @author Alone88
* @link https://alone88.cn
*/
class Bot
{
// chat_id sign key
private string $key;
//Bot Token
private string $token;

public function __construct()
{
$this->token = $_ENV['token'];
$this->key = $_ENV['sign_key'] ?? 'abc';
}

public function request($method, $data)
{
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => sprintf('https://api.telegram.org/bot%s/%s', $this->token, $method),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}

public function sendMessage($data): array
{
return $this->request('sendMessage', $data);
}

public function setWebHook($data)
{
return $this->request('setWebhook', $data);
}

public function encryption($chat_id): string
{
return base64_encode($this->key . $chat_id);
}

public function decryption($text): string
{
return substr(base64_decode($text), strlen($this->key));
}

/**
* @param string $key
* @return Bot
*/
public function setKey(string $key): Bot
{
$this->key = $key;
return $this;
}

/**
* @param string $token
* @return Bot
*/
public function setToken(string $token): Bot
{
$this->token = $token;
return $this;
}
}
70 changes: 11 additions & 59 deletions api/index.php
Original file line number Diff line number Diff line change
@@ -1,69 +1,21 @@
<?php
require_once 'Bot.php';

header('content-type: application/json');
$type = $_REQUEST['type'] ?? 'message';
$token = $_REQUEST['token'] ?? null;
$message = $_REQUEST['message'] ?? '';

$bot = new Bot();
// webhook
if ($type === 'webhook') {
// 获取/token 命令,获取聊天 id
$data = json_decode(file_get_contents("php://input"), true);
$message = $data['message']['text'] ?? '';
if ($message === '/token') {
$chat_id = $data['message']['chat']['id'];
$bot->sendMessage(['text' => $bot->encryption($chat_id), 'chat_id' => $chat_id]);
}
echo json_encode(['code' => 200, 'message' => 'success']);

if (is_null($token)) {
echo json_encode(['code' => 422, 'message' => 'token 不能为空']);
} else {
if (is_null($token)) {
echo json_encode(['code' => 422, 'message' => 'token 不能为空']);
// 发送消息
$chat_id = $bot->decryption($token);
$ret = $bot->sendMessage(['text' => $message, 'chat_id' => $chat_id]);
if ($ret['ok']) {
echo json_encode(['code' => 200, 'message' => 'success']);
} else {
// 发送消息
$chat_id = $bot->decryption($token);
$ret = $bot->sendMessage(['text' => $message, 'chat_id' => $chat_id]);
if ($ret['ok']) {
echo json_encode(['code' => 200, 'message' => 'success']);
} else {
echo json_encode(['code' => 422, 'message' => 'error']);
}
}
}

class Bot
{
// chat_id sign key
public string $key = "abc";
//Bot Token
public string $token = "1679016407:AAEHII057c26C5_vY8tSLbO8G6mnyXUOYbM";

public function sendMessage($data): array
{
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => sprintf('https://api.telegram.org/bot%s/sendMessage', $this->token),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}

public function encryption($chat_id): string
{
return base64_encode($this->key . $chat_id);
}

public function decryption($text): string
{
return substr(base64_decode($text), strlen($this->key));
echo json_encode(['code' => 422, 'message' => $ret['description']]);
}
}
20 changes: 20 additions & 0 deletions api/setWebHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
header('content-type: application/json');

$key = $_REQUEST['key'] ?? '';

if ($key !== $_ENV['key']) {
echo json_encode(['code' => 422, 'message' => '设置秘钥错误']);
} else {
//开始设置回调url
$url = $_REQUEST['url'] ?? $_ENV['url'];
require_once 'Bot.php';
$data = ['url' => $url];
$bot = new Bot();
$ret = $bot->setWebHook($data);
if ($ret['ok']) {
echo json_encode(['code' => 200, 'message' => '设置成功']);
} else {
echo json_encode(['code' => 422, 'message' => $ret['description']]);
}
}
12 changes: 12 additions & 0 deletions api/webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
header('content-type: application/json');
require_once 'Bot.php';
$bot = new Bot();
// 获取/token 命令,获取聊天 id
$data = json_decode(file_get_contents("php://input"), true);
$message = $data['message']['text'] ?? '';
if ($message === '/token') {
$chat_id = $data['message']['chat']['id'];
$bot->sendMessage(['text' => $bot->encryption($chat_id), 'chat_id' => $chat_id]);
}
echo json_encode(['code' => 200, 'message' => 'success']);
4 changes: 2 additions & 2 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
},
"routes": [
{
"src": "setWebHook",
"src": "/setWebHook",
"dest": "/api/setWebHook.php"
},
{
"src": "webhook",
"src": "/webhook",
"dest": "/api/webhook.php"
}
]
Expand Down

0 comments on commit faed795

Please sign in to comment.