junisan / php-google-chat

PHP 库,用于为 Google Chat 创建通知

0.1 2022-09-15 10:58 UTC

This package is not auto-updated.

Last update: 2024-09-25 14:45:43 UTC


README

一个简单的库,用于通过 Webhook 使用 Google Chat 编写和发送消息。它允许发送从简单的文本消息到交互式信件。

A custom card

SimpleMessage

获取您需要发送钩子的 URL 后,创建以下文件。这是可以发送的最简单的电子邮件:它通过发送者发送纯文本。

<?php

require_once 'vendor/autoload.php';

$guzzle = new \GuzzleHttp\Client();
$webhooks = [
    'default' => 'https://chat.googleapis.com/v1/spaces/AAAAK4AL5Bg/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=3PPfdSFIA_p3ColcvumRTiRnbMftokJhDjz0RJI3sa8%3D'
];
$sender = new \Junisan\GoogleChat\GoogleChatSender($guzzle, $webhooks);


$message = \Junisan\GoogleChat\SimpleMessage::create()->addText('Hello');
$sender->send($message, 'default');

SimpleMessage 支持一些格式化,遵循 Markdown 格式

<?php
//... Create sender

$message = new \Junisan\GoogleChat\SimpleMessage();
$message
    ->addText('Hola')
    ->addLine()
    ->addBoldText('Como estás')
    ->addLine()
    ->addLink('https://www.juannicolas.eu');
$sender->send($message, 'default');

消息

Message 允许您添加卡片、部分和一些 UI 组件,允许您创建非常吸引人的界面,甚至与用户交互。

$link = \Junisan\GoogleChat\UIElements\TextButton::create('http://google.es', 'Follow');
$link2 = \Junisan\GoogleChat\UIElements\TextButton::create('http://google.es', 'Unfollow');
$link3 = \Junisan\GoogleChat\UIElements\ImageButton::create('http://google.es', 'https://goo.gl/aeDtrS');

$order = \Junisan\GoogleChat\UIElements\KeyValue::create('Order No.', '12345');
$status = \Junisan\GoogleChat\UIElements\KeyValue::create('Status', '12345');
$textParagraph = \Junisan\GoogleChat\UIElements\TextParagraph::create('Hola');

$sectionA = \Junisan\GoogleChat\Elements\Section::create()
    ->addWidgets($order, $status, $textParagraph, $link3);



$sectionB = \Junisan\GoogleChat\Elements\Section::create()
    ->addWidgets($link, $link2);

$card = \Junisan\GoogleChat\Elements\Card::create('Pizza __Bot__', 'pizzabot@example.com', 'https://goo.gl/aeDtrS')
    ->addSections($sectionA, $sectionB);

$cardB = \Junisan\GoogleChat\Elements\Card::create('Hola');
$message = \Junisan\GoogleChat\Message::create()
    ->addCard($card);

$sender->send($message, 'default');

A custom card

发送者

发送者是负责发送您创建的消息实例的类。构建时,您必须传递一个包含不同频道名称和 URL 的数组,如下所示。

$webhooks = [
    'default' => 'https://chat.googleapis.com/v1/.....',
    'another_channel' => 'https://chat.googleapis.com/v1/.....'
];

$sender = new \Junisan\GoogleChat\GoogleChatSender($guzzle, $webhooks);

当您想发送消息时,必须调用 send 方法,将发送的消息和通过哪个 频道 发送该消息的名称作为参数传递。

$sender->send($message, 'another_channel');