首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Dialogflow中为TELEGRAM添加自定义JSON负载,以使用户共享他们的电话号码

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

Stack Overflow用户
提问于 2019-03-02 06:44:26
回答 2查看 6.1K关注 0票数 2

下面的功能被认为是提供share_your_phone_number意图的实现。当调用意图时,将以电报的形式向用户显示共享您的电话号码键盘。

代码语言:javascript
复制
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“字符串。未显示键盘按钮。

我需要一个线索来解决这个问题。

EN

回答 2

Stack Overflow用户

发布于 2019-03-03 16:36:48

在将Payload对象的构造函数创建为文档here时,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*/});

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

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

代码语言:javascript
复制
    // 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);
    });
票数 3
EN

Stack Overflow用户

发布于 2020-11-27 07:39:23

这是我的结果:

代码语言:javascript
复制
  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"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54953346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档