最好的方法是使用update_id
在每个新请求(即更新)时增加的特定数字。如何实施?
首先,让我们从以下匿名类开始(使用 PHP7):
$lastUpdateId = new class()
{
const FILE_PATH = "last-update-id.txt";
private $value = 1;
public function __construct()
{
$this->ensureFileExists();
$this->value = filesize(self::FILE_PATH) == 0
? 0 : (int)(file_get_contents(self::FILE_PATH));
}
public function set(int $lastUpdateId)
{
$this->ensureFileExists();
file_put_contents(self::FILE_PATH, $lastUpdateId);
$this->value = $lastUpdateId;
}
public function get(): int
{
return $this->value;
}
public function isNewRequest(int $updateId): bool
{
return $updateId > $this->value;
}
private function ensureFileExists()
{
if (!file_exists(self::FILE_PATH)) {
touch(self::FILE_PATH);
}
}
};
该类的作用很明确:update_id
通过纯文件处理最后一个。
注意:课程尽量简短。它不提供错误检查。改用您的自定义实现(例如使用SplFileObject
而不是file_{get|put}_contents()
函数)。
现在,有两种获取更新的方法:Long Polling xor WebHooks(有关每种方法和所有 JSON 属性的更多详细信息,请查看Telegram bot API)。在这两种情况下都应该使用上面的代码(或类似的)。
注意:目前,无法同时使用这两种方法。
长轮询方法(默认)
通过这种方式,您将 HTTPS 请求发送到 Telegram bot API,并且您将在 JSON 格式的对象中获得更新作为响应。因此,可以完成以下工作来获得新的更新(API,为什么使用 offset):
$botToken = "<token>";
$updates = json_decode(file_get_contents("https://api.telegram.org/bot{$botToken}/getUpdates?offset={$lastUpdateId->get()}"), true);
// Split updates from each other in $updates
// It is considered that one sample update is stored in $update
// See the section below
parseUpdate($update);
WebHook 方法(首选)
要求您的服务器支持 HTTPS POST 方法,这是目前获取更新的最佳方式。
最初,您必须使用以下请求(更多详细信息)为您的机器人启用 WebHooks:
https://api.telegram.org/bot<token>/setWebhook?url=<file>
替换<token>
为您的机器人令牌,以及<file>
将接受新请求的文件的地址。同样,它必须是 HTTPS。
好的,最后一步是在指定的 URL 创建文件:
// The update is sent
$update = $_POST;
// See the section below
parseUpdate($update);
从现在开始,您的机器人的所有请求和更新都将直接发送到该文件。
实施parseUpdate()
它的实施完全取决于你。但是,为了展示如何在实现中使用上面的类,这是一个示例和简短的实现:
function parseUpdate($update)
{
// Validate $update, first
// Actually, you should have a validation class for it
// Here, we suppose that: $update["update_id"] !== null
if ($lastUpdateId->isNewRequest($update["update_id"])) {
$lastUpdateId->set($update["update_id"]);
// New request, go on
} else {
// Old request (or possible file error)
// You may throw exceptions here
}
}
享受!
编辑:感谢@Amir 建议的版本使这个答案更加完整和有用。