-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
117 lines (90 loc) · 3.43 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
const TelegramBot = require('node-telegram-bot-api');
const config = require("./config/config.json")
const token = envIfUndefined(config.token, "THF_BOT_TOKEN")
const hashtag = envIfUndefined(config.hashtag, "THF_HASHTAG")
const sourceChatId = envIfUndefined(config.sourceChatId, "THF_SOURCE_CHAT_ID")
const targetChatId = envIfUndefined(config.targetChatId, "THF_TARGET_CHAT_ID")
const forwardMessage = envIfUndefined(config.forwardSourceMessage, "THF_SOURCE_CHAT_MESSAGE")
const forwardTargetMessage = envIfUndefined(config.forwardTargetMessage, "THF_TARGET_CHAT_MESSAGE")
preFlightCheck()
// Event handlers
const bot = new TelegramBot(token, {polling: true});
bot.on("polling_error", console.log);
bot.on('message', (msg) => {
const chatId = msg.chat.id;
if (!areSourceAndTargetSet() && isHelpCommand(msg)) {
sendConfigHelp(chatId)
return;
} else if (chatId.toString() !== sourceChatId.toString()){
//console.error("Chat id does not match. \"" + chatId + "\" instead of \"" + sourceChatId + "\".")
return;
}
if (isHelpCommand(msg)){
sendHelp(chatId)
return;
} else if (isForwardRequested(msg)) {
forwardMsg(msg, chatId)
}
});
// Actions
function preFlightCheck(){
if (token === undefined || hashtag === undefined){
console.log("You need to configure a bot token and a hashtag!")
process.exit(1)
}
if (!areSourceAndTargetSet()){
console.log("No source and no target chat defined.")
}
if (areSourceAndTargetSet() && sourceChatId.toString() === targetChatId.toString()){
console.log("Source and target can not be equal.")
process.exit(1)
}
}
async function sendConfigHelp(chatId){
let help = `Hello,
I'm a forwarding bot but am not configured fully yet.
Please configure the source and chat ids you want me to work with!
This chat hat the following id: ${chatId}
Learn more about me here: https://github.com/vspaceone/telegram-hashtag-forward
`
bot.sendMessage(chatId, help);
return;
}
async function sendHelp(chatId){
const me = await bot.getMe();
const sourceChat = await bot.getChat(sourceChatId);
const targetChat = await bot.getChat(targetChatId);
let help = `Hello,
my name is ${me.first_name}.
I'm a forwarding bot and working actively in this chat.
All messages in "${sourceChat.title}" including the hashtag ${hashtag} will be forwarded to "${targetChat.title}".
I'm not capable of doing more than this currently, but I'm hopefully still useful to you :)
Learn more about me here: https://github.com/vspaceone/telegram-hashtag-forward
`
bot.sendMessage(chatId, help);
}
async function forwardMsg(msg, chatId){
if (forwardMessage !== undefined){
bot.sendMessage(chatId, forwardMessage);
}
if (forwardTargetMessage !== undefined){
bot.sendMessage(targetChatId, forwardTargetMessage);
}
bot.forwardMessage(targetChatId, chatId, msg.message_id)
}
// Helpers
function envIfUndefined(val, env){
if (val !== undefined){
return val;
}
return process.env[env]
}
function areSourceAndTargetSet(){
return sourceChatId !== undefined && targetChatId !== undefined
}
function isHelpCommand(msg){
return (msg.text !== undefined && msg.text.startsWith("/help"))
}
function isForwardRequested(msg){
return (msg.text !== undefined && msg.text.includes(hashtag)) || (msg.caption !== undefined && msg.caption.includes(hashtag))
}