-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
150 lines (132 loc) · 6.21 KB
/
db.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
let mysql_connection = require("./mysql_connection");
const request = require('request');
let connection = mysql_connection.pool;
let wp_con = mysql_connection.wp_pool;
let wp_portal_id='_17483';
module.exports = {
UpdateWaits: async function (email, status) {
console.log(email, ":", status);
let query = "UPDATE waits SET status = '" + status + "' WHERE email = '" + email + "'";
// console.log(query);
return await connection.query(query);
},
InsertMessage: function (sender_id, receiver_id, message, type) {
let time = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
let query = "insert into w_receive_messages (`event`,`text`,sender_id,receiver_id,`time`) values ('" + type + "','" + message + "'," + sender_id + "," + receiver_id + ",'" + time + "')";
// console.log(query);
connection.query(query);
//send Billing Server
let request_body = {
sender_id: sender_id,
receiver_id: receiver_id, //consultant_id
text: message,
type:type
};
request({
"uri": "https://www.lomago.io/whatsapp/api/users/billingServer",
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log('billingServer message sent!')
console.log(body);
} else {
console.error("Unable to send message:" + err);
}
// console.log(res);
});
},
SendTelegramNotification: function (consultant_id, customer_id) {
let request_body = {
consultant_id: consultant_id,
customer_id: customer_id
};
request({
"uri": "https://www.lomago.io/whatsapp/api/users/telegramNotification",
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log('SendTelegramNotification!')
} else {
console.error("Unable to send message:" + err);
}
// console.log(res);
});
},
LAMOGA_WAF_request: async function (page_id, message, type) { //page_id:facebook_page_id or Telegram chat_id
console.log("LAMOGA_WAF_request",page_id,message,type);
if (page_id==null)
return null;
let query = "select * from LAMOGA_WAF_request"+wp_portal_id+" WHERE customer_phone = '" + page_id + "'";
[rows] = await wp_con.query(query);
if (rows.length > 0) { // found Customer connection
// new message received. save the message and send to admin web codeigniter and Gerd's code
let sender_id = rows[0].user_id;
let receiver_id = rows[0].consultant_id;
this.InsertMessage(sender_id, receiver_id, message, type);
return null;
}
//Check PIN code message
query = "select * from LAMOGA_WAF_request"+wp_portal_id+" WHERE LOCATE(consultant_phone,'" + message + "' )>0 and type='"+type+"'";
[rows] = await wp_con.query(query);
query = "select text from auto_messages"+wp_portal_id+" WHERE type='" + type + "' ";
[reply_rows] = await wp_con.query(query);
if (rows.length >0) { // Customer PIN code received.
query = "UPDATE LAMOGA_WAF_request"+wp_portal_id+" SET customer_phone='" + page_id + "' WHERE id=" + rows[0].id;
await wp_con.query(query);
let reply = reply_rows[2].text; //'Nice! $consultant will contact you.';
reply = reply.replace('$consultant', rows[0].consultant_name);
query="SELECT chatpreis_2,chatpreis_3 from pts_berater_profile"+wp_portal_id+" WHERE ID="+rows[0].consultant_id;
[prices] = await wp_con.query(query);
reply = reply.replace('$fb_price', prices[0].chatpreis_2);
reply = reply.replace('$te_price', prices[0].chatpreis_3);
this.SendTelegramNotification(rows[0].consultant_id, rows[0].user_id);
return reply;
}
return reply_rows[1].text; //'What is your PIN code?'
},
ConsultantTelegram: async function (page_id, message) { //page_id:facebook_page_id or Telegram chat_id
console.log("check",page_id,message);
if (page_id==null)
return null;
let query = "select text from auto_messages"+wp_portal_id+" WHERE type='consultant'";
[auto_messages] = await wp_con.query(query);
let findConsultant = "select * from telegram_contacts WHERE chat_id = '" + page_id + "'";
[rows] = await wp_con.query(findConsultant);
if (message=='/halt'){
if (rows.length == 0) { // Consultant telegram.
return "You are not connected.";
}
query = "UPDATE telegram_contacts SET chat_id='halt' WHERE id=" + rows[0].id;
await wp_con.query(query);
return auto_messages[3].text;//"Ok. You are halted!";
}
if (rows.length > 0) { // Consultant telegram.
return auto_messages[0].text; //"Yes. You were already connected!";
}
//Check PIN code message
query = "select * from telegram_contacts WHERE '" + message + "' like concat('%',pin_code,'%')";
[consultant_rows] = await wp_con.query(query);
if (consultant_rows.length>0){ // Consultant PIN code received.
query = "UPDATE telegram_contacts SET chat_id='" + page_id + "' WHERE id=" + consultant_rows[0].id;
await wp_con.query(query);
return auto_messages[2].text; //now you are connected
}
return auto_messages[1].text; //'What is your PIN code?'
},
GetConsultantName: async function (page_id) {
if (page_id==null)
return '';
let query = "select value from settings WHERE name = 'send_consultant_name'";
[rows] = await wp_con.query(query);
if (rows[0].value!='true')
return '';
query = "select * from LAMOGA_WAF_request"+wp_portal_id+" WHERE customer_phone = '" + page_id + "'";
[rows] = await wp_con.query(query);
if (rows.length != 0) { //PIN code is needed.
return rows[0].consultant_name;
}
return '';
}
};