federicoq / zulip-php
Zulip PHP 客户端
Requires
- guzzlehttp/guzzle: ^6.3
This package is not auto-updated.
Last update: 2024-09-29 03:16:15 UTC
README
我们在玩 Zulip 时发现没有好的 PHP 客户端... 所以我们做了一个新的!
安装
使用 composer!
composer require fredericoq/zulip-php
用法(仅限意大利语)
该库很简单,允许对所有 Zulip REST API(笔记)进行调用。
有三种可能的实现方法
第一部分是所有三个版本共有的,即创建客户端。
require 'vendor/autoload.php'; $client = new \ZulipPhp\Client('https://chat.mndrn.cloud/', 'bot-email', 'bot-api-key');
此时,我们已经建立了与 Zulip 服务器的连接,可以启动我们想要的任何方法。
WebHook
这是一种最简单的模式,面向“临时”写入,正如其名... 一个外部事件触发了消息。
在这种情况下,参考 .php 非常简单,只需创建一个客户端,格式化消息并发送即可。
私密消息
$client->sendMessage([ 'to' => 'email-utente', 'type' => 'private', 'content' => "Messaggio!" ]);
消息到一个流
$client->sendMessage([ 'to' => 'Canale', 'subject' => 'nome-dello-stream', 'type' => 'stream', 'content' => "Messaggio!" ]);
始终连接的客户端
对于始终连接的客户端,需要一些逻辑。
- 第一步是了解 Zulip 的逻辑。
- 第二步是想象我们需要一个始终运行的脚本,持续监听以接收新消息。
- Zulip 的逻辑
Zulip 允许我们以两种方式读取消息:通过注册一个队列和通过请求单个/组消息。
队列是最佳模式,因为它允许接收从注册以来发生的所有事件;不会过期,因此只需注册一次,然后可以将信息保存到存档中。一旦注册了一个队列,就可以询问 Zulip 是否有更改,并决定如何处理。
对于此目的,请求消息不是最佳选择,因此将在 Batch 版本中处理。
- 始终运行的脚本
为了创建一个始终活跃的客户端,我们需要在 while(true) 中插入逻辑。简单易懂;柠檬压榨! :)
编写读取队列中所有事件的代码段
$client = new \ZulipPhp\… $queue_id = false; $last_id = -1; // Di default, -1 ti risponde con tutti gli eventi dalla creazione della queue. // il last_id non è il vero identificativo dell'evento, è un indice parziale a partire dalla nascita della query (-1, 0, 1, 2, 3, …) // Registriamo la queue: $queue_id = $zulip->registerQueue()['queue_id']; while(true) { try { $events = $client->getEvents([ 'queue_id' => $queue_id, 'last_event_id' => $last_id, 'dont_block' => 'false']); if(count($events) > 0) { $last_id += count($events); print_r($events); } else { echo "No new messages. Last event id: ".$msg.".\n"; } sleep(2); } catch (Exception $e) { echo "Error in fetching Events. Try again in 1 sec.\n"; sleep(1); } }
一旦设计好,此脚本需要通过 cli(php script.php)执行,我们会看到它将跟踪所有发生的事件.. 消息、反应、订阅、心跳等。
此时,可以在 if(count($events)>0) {} 块中插入任何应用程序逻辑,我们可以按事件处理每个事件,并查看 $event['type'] 以区分操作。
在任何情况下,请记住,我们可以调用各种 $client->sendMessage() 和其他对我们有用的方法。
批量
此模式适用于我们的机器人执行“档案管理员”类型的工作,并且不需要实时反应;在这种情况下,我们将请求 Zulip 从一个 id(这次是一个真正的 id)开始发送消息。
$client = new \ZulipPhp\… $messages = $client->getMessages([ 'narrow' => json_encode([ [ 'stream', 'NomeStream' ] ]), 'had_error_retry' => 'false', 'anchor' => 0, 'num_before' => 1, 'num_after' => 5]); // Fare riferimento alla loro documentazione, per l'endpoint GET /messages
事件示例
私密消息
{
"type": "private",
"reactions": [],
"sender_realm_str": "",
"timestamp": 1512934642,
"id": 6516,
"content": "$ time",
"display_recipient": [
{
"id": 9,
"full_name": "Federico Quagliotto",
"email": "f.quagliotto@mandarinoadv.com",
"is_mirror_dummy": false,
"short_name": "f.quagliotto"
},
{
"id": 19,
"full_name": "Khaleesi",
"email": "khaleesi-bot@chat.mndrn.cloud",
"is_mirror_dummy": false,
"short_name": "khaleesi-bot"
}
],
"sender_short_name": "f.quagliotto",
"sender_id": 9,
"sender_full_name": "Federico Quagliotto",
"client": "website",
"content_type": "text/x-markdown",
"sender_email": "f.quagliotto@mandarinoadv.com",
"avatar_url": "/user_avatars/2/ad4fa2008dc3b96e839ce1ec9d80ad743b5362c2.png?x=x&version=2",
"recipient_id": 34,
"is_me_message": false,
"subject": "",
"subject_links": []
}
公共消息
{
"sender_short_name": "f.quagliotto",
"sender_realm_str": "",
"timestamp": 1512982424,
"client": "website",
"display_recipient": "Città dei Bot",
"sender_full_name": "Federico Quagliotto",
"sender_id": 9,
"reactions": [],
"content": "@**Alessandro Marino** eheh vuoi altri messaggi?",
"id": 6574,
"sender_email": "f.quagliotto@mandarinoadv.com",
"stream_id": 17,
"type": "stream",
"recipient_id": 43,
"is_me_message": false,
"subject": "hello",
"subject_links": [],
"content_type": "text/x-markdown"
}
反应
{
"emoji_name": "dragon_face",
"message_id": 6517,
"id": 9,
"op": "add",
"user": {
"user_id": 9,
"full_name": "Federico Quagliotto",
"email": "f.quagliotto@mandarinoadv.com"
},
"type": "reaction",
"emoji_code": "1f432",
"reaction_type": "unicode_emoji"
}