-
Notifications
You must be signed in to change notification settings - Fork 1
/
send-survey.js
77 lines (66 loc) · 1.94 KB
/
send-survey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* This code is based on the https://github.com/google-business-communications/nodejs-businessmessages Node.js
* Business Messages client library.
*/
/**
* Edit the values below:
*/
const PATH_TO_SERVICE_ACCOUNT_KEY = './service_account_key.json';
const CONVERSATION_ID = 'EDIT_HERE';
const businessmessages = require('businessmessages');
const uuidv4 = require('uuid/v4');
const {google} = require('googleapis');
// Initialize the Business Messages API
const bmApi = new businessmessages.businessmessages_v1.Businessmessages({});
// Set the scope that we need for the Business Messages API
const scopes = [
'https://www.googleapis.com/auth/businessmessages',
];
// Set the private key to the service account file
const privatekey = require(PATH_TO_SERVICE_ACCOUNT_KEY);
/**
* Creates a new survey to send to a user.
*
* @param {string} conversationId The unique id for this user and agent.
*/
async function sendSurvey(conversationId) {
const authClient = await initCredentials();
// Create the payload for creating a new survey
const apiParams = {
auth: authClient,
parent: 'conversations/' + conversationId,
surveyId: uuidv4(),
resource: {}
};
// Call the message create function using the
// Business Messages client library
bmApi.conversations.surveys.create(apiParams,
{auth: authClient}, (err, response) => {
console.log(err);
console.log(response);
});
}
/**
* Initializes the Google credentials for calling the
* Business Messages API.
*/
async function initCredentials() {
// configure a JWT auth client
const authClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
scopes,
);
return new Promise(function(resolve, reject) {
// authenticate request
authClient.authorize(function(err, tokens) {
if (err) {
reject(false);
} else {
resolve(authClient);
}
});
});
}
sendSurvey(CONVERSATION_ID);