finwe/chatfuel

Chatfuel JSON API PHP 封装

dev-master 2019-09-10 11:50 UTC

This package is auto-updated.

Last update: 2024-09-10 22:51:40 UTC


README

https://docs.chatfuel.com/en/articles/735122-json-api

用法

<?php

use Finwe\Chatfuel\JsonApi\JsonApiResponse;
use Finwe\Chatfuel\JsonApi\Message\ImageMessage;
use Finwe\Chatfuel\JsonApi\Message\TextMessage;
use Finwe\Chatfuel\JsonApi\Redirect;
use Finwe\Chatfuel\JsonApi\SetAttribute;

$response = new JsonApiResponse();

$attributes = new SetAttribute();
$attributes->addAttribute('attribute', 'value');

$redirect = new Redirect();
$redirect->addBlock('Bot start');

$response->addMessage(new TextMessage('Hello'))
	->addMessage(new ImageMessage('http://example.com'))
	->setAttributes($attributes)
	->setRedirect($redirect);

header('Content-type: application/json');
echo json_encode($response->getResponse());

可使用的信息类型

Finwe\Chatfuel\JsonApi\Message\TextMessage(string $text)

简单的文本信息。

Finwe\Chatfuel\JsonApi\Message\ImageMessage(string $url)

图片信息。

Finwe\Chatfuel\JsonApi\Message\VideoMessage(string $url)

视频信息。

Finwe\Chatfuel\JsonApi\Message\AudioMessage(string $url)

音频信息。

Finwe\Chatfuel\JsonApi\Message\ButtonMessage(string $text, \Finwe\Chatfuel\JsonApi\Template\Button[] $buttons)

带按钮的文本信息。

也可以通过 addButton(Button $button) 方法添加按钮。

Finwe\Chatfuel\JsonApi\Message\AttachmentMessage(string $type, array $payload)

具有自定义有效载荷的通用信息。更具体的信息类型从这个类扩展。

有效载荷部分类

Finwe\Chatfuel\JsonApi\Template\Button(ButtonType $type, string $text, mixed $specification)

用于向元素下方添加按钮的类。

按钮类型是 Consistence\Enum 实例,例如 ButtonType::get(ButtonType::WEB_URL);。可用类型有 ButtonType::WEB_URLButtonType::JSON_PLUGIN_URLButtonType::SHOW_BLOCK

规范可以是字符串(用于所有类型),也可以是字符串数组(用于 ButtonType::SHOW_BLOCK)。