discord/interactions

实现 Discord Interactions API 的实用工具

2.2.0 2022-02-09 17:58 UTC

This package is not auto-updated.

Last update: 2024-09-19 06:47:10 UTC


README

在实现 Discord Interactions webhook 时可能用到的类型和辅助函数。

安装

packagist 安装

composer require discord/interactions

验证请求签名需要安装 simplito/elliptic-php 包,该包需要启用 php-gmp 扩展

composer require simplito/elliptic-php

用法

使用 InteractionTypeInteractionResponseType 来解析和响应 webhook。

使用 InteractionResponseFlags 使你的响应变得特殊。

使用 verifyKey 检查请求签名。注意您必须先安装 simplito/elliptic-php 包。例如

use Discord\Interaction;
use Discord\InteractionResponseType;

$CLIENT_PUBLIC_KEY = getenv('CLIENT_PUBLIC_KEY');

$signature = $_SERVER['HTTP_X_SIGNATURE_ED25519'];
$timestamp = $_SERVER['HTTP_X_SIGNATURE_TIMESTAMP'];
$postData = file_get_contents('php://input');

if (Interaction::verifyKey($postData, $signature, $timestamp, $CLIENT_PUBLIC_KEY)) {
  echo json_encode(array(
    'type' => InteractionResponseType::PONG
  ));
} else {
  http_response_code(401);
  echo "Not verified";
}