为 Telegram (PHP) Bot 使用会话
Use session for Telegram (PHP) Bot
我正在尝试制作一个连接到 RCON 的 Telegram 机器人,但目前我卡在会话的使用中,似乎它们没有保存。
到目前为止,这是我的代码:
<?php
define('BOT_TOKEN', 'xx:xxxxxx');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
//Start session
session_set_cookie_params(3600, "/");
session_start();
//read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$reply = "";
if (isset($_SESSION['AskIP'])){
$_SESSION = array('AskPort' => true,
'ip' => $message);
$reply = "Saved IP: ". $message ."Write the port";
}
else if (isset($_SESSION['AskPort'])){
$ip = $_SESSION['ip'];
$_SESSION = array('connected' => true,
'ip' => $ip,
'port' => $message);
$reply = "Now you are connected the rcon (".$_SESSION['ip']. ":". $_SESSION['port'].")! Type /disconnect to disconnect from the rcon!";
}
else if (substr($message, 0, 11) === "/disconnect" && strlen($message) == 11 && isset($_SESSION['connected'])){
$_SESSION['connected'] = false;
$reply = "Disconnected from". $_SESSION['ip'] .":". $_SESSION['port'];
$_SESSION = array();
session_destroy();
}
else if (substr($message, 0, 5) === "/rcon" && strlen($message) == 5) {
$_SESSION = array('AskIP' => true);
$reply = "Write the Server IP";
}
if (empty($reply))
return;
$sendto = API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
?>
目前它仅在您键入 /rcon 时有效(以及回复 "Write the IP" 但在那之后没有回复,隐私设置已经被禁用。
我最终只是使用 mysql。
我正在尝试制作一个连接到 RCON 的 Telegram 机器人,但目前我卡在会话的使用中,似乎它们没有保存。 到目前为止,这是我的代码:
<?php
define('BOT_TOKEN', 'xx:xxxxxx');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
//Start session
session_set_cookie_params(3600, "/");
session_start();
//read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$reply = "";
if (isset($_SESSION['AskIP'])){
$_SESSION = array('AskPort' => true,
'ip' => $message);
$reply = "Saved IP: ". $message ."Write the port";
}
else if (isset($_SESSION['AskPort'])){
$ip = $_SESSION['ip'];
$_SESSION = array('connected' => true,
'ip' => $ip,
'port' => $message);
$reply = "Now you are connected the rcon (".$_SESSION['ip']. ":". $_SESSION['port'].")! Type /disconnect to disconnect from the rcon!";
}
else if (substr($message, 0, 11) === "/disconnect" && strlen($message) == 11 && isset($_SESSION['connected'])){
$_SESSION['connected'] = false;
$reply = "Disconnected from". $_SESSION['ip'] .":". $_SESSION['port'];
$_SESSION = array();
session_destroy();
}
else if (substr($message, 0, 5) === "/rcon" && strlen($message) == 5) {
$_SESSION = array('AskIP' => true);
$reply = "Write the Server IP";
}
if (empty($reply))
return;
$sendto = API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
?>
目前它仅在您键入 /rcon 时有效(以及回复 "Write the IP" 但在那之后没有回复,隐私设置已经被禁用。
我最终只是使用 mysql。