louisgjbertrand/discordwebhook

此包的最新版本(1.0.0)没有可用的许可信息。

1.0.0 2023-01-29 19:12 UTC

This package is auto-updated.

Last update: 2024-09-29 06:15:00 UTC


README

一个简单且轻量级的 webhook 发送器。

基于 Stack exchange 的代码,这个库是一个轻量级的 Discord Webhook 事件发送器,包含两个主要功能。

安装

在您的项目中运行以下命令

composer require louisgjbertrand/discordwebhook

用法

需求

需要内置的 CURL 库。请在您的 php.ini 文件中取消注释 CURL 库

# in the php.ini file
extension=curl

在需要它的 PHP 脚本中引入该类。

require './vendor/autoload.php';

use LouisGJBertrand\DiscordWebhook\DiscordWebhook;

发送事件

您只需使用 Send 函数发送一条消息。此函数只需要 URL。

您可以使用一个指针来存储从 Discord API 返回的 JSON 响应,使用 response 参数。

DiscordWebhook::Send($webhookurl, content: $content, response: $response);

当前状态不支持所有参数,但将在以后添加。它是对 Discord webhook API 的映射。

static public function Send(
        string $webhookurl,
        string $content = null,
        string $username = null,
        string $avatar_url = null,
        bool $tts = false,
        array $embeds = null,
        string &$response = null)

注意:tts 是语音到文本。

生成嵌入消息

您可以使用此函数创建一个数组以生成嵌入消息

$embed = DiscordWebhook::GenerateEmbed(title: "test", description: "test embeded message");
$embeds = [$embed];

将嵌入消息放入数组是必要的,因为 Discord webhook 文档中要求这样做。通过 webhook 事件可以嵌入的最大消息数为 10。

生成嵌入消息的函数遵循 Discord 文档。

static public function GenerateEmbed(
        string $title = null,
        string $type = null,
        string $description = null,
        string $url = null,
        int $timestamp = null,
        int $color= null,
        array $footer= null,
        array $image= null,
        array $thumbnail= null,
        array $video= null,
        array $provider= null,
        array $author= null,
        array $fields= null): array

将来,数组将被 Discord API 对象替换,但仍然保持向后兼容。

示例脚本

您可以在 tests/ 文件夹中找到此示例脚本。

require './vendor/autoload.php';

use LouisGJBertrand\DiscordWebhook\DiscordWebhook;


// Replace with the webhook url
$webhookurl = "https://discord.com/api/webhooks/.../...";


$embed = DiscordWebhook::GenerateEmbed(title: "test", description: "test embeded message");
$embeds = [$embed];

DiscordWebhook::Send($webhookurl, embeds: $embeds, response: $response);

// returns the response from the Discord API
var_dump($response);

注意

此库默认不使用 curl 的 ssl 验证。请将静态变量 DiscordWebhook::$SECURE_CURL_CONNECTION 中的 secure 标志设置为 true 以使用 CURL 的 SSL 验证。

外部资源

1 - composer 文档 2 - Discord Webhook 文档 3 - Discord 嵌入消息文档 4 - Stack Exchange Discord Webhook 原始帖子