-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
215 lines (177 loc) · 5.15 KB
/
bot.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
require('dotenv').config()
const TelegramBot = require('node-telegram-bot-api')
const axios = require('axios')
const { JSDOM } = require('jsdom')
const FormData = require('form-data')
const { TOKEN, HOST, NODE_ENV, API_KEY } = process.env
let bot
if (NODE_ENV === 'production') {
bot = new TelegramBot(TOKEN)
bot.setWebHook(new URL(TOKEN, HOST).href)
} else {
bot = new TelegramBot(TOKEN, { polling: true })
}
console.log(`Bot started in the ${NODE_ENV} mode`)
function ruMobile(item) {
const {
number_current,
def,
code_start,
code_end,
operator,
operator_full,
region,
bdpn,
bdpn_operator,
time,
} = item
return `
📱 ${number_current} 🇷🇺
🗂 ${def} : ${code_start} ➡ ${code_end}
📶 ${operator} ${operator_full}
🌐 ${region}
🔀 ${bdpn ? bdpn_operator : 'Номер не перенесён'}
🕓 UTC${time < 0 ? '' : '+'}${+time.toLocaleString('ru-RU')}
`
}
function uaMobile(item) {
const {
number,
def,
operator,
time,
} = item
return `
📱 ${number} 🇺🇦
🗂 ${def}
📶 ${operator}
🕓 UTC${time < 0 ? '' : '+'}${+time.toLocaleString('ru-RU')}
`
}
function ruFixed(item) {
const {
number,
code,
operator,
operator_full,
region,
city,
time,
} = item
return `
☎ ${number} 🇷🇺
🗂 ${code}
📞 ${operator} ${operator_full}
🌐 ${region}
🏙 ${city}
🕓 UTC${time < 0 ? '' : '+'}${+time.toLocaleString('ru-RU')}
`
}
function otherMobile(item) {
const {
number,
country_code,
city_code,
country,
region,
city,
time,
} = item
return `
📱 ${number} 🏴☠️
🗂 ${country_code} ➡ ${city_code}
🌐 ${country} ${region}
🏙 ${city}
🕓 UTC${time < 0 ? '' : '+'}${+time.toLocaleString('ru-RU')}
`
}
function numbersMessage(numbers) {
return numbers.map(item => {
if (item.number_success) {
switch (item.number_type_str) {
case 'ru_mobile':
return ruMobile(item)
case 'ru_fixed':
return ruFixed(item)
case 'ua_mobile':
return uaMobile(item)
default:
return otherMobile(item)
}
} else {
const { error_code = '', error_message = '' } = item
throw new Error(`⛔️ Ошибка определения номера ${error_code} ${error_message}❗`)
}
})
}
function jsonToMessage(data) {
if (!data.success) {
const { error_code = '', error_message = '' } = data
throw new Error(`⛔️ Ошибка парсинга результата ${error_code} ${error_message}❗`)
}
const { query, quota, numbers } = data
return `✅*${query}*❓
${numbersMessage(numbers).join('\n')}
${quota < 10 ? '\n🆓Оставшаяся квота: ' + quota : ''}
`
}
function formatHtmlToMessage(message1, message2) {
return `${message1}\n${message2}`.replace(/, \//g, '\n').replace(/\//g, '').replace(/:\n/g, ': ')
}
async function tryHtml(query) {
const formData = new FormData()
formData.append('number', query)
const axiosConfig = {
method: 'post',
url: 'https://www.kody.su/check-tel',
headers: formData.getHeaders(),
data: formData
}
const { data } = await axios.request(axiosConfig)
const dom = new JSDOM(data)
const p = dom.window.document.querySelectorAll('p')
const tr = dom.window.document.querySelectorAll('td')
const message1 = Array.from(p).slice(2, -1).map(item => item.textContent).join('\n')
const message2 = Array.from(tr).slice(3, 5).map(item => item.textContent).join('\n')
return `✅*${query}*❓\n${formatHtmlToMessage(message1, message2)}
`
}
async function numberRequest(phoneNumber) {
let data
try {
const res = await axios(`https://www.kody.su/api/v2.1/search.json?q=+${phoneNumber}&key=${API_KEY}`)
data = res.data
} catch (error) {
if (error.response.data.error_code === 'LIMIT_EXCEEDED') return tryHtml(error.response.data.query)
data = error.response.data
}
return jsonToMessage(data)
}
bot.onText(/^\/start$/, async (msg) => {
NODE_ENV === 'production'
? await bot.sendPhoto(msg.chat.id, `${new URL('/defcodesbot.jpg', HOST).href}`)
: await bot.sendMessage(msg.chat.id, '🚀')
await bot.sendMessage(msg.chat.id, `Здравствуйте❗
Это [бот](${HOST}) для проверки телефонных кодов ☎📱.
Для получения информации пришлите мне номер в международном формате, с "➕" или без.
пример:
*+79040000000*`.replace(/([\!\+.-])/g, '\\$1'), {
parse_mode: 'MarkdownV2',
})
})
bot.onText(/^\+*[0-9\(\)-\.\s]+$/, async (msg, match) => {
try {
const phoneNumber = match[0].replace(/\D/g, '')
if (!phoneNumber) {
throw new Error('⛔️ Номер не указан ' + phoneNumber + '❗')
}
const message = await numberRequest(phoneNumber)
await bot.sendMessage(msg.chat.id, message.replace(/([\(\)\!\+.-\{\}])/g, '\\$1').replace(/"/g, '"'), {
parse_mode: 'MarkdownV2',
})
} catch (error) {
await bot.sendMessage(msg.chat.id, `⛔️ Ошибка при обработке запроса❗ ${error.message || ''}`)
console.error(error)
}
})
module.exports = bot