-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcs-package-delivery.js
162 lines (137 loc) · 5.58 KB
/
rcs-package-delivery.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const http = require('http');
const Maap = require('rcs-maap-bot');
const axios = require('axios');
const oauth = require('axios-oauth-client');
const port = 3200;
const oauthUrl = 'https://rcsmaapsim.melroselabs.com/oauth2/v1/token';
const maapUrl = 'https://rcsmaapsim.melroselabs.com/rcs/bot/v1';
const botId = 'bot393018';
const clientId = '1410403923657313325';
const clientSecret = '15471668413685411096';
const activeConversations = {};
async function performOAuth() {
const getClientCredentials = oauth.clientCredentials(
axios.create(),
oauthUrl,
clientId,
clientSecret
);
try {
const auth = await getClientCredentials('botmessage');
return auth;
} catch (error) {
console.error('Problem getting access token - check client_id and client_secret', error);
process.exit(1);
}
}
async function sendMessageWelcome(bot, accessToken, userContact) {
resetConversation(userContact);
const suggestions = new Maap.Suggestions();
suggestions.addReply('Track Package', 'TRACK_PACKAGE');
suggestions.addReply('Book Collection', 'BOOK_COLLECTION');
try {
await bot.sendMessage(
userContact,
'Welcome to the chatbot. Do you want to track a package or book a package collection?',
suggestions
);
} catch (error) {
console.error('Problem sending message', error);
process.exit(1);
}
}
async function handleMessage(bot, accessToken, userContact, text) {
const conversation = openConversation(userContact);
switch (conversation.status) {
case 'awaiting_tracking_number':
await trackPackage(text, bot, userContact);
break;
case 'awaiting_package_weight':
conversation.packageDetails.weight = text;
await requestPackageWeight(bot, userContact);
break;
case 'awaiting_pickup_address':
conversation.packageDetails.pickupAddress = text;
await requestPickupAddress(bot, userContact);
break;
case 'awaiting_delivery_address':
conversation.packageDetails.deliveryAddress = text;
await bookPackageCollection(conversation.packageDetails, bot, userContact);
break;
default:
await processDefaultText(bot, accessToken, userContact, text);
}
}
function resetConversation(userContact) {
activeConversations[userContact] = { status: '', packageDetails: {} };
return activeConversations[userContact];
}
function openConversation(userContact) {
if (!activeConversations[userContact]) {
resetConversation(userContact);
}
return activeConversations[userContact];
}
async function requestTrackingNumber(bot, userContact) {
activeConversations[userContact].status = 'awaiting_tracking_number';
await bot.sendMessage(userContact, 'Please enter your tracking number.');
}
async function requestPackageWeight(bot, userContact) {
activeConversations[userContact].status = 'awaiting_package_weight';
await bot.sendMessage(userContact, 'Please enter your package weight.');
}
async function requestPickupAddress(bot, userContact) {
activeConversations[userContact].status = 'awaiting_pickup_address';
await bot.sendMessage(userContact, 'Please enter the pick-up address.');
}
async function requestDeliveryAddress(bot, userContact) {
activeConversations[userContact].status = 'awaiting_delivery_address';
await bot.sendMessage(userContact, 'Please enter the delivery address.');
}
async function trackPackage(trackingNumber, bot, userContact) {
try {
await bot.sendMessage(userContact, `Tracking info for ${trackingNumber}: In transit.`);
await sendMessageWelcome(bot, null, userContact);
} catch (error) {
console.error('Failed to send message', error);
}
}
async function bookPackageCollection(details, bot, userContact) {
try {
await bot.sendMessage(userContact, `Collection booked. Weight: ${details.weight}, Pickup: ${details.pickupAddress}, Delivery: ${details.deliveryAddress}`);
await sendMessageWelcome(bot, null, userContact);
} catch (error) {
console.error('Failed to send message', error);
}
}
async function processDefaultText(bot, accessToken, userContact, text) {
// Handle the default case when the text does not match any commands
if (text === 'TRACK_PACKAGE') {
await requestTrackingNumber(bot, userContact);
} else if (text === 'BOOK_COLLECTION') {
await requestPackageWeight(bot, userContact);
} else {
await sendMessageWelcome(bot, accessToken, userContact);
}
}
performOAuth().then(auth => {
console.log('Access token:', auth.access_token);
const bot = new Maap.Bot({
token: auth.access_token,
api_url: maapUrl,
bot_id: botId
});
bot.on('newUser', async (payload) => {
await openConversation(payload.messageContact.userContact);
await sendMessageWelcome(bot, auth.access_token, payload.messageContact.userContact);
});
bot.on('response', async (payload) => {
await handleMessage(bot, auth.access_token, payload.messageContact.userContact, payload.RCSMessage.suggestedResponse.response.reply.postback.data);
});
bot.on('message', async (payload) => {
await handleMessage(bot, auth.access_token, payload.messageContact.userContact, payload.RCSMessage.textMessage);
});
http.createServer(bot.handleWebhook()).listen(port, () => {
console.log(`Bot listening on port ${port}`);
});
}).catch(error => console.error('Failed to start the bot', error));