-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.js
87 lines (77 loc) · 2.18 KB
/
telegram.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
const errors = require('request-promise/errors');
import { format } from 'date-fns';
import { degreesToCardinal, post } from './utils';
import { BOT_URL, DATE_FORMAT, UPDATE_WEATHER_CALLBACK } from './constants';
export const sendMessage = (chat, text, extra = {}) => {
const url = `${BOT_URL}/sendMessage`;
const body = {
...extra,
chat_id: chat.id,
text,
};
return post(url, body);
};
export const editMessageText = (message, text, extra = {}) => {
const url = `${BOT_URL}/editMessageText`;
const { chat, message_id } = message;
const body = {
...extra,
chat_id: chat.id,
message_id,
text,
};
return post(url, body).catch(errors.StatusCodeError, error => {
// A 400 error code is received when the message
// text and the reply_markup didn't change.
// Only rethrow error if it wasn't because of this.
if (error.statusCode !== 400) {
throw error;
}
});
};
export const answerCallbackQuery = (query, text) => {
const url = `${BOT_URL}/answerCallbackQuery`;
const body = {
callback_query_id: query.id,
text,
};
return post(url, body);
};
export const sendLocation = (chat, location) => {
const url = `${BOT_URL}/sendLocation`;
const body = {
chat_id: chat.id,
...location,
};
return post(url, body);
};
export const sendWeather = (message, weather, editMessage) => {
const {
wind_avg,
wind_max,
wind_min,
wind_direction,
temperature,
datetime,
} = weather;
const cardinal = degreesToCardinal(wind_direction);
let text;
text = `*Viento promedio:* ${wind_avg} nudos\n`;
text += `*Viento máximo:* ${wind_max} nudos\n`;
text += `*Viento mínimo:* ${wind_min} nudos\n`;
text += `*Dirección:* ${cardinal} ${wind_direction}°\n`;
text += `*Temperatura:* ${temperature} °C\n`;
text += `\n_${format(datetime, DATE_FORMAT)}_`;
const extra = {
reply_markup: { inline_keyboard: updateWeatherButton },
parse_mode: 'Markdown',
};
if (editMessage) {
return editMessageText(message, text, extra);
} else {
return sendMessage(message.chat, text, extra);
}
};
const updateWeatherButton = [
[{ text: 'Actualizar', callback_data: UPDATE_WEATHER_CALLBACK }],
];