extended-salmon
extended-salmon2y ago

how to capture the user name and number from whatsapp ?

Hey im doing an integration of abot with WhatsApp and voiceglow/voiceflow and i would like to capture the user name and their whatsapp once they initiate the conversation on whatsapp with my bot and transfer them to google sheets Is it Possible to do so with a function ? If so what would be the javascript code ? Is it possible maybe to store it in a variable ?
2 Replies
Tico | Voiceflow Assistant
Yes, it's possible to capture user information such as name and number from WhatsApp and store it in a variable using Voiceflow. You can use the context object provided by the WhatsApp Business API to access the user's phone number. However, please note that WhatsApp does not provide the user's name directly. Here's a basic example of how you might do this:
let userName = 'Default User'; // Default name in case the user's name isn't available
let userNumber = context.message.from; // Access the user's phone number from the context object

// Store the user's name and number in variables
let user = {
name: userName,
number: userNumber
};

// You can then use these variables in your conversation
let userName = 'Default User'; // Default name in case the user's name isn't available
let userNumber = context.message.from; // Access the user's phone number from the context object

// Store the user's name and number in variables
let user = {
name: userName,
number: userNumber
};

// You can then use these variables in your conversation
To transfer this data to Google Sheets, you would need to use the Google Sheets API. Here's a basic example of how you might do this:
const {google} = require('googleapis');

// Authenticate with the Google Sheets API
let auth = new google.auth.GoogleAuth({
keyFile: '/path/to/your/service/account/key.json',
scopes: ['https://www.googleapis.com/auth/spreadsheets']
});

let sheets = google.sheets({version: 'v4', auth});

// Write the user's name and number to a Google Sheet
let request = {
spreadsheetId: 'your-spreadsheet-id',
range: 'Sheet1!A1:B1',
valueInputOption: 'USER_ENTERED',
resource: {
values: [
[user.name, user.number]
]
}
};

sheets.spreadsheets.values.update(request, function(err, response) {
if (err) {
console.error(err);
return;
}

// TODO: Handle the response
});
const {google} = require('googleapis');

// Authenticate with the Google Sheets API
let auth = new google.auth.GoogleAuth({
keyFile: '/path/to/your/service/account/key.json',
scopes: ['https://www.googleapis.com/auth/spreadsheets']
});

let sheets = google.sheets({version: 'v4', auth});

// Write the user's name and number to a Google Sheet
let request = {
spreadsheetId: 'your-spreadsheet-id',
range: 'Sheet1!A1:B1',
valueInputOption: 'USER_ENTERED',
resource: {
values: [
[user.name, user.number]
]
}
};

sheets.spreadsheets.values.update(request, function(err, response) {
if (err) {
console.error(err);
return;
}

// TODO: Handle the response
});
Please replace 'your-spreadsheet-id' with the ID of your Google Sheet, and '/path/to/your/service/account/key.json' with the path to your service account key file. Remember, this is a basic example and might need to be adjusted based on your specific needs and setup. Also, please note that you need to handle user data responsibly and in accordance with WhatsApp's policies and guidelines.
extended-salmon
extended-salmonOP2y ago
@Tico | Voiceflow Assistant could i input this as a functin? Function? Is it possible to input this as a function? @Tico | Voiceflow Assistant

Did you find this page helpful?