-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_func.js
246 lines (163 loc) · 6.51 KB
/
telegram_func.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {
checkUsersForTGConnection,
findMember,
findPosition,
findQueryResponses,
updateQueryResponse,
addChatExternalApp,
updateStateEdenChat,
} from "./backEnd_api_func.js";
export async function checkIfFirstMessageTGConnection(msgTG) {
const message = msgTG.text;
// if message is only 3 letters
// if all letters of message are numbers
if (message.length == 4 && message.match(/^[0-9]+$/) != null) {
const chatId = msgTG.chat.id;
const username = msgTG.chat.username;
let resCheck = await checkUsersForTGConnection({
authNumberTGMessage: message,
telegramID: username,
telegramChatID: chatId
})
console.log("resCheck = " , resCheck)
let messageBack = "This is not the right code try again please"
if (resCheck._id) {
messageBack = `Hi ${resCheck.name},
Was great chatting to you earlier! I'll inform you of any updates here - also feel free to ask me any type of questions relating to your application. I'll do my best to answer them!
For now, please finalise your application by going back to your application`
}
return {
done: true,
messageBack: messageBack,
}
} else {
return {
done: false,
messageBack: "",
}
}
}
export async function sentBotMessageAndSaveBackend(chatID, message,bot) {
await bot.sendMessage(chatID, message);
await addChatExternalApp({
chatID_TG: chatID,
message: message,
senderRole: "assistant",
})
}
export async function createMessageBasedOnCategory(queryResponse) {
let messageSendRes = ""
if (queryResponse.category == "REJECT_CANDIDATE"){
messageSendRes = `${queryResponse?.question?.content}`
} else if (queryResponse.category == "ACCEPT_CANDIDATE"){
messageSendRes = `${queryResponse?.question?.content}`
} else if (queryResponse.category == "PITCH_POSITION_CANDIDATE"){
messageSendRes = `${queryResponse?.question?.content}`
} else if (queryResponse.category == "ASK_CANDIDATE"){
messageSendRes = `Wow people are looking at your profile, you have a question:
${queryResponse?.question?.content}`
} else {
messageSendRes = `Wow people are looking at your profile, you have a question:
${queryResponse?.question?.content}`
}
return messageSendRes
}
export async function sentMessageBasedOnCategory(chatID, messageSendRes,queryResponse,bot) {
if (!chatID)
return
console.log("queryResponse.category = " , queryResponse.category)
if (queryResponse.category == "REJECT_CANDIDATE"){
await sentBotMessageAndSaveBackend(chatID, messageSendRes,bot);
await sentBotMessageAndSaveBackend(chatID, "Do you want me to help you find a different opportunity, I already know so much about you so it will be easy?",bot);
await updateStateEdenChat({
userID: queryResponse.responder.userID,
positionIDs: [queryResponse.sender.positionID],
categoryChat: queryResponse.category,
})
} else if (queryResponse.category == "ACCEPT_CANDIDATE"){
await sentBotMessageAndSaveBackend(chatID, messageSendRes,bot);
await sentBotMessageAndSaveBackend(chatID, "Incredible job, we will conduct you with details in the next 24-48 hours about your next interview",bot);
await updateStateEdenChat({
userID: queryResponse.responder.userID,
positionIDs: [queryResponse.sender.positionID],
categoryChat: queryResponse.category,
})
} else if (queryResponse.category == "PITCH_POSITION_CANDIDATE"){
await sentBotMessageAndSaveBackend(chatID, messageSendRes,bot);
console.log("-=asdf-as=df-as=f-as=df-asdf-asdf-as=df-s=" )
await updateStateEdenChat({
userID: queryResponse.responder.userID,
positionIDs: [queryResponse.sender.positionID],
categoryChat: queryResponse.category,
})
} else if (queryResponse.category == "ASK_CANDIDATE"){
await sentBotMessageAndSaveBackend(chatID, messageSendRes,bot);
await updateStateEdenChat({
userID: queryResponse.responder.userID,
positionIDs: [queryResponse.sender.positionID],
categoryChat: queryResponse.category,
})
} else {
await sentBotMessageAndSaveBackend(chatID, messageSendRes,bot);
}
}
export async function findChatIDforUser(conduct) {
// conduct = { positionID: "null", userID: '234' },
// or
// conduct = { positionID: "234", userID: 'null' },
let res
if (conduct.positionID != null) {
res = await findPosition({
_id: conduct.positionID
});
if (res?.conduct?.telegramChatID) {
return res.conduct.telegramChatID;
}
} else if (conduct.userID != null) {
res = await findMember({
_id: conduct.userID
});
console.log("res = " , res)
if (res?.conduct?.telegramChatID) {
return res.conduct.telegramChatID;
}
}
}
export async function findIfMessageIsAResponse(chatId,msg) {
let memberData = await findMember({
telegramChatID: chatId
});
let findQueryResponsesRes
if (!memberData) {
let positionData = await findPosition({
telegramChatID: chatId
});
findQueryResponsesRes = await findQueryResponses({
sentFlag: true,
phase: "QUERY",
responderType: "POSITION",
responderID: positionData._id,
});
} else {
console.log("change = -d-d-d-d-d-d" )
findQueryResponsesRes = await findQueryResponses({
sentFlag: true,
phase: "QUERY",
responderType: "USER",
responderID: memberData._id,
});
}
if (findQueryResponsesRes.length > 0) {
let queryResponse = findQueryResponsesRes[0];
console.log("queryResponse = " , queryResponse)
// --------------- update backend that message was sent ----------------
let res = await updateQueryResponse({
_id: queryResponse._id,
sentFlag: false,
answer: msg,
phase: "RESPONDED",
})
console.log("res = " , res)
// --------------- update backend that message was sent ----------------
}
}