在Dialogflow中为TELEGRAM添加自定义JSON负载以使用户共享他们的电话号码

时间:2019-03-01 22:44:26

标签: dialogflow-fulfillment

下面的函数应该为share_your_phone_number意图提供实现。 调用该意图后,将以电报方式为用户显示“共享您的电话号码”键盘。

function share_your_phone_number(agent) {
agent.add(`Welcome.`);
agent.add(new Payload("telegram", {
    "text": "Please click on button below to share your number",
    "reply_markup": {
      "one_time_keyboard": true,
      "resize_keyboard": true,
      "keyboard": [
        [
          {
            "text": "Share my phone number",
            "callback_data": "phone",
            "request_contact": true
          }
        ],
        [
          {
            "text": "Cancel",
            "callback_data": "Cancel"
          }
        ]
      ]
    }
  }
 ));
} 

当我在内联编辑器中部署API时,电报机器人聊天中仅返回“ Welcome”字符串。键盘按钮不显示。

我需要解决此问题的线索。

2 个答案:

答案 0 :(得分:1)

在[此处] https://dialogflow.com/docs/reference/fulfillment-library/rich-responses#new_payloadplatform_payload中说明的有效载荷构造函数对象的创建中,需要platformpayload参数。

new Payload(platform, payload)

platform参数是WebhookClient对象的属性,并且假设webhookClient已实例化并存储在agent

中,因此应将其定义为(agent.SLACK,agent.TELEGRAM等)

示例:

agent.add(new Payload(agent.ACTIONS_ON_GOOGLE, {/*your Google payload here*/}); agent.add(new Payload(agent.SLACK, {/*your Slack payload here*/});

agent.add(new Payload(agent.TELEGRAM, {/*your telegram payload here*/});

ref:https://blog.dialogflow.com/post/fulfillment-library-beta/

对于问题中概述的用例,这是我的完整解决方案:

    // See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
    // for Dialogflow fulfillment library docs, samples, and to report issues
    'use strict';

    const functions = require('firebase-functions');
    const {WebhookClient} = require('dialogflow-fulfillment');
    const {Text, Card, Image, Suggestion, Payload} = require('dialogflow-fulfillment'); 
    process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });
    console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
    console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

    function welcome(agent) {
      agent.add(new Payload(agent.TELEGRAM, {

        "text": "Please click on button below to share your number",
        "reply_markup": {
          "one_time_keyboard": true,
          "resize_keyboard": true,
          "keyboard": [
            [
              {
                "text": "Share my phone number",
                "callback_data": "phone",
                "request_contact": true
              }
            ],
            [
              {
                "text": "Cancel",
                "callback_data": "Cancel"
              }
            ]
          ]
        }
       }));
      }

      // Run the proper function handler based on the matched Dialogflow intent name
      let intentMap = new Map();
      intentMap.set('Default Welcome Intent', welcome);
      agent.handleRequest(intentMap);
    });

答案 1 :(得分:0)

这是我的结果:

  function welcome(agent) {
    const payload = {
      "text": "Pick a color",
      "reply_markup": {
        "inline_keyboard": [
          [
            {
              "text": "Yellow",
              "callback_data": "Yellow"
            }
          ],
          [
            {
              "text": "Blue",
              "callback_data": "Blue"
            }
          ]
        ]
      }
    };

    console.log('queryText ' + JSON.stringify(agent.request_.body.queryResult.queryText));
    console.log('displayName ' + JSON.stringify(agent.request_.body.queryResult.intent.displayName)

    agent.add(
      new Payload(agent.TELEGRAM, payload, {rawPayload: false, sendAsMessage: true})
    );
  }

还必须将package.json中的dialogflow-fulfillment版本更新为最新版本。现在我有了这个版本-"dialogflow-fulfillment": "^0.6.1"