Help us understand the problem. What is going on with this article?

api.aiのwebhookで音声データを返す

More than 1 year has passed since last update.

概要

  • api.aiでは単なるテキストだけではなく、Custom Payloadとして音声データや動画、ファイルなどのリッチメッセージを返すことができます。
    • 参考)Rich Messages
    • 全てのintegrationが対応しているわけではないっぽい
  • facebookメッセンジャーでのwebhook時にどういったレスポンスを返せばいいかの備忘録です。

実装

  • というほどのものではないですが、例えば音声データを返したいというときは以下のようなjsonを返す実装をしていきます。
fulfillment
"fulfillment": {
      "messages": [
        {
          "payload": {
            "facebook": {
              "attachment": {
                "payload": {
                  "url": "https://hogehoge/audio/description.mp3"
                },
                "type": "audio"
              }
            }
          },
          "type": 4
        }
      ]
    }
  • Controllerです。何やらFulfillmetをそのまま返すとExceptionが発生してしまうので文字列で返してます。
MainController.java
@Controller
public class MainController {

    private final MainService mainService;

    public MainController(MainService mainService) {
        this.mainService = mainService;
    }

    @ResponseBody
    @PostMapping(value="/webhook")
    public String webhook(HttpServletRequest request) {
        return mainService.createFulfillmentJson();
    }
}
  • json作ります。
  • リッチメッセージに合わせて、ResponseMessageを継承したResponseSpeechResponseImageResponsePayloadなどがそれぞれ用意されています。
MainService.java
@Slf4j
@Service
public class MainService {
    private final Gson gson = GsonFactory.getDefaultFactory().getGson();

    public String createFulfillmentJson() {
        JsonObject urlJson = new JsonObject();
        urlJson.addProperty("url", "https://hogehoge/audio/description.mp3");

        JsonObject payloadJson = new JsonObject();
        payloadJson.add("payload", urlJson);
        payloadJson.addProperty("type", "audio");

        JsonObject attachmentJson = new JsonObject();
        attachmentJson.add("attachment", payloadJson);

        JsonObject facebookJson   = new JsonObject();
        facebookJson.add("facebook", attachmentJson);

        ResponsePayload responsePayload = new ResponsePayload();
        responsePayload.setPayload(facebookJson);

        List<ResponseMessage> messages = Collections.singletonList(responsePayload);

        Fulfillment fulfillment = new Fulfillment();
        fulfillment.setMessages(messages);

        return gson.toJson(fulfillment);
    }
}

* こんな感じで音声データが返されます。
fb-mess.png

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした