谜题 / client-sdk
1.3.3
2024-09-25 10:08 UTC
Requires
- php: ^7.4|^8.0|^8.1
Requires (Dev)
- phpunit/phpunit: ^9
README
这个库使您能够轻松地与...
- Riddle API v3
- incoming Riddle 网络钩子(验证并解析它们)
可用的端点可以在这里找到。
安装
要使用此库,您需要安装PHP 7.4或更高版本(8.0 / 8.1)。
使用Composer
当使用Composer时,您只需运行以下命令
composer require riddle/client-sdk
在您的.php文件中,您然后可以包含composer自动加载器
require 'vendor/autoload.php'; // [... your code, e.g. fetching riddles / stats / ...]
不使用Composer
要无包管理器使用它,请下载此存储库,提取它,保留src/
文件夹,并在您的项目中包含Client.php
文件
require 'riddle-client/src/Client.php'; $client = new Riddle\Api\Client('...');
身份验证
当使用API时,您需要使用访问令牌进行身份验证。您可以在您的Riddle账户中创建一个(您必须登录才能查看此页面)。然后您可以将令牌传递给客户端
require 'riddle-client/src/Client.php'; $client = new Riddle\Api\Client('access token');
现在您可以开始使用API了。
示例用法
获取所有谜题
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Service/Riddle.php'; $client = new Riddle\Api\Client('access token'); $riddles = $client->riddle()->list();
获取Riddle的总体统计数据(仅适用于商业和企业计划)
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Service/Stats.php'; $client = new Riddle\Api\Client('access token'); $riddleStats = $client->stats()->fetchRiddleStats('[RIDDLE_ID]');
获取具有标签的团队谜题
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Service/Riddle.php'; $teamId = 123; $tagIds = [2]; // to get tag IDs use the tag list endpoint $client = new Riddle\Api\Client('access token'); $riddles = $client->riddle()->list($teamId, null, $tagIds);
解析网络钩子
require 'riddle-client/src/Webhook/Webhook.php'; try { $webhook = new Riddle\Api\Webhook\Webhook('WveoGBv11xet392jUwPWbmEbicUn13zR'); $payload = $webhook->parse(); \file_put_contents(__DIR__.'/test-webhook.json', \json_encode($payload->getPayload())); // log the webhook payload // now work with the payload $resultId = $webhookResponse->getResultData()['blockId']; // ... } catch (\Throwable $ex) { \file_put_contents(__DIR__.'/exception.txt', $ex->getMessage()); // write to a log file in case of an exception }
注意:网络钩子签名密钥不是必需的 - 如果未提供,则将跳过签名验证。
构建基本谜题
您还可以通过API构建谜题。请注意,此功能目前处于测试版,预计将在未来进行扩展。
请通过riddle.com上的支持聊天或通过电子邮件发送给我们
要使用此功能,请创建PollBuilder
或QuizBuilder
的实例
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Builder/PollBuilder.php'; $client = new Riddle\Api\Client('access token'); $pollBuilder = new Riddle\Api\Builder\PollBuilder($client);
构建投票
当构建投票时,您可以
- 设置标题
- 添加单选问题
- 添加多选问题
- 设置用户在Riddle结束时将看到的单个结果
以下是完整的代码片段
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Builder/PollBuilder.php'; $client = new Riddle\Api\Client('access token'); $pollBuilder = new Riddle\Api\Builder\PollBuilder($client); // configure the poll's settings, such as questions / title / result $pollBuilder ->setTitle('My Riddle Title') ->addSingleChoiceQuestion('What is the answer?', ['Yes', 'No', 'Maybe']) ->addMultipleChoiceQuestion('What are the answers?', ['Yes', 'No', 'Maybe']) ->setResult('Thanks for participating!', 'We will process your answers accordingly.'); // requests the API and returns the built poll $builtPoll = $pollBuilder->build();
构建测验
当构建测验时,您可以
- 设置标题
- 添加单选问题
- 添加多选问题
- 添加尽可能多的结果,每个结果都带有标题、描述和分数范围(最小/最大)
以下是完整的代码片段
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Builder/QuizBuilder.php'; $client = new Riddle\Api\Client('access token'); $quizBuilder = new Riddle\Api\Builder\QuizBuilder($client); // configure the quizz settings, such as questions / title / results $quizBuilder ->setTitle('My Riddle Title') ->addSingleChoiceQuestion('What is the capital of germany?', ['Berlin' => true, 'Munich' => false, 'Hamburg' => false]) ->addMultipleChoiceQuestion('Which words can you use to say "Hello" in German?', ['Hallo' => true, 'Ciao' => false, 'Guten Tag' => true]) ->addResult('Not so good', 'You answered most questions incorrectly.', 0, 50) ->addResult('Well done', 'You answered most questions correctly.', 51, 100); // requests the API and returns the built quiz $builtQuiz = $quizBuilder->build();
注意:生成的Riddles将自动发布。要禁用此功能,必须在build()
方法中传递false
。
$builtQuiz = $quizBuilder->build(false);
使用自定义结果页面(按钮、回答块等)构建Riddles
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Builder/PollBuilder.php'; require 'riddle-client/src/Builder/QuizBuilder.php'; $client = new Riddle\Api\Client('access token'); // custom result page via the ResultPage class $resultPage = (new Riddle\Api\Builder\Objects\ResultPage()) ->addTextBlock('Thank you for participating in our poll!') ->addAnsweredBlocks() ->addMedia('MEDIA_RUL') ->addSocialShare('Share this poll with your friends!', 'Check out this poll', 'This is a poll about colors') ->addButton('Go to our website', 'https://www.riddle.com', true) ; // Adding it to a quiz $quizBuilder = new (new Riddle\Api\Builder\QuizBuilder($client)) // - >... // add other questions, form fields, etc ->addResultPage($resultPage, minPercentage: 0, maxPercentage: 100); // Adding it to a poll (only one result page possible) $pollBuilder = new (new Riddle\Api\Builder\PollBuilder($client)) // - >... // add other questions, form fields, etc ->setResultPage($resultPage);
使用表单构建Riddles
您可以将“制作表单”块添加到Riddle中。这允许您收集用户数据(姓名、电子邮件、电话等)。
require 'riddle-client/src/Client.php'; require 'riddle-client/src/Builder/QuizBuilder.php'; $client = new Riddle\Api\Client('access token'); // custom form field builder via the FormFieldBuilder class $formBuilder = (new Riddle\Api\Builder\Objects\FormFieldBuilder()) ->setTitle('Contact us') ->addNameField('My name field') ->addEmailField('My email field') ->addPhoneField('My phone field') ->addUrlField('My URL field') ->addNumberField('My number field') ->addCountryField('My country field') ->addShortTextField('My short text field') ->addLongTextField('My long text field'); // Adding it to any builder (poll, quiz, etc) $quizBuilder = new (new Riddle\Api\Builder\QuizBuilder($client)) // - >... // add other questions, form fields, etc ->addFormBuilder($formBuilder);
直接操作构建
我们知道此SDK中的当前构建类没有涵盖构建API的所有可能性(有太多!)。
因此,您还可以直接操作构建。
$quizBuilder = new (new Riddle\Api\Builder\QuizBuilder($client)); $rawBuild = $quizBuilder->getRawBuild(); $rawBuild['publish']['isShowcaseEnabled'] = false; // advanced option to disable Riddle showcase (only available embedded on customer page) $quizBuilder->setRawBuild($rawBuild);