Использование телеграммы KeyboardButton в Dialogflow для получения номера телефона пользователя
Предполагается, что приведенная ниже функция обеспечивает выполнение намерения 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 во встроенном редакторе, в чате бота телеграммы возвращается только строка "Добро пожаловать". кнопки клавиатуры не отображаются.
Мне нужна подсказка, чтобы исправить это.
0 ответов
При создании объекта "Конструктор для полезной нагрузки", как это описано [здесь] https://dialogflow.com/docs/reference/fulfillment-library/rich-responses, platform
а также payload
параметры обязательны.
new Payload(platform, payload)
platform
Параметр является свойством объекта WebhookClient и должен быть определен как таковой (agent.SLACK, agent.TELEGRAM и т. д.), предполагая, что экземпляр webhookClient был создан и сохранен в agent
Примеры:
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/.
Для моего варианта использования, изложенного в вопросе, это мое полное решение:
// 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);
});
Вот мой результат:
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})
);
}
Также вы должны обновить версию
dialogflow-fulfillment
в package.json до последней версии. Теперь у меня есть такая версия -
"dialogflow-fulfillment": "^0.6.1"