问题
the function below is supposed to provides the fulfillment to the share_your_phone_number intent. When the intent is invoked, the share your phone number keyboard is displayed for the user in telegram.
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"
}
]
]
}
}
));
}
When I deploy the API in the inline editor, only the "Welcome" string is returned in telegram bot chat. the key board buttons are not displayed.
I need a clue to fix fix this.
回答1:
In creatin the Constructor for Payload object as documented [here]https://dialogflow.com/docs/reference/fulfillment-library/rich-responses#new_payloadplatform_payload, the platform
and payload
parameters are required.
new Payload(platform, payload)
The platform
parameter is a property of WebhookClient object and should be defined as such (agent.SLACK, agent.TELEGRAM etc) assuming the webhookClient was instantiated and stored in agent
Examples:
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/.
For my use-case outlined in the question this is my full solution:
// 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);
});
来源:https://stackoverflow.com/questions/54953346/adding-custom-json-payloads-for-telegram-in-dialogflow-to-get-users-to-share-the