Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·240 lines (220 loc) · 6.92 KB
/
Copy pathmain.js
File metadata and controls
executable file
·240 lines (220 loc) · 6.92 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const { Client, LocalAuth, MessageMedia } = require("whatsapp-web.js");
const { Telegraf } = require("telegraf");
const config = require("./config");
const { Markup } = require("telegraf");
const handleMessage = require("./helpers/handleMessage");
const handleTgBot = require("./helpers/handleTG");
const { database, pmEnabled, cachedData } = require("./db");
const { clean } = require("./session/manage");
const bot = new Telegraf(config.telegramBotToken);
const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
authStrategy: new LocalAuth({ clientId: "whatsbot" }),
});
client.on("auth_failure", () => {
console.error(
"There is a problem in authentication, Kindly set the env var again and restart the app"
);
client.tgbot.telegram.sendMessage(
config.ownerID,
"#ERROR\nThere is a problem in authentication, Kindly set the env var again and restart the app"
);
});
client.on("ready", () => {
console.log("WhatsApp Bot started !");
bot.launch();
console.log("Telegram Bot started!");
});
client.on("message", async (msg) => {
if (msg.body.startsWith("!connect")) {
const whatsAppChat = await msg.getChat();
if (whatsAppChat.isGroup) {
const chatID = msg.body.split("!connect ")[1].trim();
try {
var { conn, coll } = await database("connections");
const data = await coll.findOne({
tgID: chatID,
whatsAppID: whatsAppChat.id._serialized.toString(),
});
if (data) {
return await msg.reply(`Already connected to a chat! ${data.tgID}.`);
}
} catch (error) {
console.log(error);
} finally {
if (conn) {
await conn.close();
}
}
const keyboard = Markup.inlineKeyboard([
Markup.button.callback("Connect", whatsAppChat.id._serialized),
]);
await msg.reply(
"connection request send to telegram. waiting for approval!"
);
client.tgbot.telegram.sendMessage(
chatID,
`${msg.author} requested to connect this chat with ${whatsAppChat.name}`,
keyboard
);
}
}
await handleMessage(msg, client.tgbot, client);
});
client.on("message_revoke_everyone", async (after, before) => {
if (before) {
// TODO handle deleted messages on telegram
// console.log("Deleted message");
}
});
client.on("disconnected", (reason) => {
console.log("Client was logged out", reason);
client.tgbot.telegram.sendMessage(
config.ownerID,
`WhatsApp client was logged out.\nReason ${reason}`
);
});
client.on("qr", (qr) => {
console.log(`Session expired, please generate new session file!\n`);
client.tgbot.telegram.sendMessage(
config.ownerID,
"Session Expired / Not found, please login and setup session again!"
);
clean();
process.exit();
});
// tg bot commands
bot.command("connect", (ctx) => {
if (["group", "supergroup"].includes(ctx.chat.type)) {
ctx.reply(
`Send <code>!connect ${ctx.chat.id}</code> in your WhatsApp group.`,
{ parse_mode: "HTML" }
);
}
});
bot.on("callback_query", async (ctx) => {
if (ctx.callbackQuery.from.id.toString() == config.ownerID.toString()) {
const queryData = ctx.callbackQuery.data;
let waChatID, tgID, chatTitle;
if (queryData.startsWith("pm")) {
waChatID = queryData.split(" ")[1].trim();
let value;
if (queryData.startsWith("pmEnable")) {
value = "true";
} else {
value = "false";
}
try {
var { conn, coll } = await database("pmlog");
await coll.updateOne(
{ chatID: waChatID },
{ $set: { status: value } },
{ upsert: true }
);
pmEnabled.set(waChatID, value);
} catch (error) {
console.log(error);
} finally {
if (conn) {
await conn.close();
}
}
if (value == "true") {
await client.sendMessage(
waChatID,
`this chat is connected now with telegram!`
);
ctx.answerCbQuery("Successfully Connected!", {
show_alert: true,
});
ctx.editMessageText("successfully connected!");
} else {
ctx.answerCbQuery("connection cancelled!", { show_alert: true });
await ctx.editMessageText("Connection refused successfully!");
}
return;
}
tgID = ctx.callbackQuery.message.chat.id.toString();
chatTitle = ctx.callbackQuery.message.chat.title;
waChatID = queryData;
ctx.answerCbQuery("Successfully Connected!", { show_alert: true });
try {
var { conn, coll } = await database("connections");
await coll.updateOne(
{ tgID: tgID },
{ $set: { whatsAppID: waChatID } },
{ upsert: true }
);
await client.sendMessage(
waChatID,
`this chat is connected with ${chatTitle} on telegram!`
);
cachedData.set(tgID, waChatID);
cachedData.set(waChatID, tgID);
} catch (error) {
console.log(error);
} finally {
if (conn) {
await conn.close();
}
}
ctx.editMessageText("successfully connected!");
}
});
bot.command("disconnect", async (ctx) => {
if (["group", "supergroup"].includes(ctx.chat.type)) {
const chatID = ctx.message.chat.id.toString();
if (ctx.message.from.id.toString() == config.ownerID.toString()) {
try {
var { conn, coll } = await database("connections");
const data = await coll.findOne({
tgID: chatID,
});
if (data) {
ctx.reply(`Disconnected!.`);
await coll.deleteOne({
tgID: chatID,
});
if (cachedData.get(chatID)) {
cachedData.delete(chatID);
}
if (cachedData.get(data.whatsAppID.toString())) {
cachedData.delete(data.whatsAppID);
}
await client.sendMessage(
data.whatsAppID,
"this chat is now disconnected!"
);
} else {
ctx.reply(`no chat found to Disconnect!.`);
}
} catch (error) {
console.log(error);
} finally {
if (conn) {
await conn.close();
}
}
}
}
});
bot.start((ctx) =>
ctx.replyWithMarkdownV2(
`Hey **${ctx.message.from.first_name}**, Welcome\\!\n\nPowered by [TG\\-WhatsApp](https://github.com/subinps/TG-WhatsApp)\\.`,
{
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[{ text: "Repo", url: "https://github.com/subinps/TG-WhatsApp" }],
],
},
}
)
);
bot.on("message", (ctx) => {
// Listen TG Bot messages and take action
handleTgBot(ctx, client, MessageMedia);
});
client.tgbot = bot;
client.initialize();
console.log("Initializing clients.. please wait...");