-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (127 loc) · 4.35 KB
/
index.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
import 'dotenv/config'
import { MongoClient } from 'mongodb'
import TelegramApi from 'node-telegram-bot-api'
import axios from 'axios'
const { BOT_TOKEN, MONGODB_URI, WEATHER_API, STICKER } = process.env
const client = new MongoClient(MONGODB_URI)
client.connect()
const db = client.db('weather-bot')
const users = db.collection('users')
const bot = new TelegramApi(BOT_TOKEN, { polling: true })
const translit = word => {
let answer = ''
let converter = {
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd',
'е': 'e', 'ё': 'e', 'ж': 'zh', 'з': 'z', 'и': 'i',
'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n',
'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't',
'у': 'u', 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch',
'ш': 'sh', 'щ': 'sch', 'ь': '', 'ы': 'y', 'ъ': '',
'э': 'e', 'ю': 'yu', 'я': 'ya',
'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D',
'Е': 'E', 'Ё': 'E', 'Ж': 'Zh', 'З': 'Z', 'И': 'I',
'Й': 'Y', 'К': 'K', 'Л': 'L', 'М': 'M', 'Н': 'N',
'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T',
'У': 'U', 'Ф': 'F', 'Х': 'H', 'Ц': 'C', 'Ч': 'Ch',
'Ш': 'Sh', 'Щ': 'Sch', 'Ь': '', 'Ы': 'Y', 'Ъ': '',
'Э': 'E', 'Ю': 'Yu', 'Я': 'Ya'
}
for (let i = 0; i < word.length; ++i ) {
if (converter[word[i]] == undefined){
answer += word[i]
} else {
answer += converter[word[i]]
}
}
return answer
}
bot.on('message', async msg => {
const text = msg.text
const chatId = msg.chat.id
if (text === '/start') {
await bot.sendSticker(chatId, STICKER)
await bot.sendMessage(chatId,
`👋 Привет ${msg.from.first_name}${(msg.from.last_name === undefined) ? '': ` ${msg.from.last_name}`}!\n` +
'🌤 Это погодный бот.\n' +
'👨💻 Автор: @SmartMainnet'
)
await bot.sendMessage(chatId,
'Отправь мне название города,\n' +
'например Москва,\n' +
'чтобы узнать его погоду.'
)
await users.findOne({ id: chatId }).then(async res => {
if (!res) {
await users.insertOne({
id: chatId,
username: msg.from.username,
first_name: msg.from.first_name,
last_name: msg.from.last_name,
start_date: new Date()
})
}
})
} else {
await users.findOne({ id: chatId }).then(async res => {
if (!res) {
await users.insertOne({
id: chatId,
username: msg.from.username,
first_name: msg.from.first_name,
last_name: msg.from.last_name,
start_date: new Date()
})
}
})
try {
let city = translit(text)
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&lang=ru&units=metric&appid=${WEATHER_API}`
await axios.get(url).then(async res => {
await bot.sendMessage(chatId,
`☁️ Погода: ${res.data.weather[0].description}\n` +
`☀️ Температура: ${Math.round(res.data.main.temp)}°C\n` +
`💨 Ветер: ${(res.data.wind.speed / 3.6).toFixed(1)} м/с\n` +
`💧 Влажность: ${res.data.main.humidity}%\n`
)
})
await users.updateOne({ id: chatId },
{
$set: {
username: msg.from.username,
first_name: msg.from.first_name,
last_name: msg.from.last_name,
date_last_call: new Date(),
last_call: text
},
$inc: { number_calls: 1 },
$push: {
calls: {
call: text,
date: new Date()
}
}
}
)
} catch {
await bot.sendMessage(chatId, 'Это не город!')
await users.updateOne({ id: chatId },
{
$set: {
username: msg.from.username,
first_name: msg.from.first_name,
last_name: msg.from.last_name,
date_last_bad_call: new Date(),
last_bad_call: text
},
$inc: { number_bad_calls: 1 },
$push: {
bad_calls: {
call: text,
date: new Date()
}
}
}
)
}
}
})