3: 接收消息

网关通过向您应用中的 webhook route 发送 POST 请求来传递每个传入消息。您验证共享密钥,然后可以随意处理该消息。

3.1: Webhook 路由

route both POST receive {
	$given = (string)($_SERVER['HTTP_SECRET'] ?? '')
	if ($this->secret === '' || !hash_equals($this->secret, $given)) return output(['error' => 'unauthorized'], code: 401)
	$data = json_decode((string)file_get_contents('php://input'), true) ?: []
	type_message::create(sender: (string)($data['from'] ?? ''), kind: (string)($data['type'] ?? 'text'), body: (string)($data['text'] ?? ''), ts: time())
	return output(['ok' => true])
}

两个细节很重要:

3.2: 处理消息

在 webhook 内部,您可以访问完整的应用程序。像上面那样记录它,使用 WhatsApp 资源自动回复,将其路由到人工处理,或通过 Phlo Realtime 将其分发到实时收件箱。演示记录到 JSON 模型并渲染一个收件箱;只需将网关的 webhook 指向 /receive,消息就会出现。

我们使用必要的cookie来使该网站正常工作。在您的许可下,我们还使用分析工具来改善网站。