-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot_v1.js
More file actions
148 lines (122 loc) · 3.64 KB
/
chatbot_v1.js
File metadata and controls
148 lines (122 loc) · 3.64 KB
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
const express = require("express");
const { WebhookClient } = require("dialogflow-fulfillment");
const { Payload } =require("dialogflow-fulfillment");
const app = express();
const MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var randomstring = require("randomstring");
var user_name="";
app.post("/dialogflow", express.json(), (req, res) => {
const agent = new WebhookClient({
request: req, response: res
});
async function identify_user(agent)
{
const acct_num = agent.parameters.acct_num;
const client = new MongoClient(url);
await client.connect();
const snap = await client.db("chatbot").collection("user_table").findOne({acct_num: acct_num});
if(snap==null){
await agent.add("Re-Enter your account number");
}
else
{
user_name=snap.username;
await agent.add("Welcome "+user_name+"!! \n How can I help you");}
}
function report_issue(agent)
{
var issue_vals={1:"Internet Down",2:"Slow Internet",3:"Buffering problem",4:"No connectivity"};
const intent_val=agent.parameters.issue_num;
var val=issue_vals[intent_val];
var trouble_ticket=randomstring.generate(7);
//Generating trouble ticket and storing it in Mongodb
//Using random module
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("chatbot");
var u_name = user_name;
var issue_val= val;
var status="pending";
let ts = Date.now();
let date_ob = new Date(ts);
let date = date_ob.getDate();
let month = date_ob.getMonth() + 1;
let year = date_ob.getFullYear();
var time_date=year + "-" + month + "-" + date;
var myobj = { username:u_name, issue:issue_val,status:status,time_date:time_date,trouble_ticket:trouble_ticket };
dbo.collection("user_issues").insertOne(myobj, function(err, res) {
if (err) throw err;
db.close();
});
});
agent.add("The issue reported is: "+ val +"\nThe ticket number is: "+trouble_ticket);
}
//trying to load rich response
function custom_payload(agent)
{
var payLoadData=
{
"richContent": [
[
{
"type": "list",
"title": "Internet Down",
"subtitle": "Press '1' for Internet is down",
"event": {
"name": "",
"languageCode": "",
"parameters": {}
}
},
{
"type": "divider"
},
{
"type": "list",
"title": "Slow Internet",
"subtitle": "Press '2' Slow Internet",
"event": {
"name": "",
"languageCode": "",
"parameters": {}
}
},
{
"type": "divider"
},
{
"type": "list",
"title": "Buffering problem",
"subtitle": "Press '3' for Buffering problem",
"event": {
"name": "",
"languageCode": "",
"parameters": {}
}
},
{
"type": "divider"
},
{
"type": "list",
"title": "No connectivity",
"subtitle": "Press '4' for No connectivity",
"event": {
"name": "",
"languageCode": "",
"parameters": {}
}
}
]
]
}
agent.add(new Payload(agent.UNSPECIFIED,payLoadData,{sendAsMessage:true, rawPayload:true }));
}
var intentMap = new Map();
intentMap.set("service_intent", identify_user);
intentMap.set("service_intent - custom - custom", report_issue);
intentMap.set("service_intent - custom", custom_payload);
agent.handleRequest(intentMap);
});//Closing tag of app.post
app.listen(process.env.PORT || 8080);