This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
172 lines (153 loc) · 5.89 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
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
const TelegramBot = require('node-telegram-bot-api');
const axios = require('axios');
const date = require('date-and-time');
const fs = require('fs');
require('dotenv').config('.env');
const url = process.env.APP_URL || 'https://example.com';
const port = Number(process.env.PORT || 8080);
const token = process.env.CHANNEL_TOKEN;
// Use polling
// const bot = new TelegramBot(token, {polling: true});
// Use Webhook
const bot = new TelegramBot(token, {webHook: {port}});
bot.setWebHook(`${url}/bot${token}`);
// /start
bot.onText(/^\/start(@.*|$)/, function(msg) {
const chatId = msg.chat.id;
const replyMessage = 'Hi, 這是 Justin 拉麵 bot!';
bot.sendMessage(chatId, replyMessage);
});
// /help
bot.onText(/^\/help(@.*|$)/, function(msg) {
const chatId = msg.chat.id;
let replyMessage = 'Justin 請我吃拉麵 bot\n';
replyMessage += '\n';
replyMessage += '/start\n';
replyMessage += '/intro Justin 的集點計畫';
replyMessage += '/poll 開啟投票\n';
replyMessage += '/point Justin 已經累積的點數\n';
replyMessage += '/location 麵屋雞金位置\n';
replyMessage += '/pointrule 麵屋雞金點數規則\n';
replyMessage += '/dev 開發者資訊';
bot.sendMessage(chatId, replyMessage);
});
// /intro
bot.onText(/^\/intro(@.*|$)/, (message) => {
const chatId = message.chat.id;
fs.readFile('./data.json', (err, data) => {
const replyMessage = JSON.parse(data).data.intro;
bot.sendMessage(chatId, replyMessage);
});
});
const processPinedMessage = (message) => {
fs.readFile('./data/db.json', (error, content) => {
if (!content) return;
const data = JSON.parse(content);
const chatId = data.chatId;
bot.unpinChatMessage(chatId);
});
bot.pinChatMessage(message.chat.id, message.message_id);
const pinnedMessageInfo = {
chatId: message.chat.id,
message_id: message.message_id,
};
fs.writeFileSync('./data/db.json', JSON.stringify(pinnedMessageInfo));
};
// /poll
bot.onText(/^\/poll(?:@.*?)?( .*|$)/, function(msg, match) {
const chatId = msg.chat.id;
// @<username> if users have set up their username;
// <first_name> if users have not set up their username.
const userIdentifier = msg.from.username ?
`@${msg.from.username}` :
`${msg.from.first_name} ${msg.from.last_name ?? ''}`.trim();
const time = msg.text?.split(' ')[1] ?? '';
const timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
if (msg.from.username === 'JustinLin099') {
if (!time) bot.sendMessage(chatId, 'Justin, 你忘了填時間');
else if (!time.match(timeRegex)) {
bot.sendMessage(chatId, 'Justin, 這不是時間');
} else {
const today = new Date();
message = `${today.getMonth()+1+'/'+today.getDate()} ${time} 要吃拉麵嗎`;
const sentPollMessage = bot.sendPoll(chatId, message, ['要', '不要'], {
is_anonymous: false,
});
sentPollMessage.then((message) => {
processPinedMessage(message);
});
}
} else {
const replyMessage = `你不是 Justin, 你是 ${userIdentifier}`;
bot.sendMessage(chatId, replyMessage);
}
});
const getPoint = () => {
const data = axios
.get('https://ramen.justinlin.tw/data.json')
.then(function(response) {
return response.data.total;
})
.catch(function(error) {
console.log(error);
});
return data;
};
const getTodayRequiredPoint = (endDay, startDay) => {
return Math.ceil(date.subtract(endDay, startDay).toDays()) * 14;
};
// /point
bot.onText(/^\/point(@.*|$)/, async (message) => {
const chatId = message.chat.id;
const startDay = new Date('2022/9/12');
const endDay = new Date('2022/9/27');
// const today = new Date();
const point = await getPoint();
const todayRequiredPoint = getTodayRequiredPoint(endDay, startDay);
replyMessage = `Justin 目前有 ${point} 點, 完成度 ${Math.floor(
(point / 300) * 100,
)}%\n`;
if (point <= 300) replyMessage += `Justin 到今天應該要有 ${todayRequiredPoint} 點\n`;
if (todayRequiredPoint - point > 0) replyMessage += 'Justin 進度落後了!\n\n';
else if (point >= 300) replyMessage += 'Justin 完成目標了!\n\n';
else replyMessage += 'Justin 目前有達到目標進度!\n\n';
replyMessage += `依照目前進度,只要在一般時間有 ${300 - point} 人,\
或是夜間時段有 ${(300 - point) / 2} 人就可以達到目標了!\n\n`;
replyMessage += '資料由 @gnehs 提供';
bot.sendMessage(chatId, replyMessage);
});
// /location
bot.onText(/^\/location(@.*|$)/, (msg) => {
const chatId = msg.chat.id;
const latitude = 25.04406477400013;
const longitude = 121.53271914128021;
bot.sendMessage(chatId, '這是麵屋雞金的位置!');
bot.sendLocation(chatId, latitude, longitude);
});
// /pointrule
bot.onText(/^\/pointrule(@.*|$)/, (msg) => {
const chatId = msg.chat.id;
let replyMessage = '1.每消費一碗拉麵可獲得點數一點\n';
replyMessage += '2.深夜消費(PM10:00起)點數雙倍送!\n';
replyMessage +=
'(麵屋雞金 新生南路店深夜時段將於近期開放、開放時間請注意麵屋雞金 粉專公告)\n';
replyMessage +=
'3.多人消費可以蓋在同一張點卡上、但是不能多張卡合併點數兌換。\n';
replyMessage += '4.消費當下給予點數、後面不補發。\n';
replyMessage +=
'5.累積的點數、可以在五之神製作所重新開幕後、至【五之神製作所】兌換\n';
replyMessage += '6.集點卡上兌換品項擇一兌換、以點數最高的品項為準\n';
bot.sendMessage(chatId, replyMessage);
});
// /dev
bot.onText(/^\/dev(@.*|$)/, function(msg) {
const chatId = msg.chat.id;
let replyMessage = '開發者資訊\n';
replyMessage += '\n';
replyMessage += 'Web develop by @gnehs\n';
replyMessage += 'https://ramen.justinlin.tw/\n';
replyMessage += '\n';
replyMessage += 'Bot develop by @liaojason2\n';
replyMessage += 'https://github.com/liaojason2/justin-ramen-telegram-bot';
bot.sendMessage(chatId, replyMessage);
});