入口文件index.php
require 'CallBack.php';
// 处理回调请求逻辑
$bot = new CallBack();
// 请求方式
if (isset($_GET['method'])) {
$method = $_GET['method'];
if (method_exists($bot, $method)) {
return $bot->$method();
} else {
echo "没有该功能";
die();
}
} else {
echo "调用哪个功能?";
die();
}
CallBack.php
require 'Config.php';
class CallBack
{
private $botApiKey;
private $hookUrl;
private $winiUrl;
public function __construct()
{
$config = new Config();
$this->hookUrl = $config->getUrl();
$this->botApiKey = $config->getApiKey();
$this->winiUrl = $config->getwini();
}
public function index()
{
phpinfo();
}
// 设置回调地址
public function set_callback_url() {
$deleteResponse = $this->sendCurlRequest("https://api.telegram.org/bot{$this->botApiKey}/deleteWebhook");
print_r($deleteResponse);
echo("
");
$data = ['url' => $this->hookUrl];
$setResponse = $this->sendCurlRequest("https://api.telegram.org/bot{$this->botApiKey}/setWebhook", $data);
print_r($setResponse);
}
// 设置菜单命令
public function setCommands() {
$commands = [
['command' => 'start', 'description' => 'Start'],
];
$data = ['commands' => $commands];
$response = $this->sendCurlRequest("https://api.telegram.org/bot{$this->botApiKey}/setMyCommands", $data);
// print_r($response);die;
if ($response['ok']) {
echo json_encode(['code' => 1, 'msg' => 'ok']);
} else {
echo json_encode(['code' => 0, 'msg' => $response['description']]);
}
}
// 回调主逻辑
public function tg_msg() {
$data = json_decode(file_get_contents('php://input'), true);
$keyboard_data = [
[["text" => "点击", "web_app" => ["url" => $this->winiUrl]]],
];
// 将 $data 写入日志文件
$logFile = 'telegram_log.txt';
file_put_contents($logFile, date('Y-m-d H:i:s') . " - " . json_encode($data) . PHP_EOL, FILE_APPEND);
// file_put_contents($logFile, date('Y-m-d H:i:s') . " - " . print_r($data, true) . PHP_EOL, FILE_APPEND);
$reply_markup = json_encode(["inline_keyboard" => $keyboard_data]);
$message = "点击app ";
if (isset($data['message']['text']) && !isset($data['message']['entities'])) {
$chat_id = $data['message']['chat']['id'];
$message_id = $data['message']['message_id'];
$this->deleteMessage($chat_id, $message_id);
$this->respondWithJson(1, 'ok');
return;
}
if (empty($data['message']) && !isset($data['callback_query']['data'])) {
$this->respondWithJson(1, 'ok');
return;
}
if (!empty($data['message'])) {
$this->sendMessage($data['message']['chat']['id'], $message, $reply_markup);
$this->respondWithJson(1, 'ok');
return;
}
$this->respondWithJson(1, 'ok');
}
// 删除消息
public function deleteMessage($chat_id, $message_id)
{
$data = [
'chat_id' => $chat_id,
'message_id' => $message_id,
];
return $this->sendTelegramRequest('deleteMessage', $data);
}
// 发送消息
public function sendMessage($chat_id, $message, $reply_markup)
{
$data = [
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'HTML',
'reply_markup' => $reply_markup,
];
return $this->sendTelegramRequest('sendMessage', $data);
}
// 发送 Telegram 请求
private function sendTelegramRequest($method, $data)
{
$url = "https://api.telegram.org/bot{$this->botApiKey}/{$method}";
return $this->sendCurlRequest($url, $data);
}
// 发送 cURL 请求
private function sendCurlRequest($url, $data = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($data !== null) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// 响应 JSON 数据
private function respondWithJson($code, $msg) {
echo json_encode(['code' => $code, 'msg' => $msg]);
}
}
Config.php
class Config {
private $hookUrl = '域名/wini-mini-app/telegram/index.php?method=tg_msg';//回调地址
private $winiUrl = '域名/wini-mini-app/';
private $botApiKey = '7189631927:AAHfYLqKhxjGumIHCqlNuv8jH8';
public function getUrl() {
return $this->hookUrl;
}
public function getApiKey() {
return $this->botApiKey;
}
public function getwini() {
return $this->winiUrl;
}
}
浏览器访问,通过method传参数,请求对应的方法
域名/wini-mini-app/telegram/index.php?method=index