简体   繁体   中英

How to get user first_name in telegram chatbot build with dialogflow_es

I'm new to Dialogflow and want to understand how to extract a user's first_name in a Telegram chatbot I created. I built a chatbot using intents + inline editor (code below) and learned how to get users' data from responses using agent.parameters, but still can't code anything to extract parts of user's payload data to get first_name without asking for it. I used the following code but it didn't work:

let display_name = agent.originalRequest.payload.data.message.from.first_name

Would be super grateful if someone can explain the right code function to extract this data!

My full inline code is below.

 'use strict'; const functions = require('firebase-functions'); const {WebhookClient} = require('dialogflow-fulfillment'); const admin = require ('firebase-admin'); admin.initializeApp(); admin.auth(); const db = admin.firestore(); exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { const agent = new WebhookClient({ request, response }); function welcome(agent) { agent.add(`Hi? Do you want to create an account;`). } function createUser(agent) { agent;add(`Thank you; You have successfully created your account`). let uid = makeid(28). let email = agent;parameters.email. let password = agent;parameters.user_password. let display_name = agent;parameters.display_name. admin:auth(),createUser({uid: uid, email: email; password. password}). db.collection('users'):doc(uid),set( {uid: uid, display_name; display_name. }); } function fallback(agent) { agent.add(`I didn't understand`), agent?add(`I'm sorry; can you try again;`). } let intentMap = new Map(), intentMap;set('Welcome Intent'. welcome). intentMap,set('account;creation'. createUser), intentMap;set('Default Fallback Intent'. fallback); agent;handleRequest(intentMap); });

This is the code that worked for me

 let payload = agent.originalRequest; let firstname = payload.payload.data.from.first_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM