forked from warp-id/solana-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messaging.ts
121 lines (104 loc) · 3.65 KB
/
messaging.ts
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
import { Telegraf } from "telegraf";
import { InlineKeyboardMarkup, Message } from "telegraf/typings/core/types/typegram";
import { BotConfig } from "./bot";
export class Messaging {
private readonly tg_bot: Telegraf;
constructor(readonly config: BotConfig) {
if (this.config.useTelegram) {
this.tg_bot = new Telegraf(this.config.telegramBotToken);
this.setupBot();
this.tg_bot.launch();
}
}
private setupBot() {
this.tg_bot.on("message", async (ctx) => {
if (!this.checkChatId(ctx)) {
return;
}
if (ctx.text?.toLowerCase().includes("/status")) {
ctx.reply("Working", { parse_mode: "HTML" });
}
if (ctx.text?.toLowerCase().includes("/config")) {
ctx.reply(this.objectToText(this.config, ["wallet", "telegramBotToken", "telegramChatId"]), { parse_mode: "HTML" });
}
if (ctx.text?.toLowerCase().includes("/help")) {
let kb: InlineKeyboardMarkup = {
inline_keyboard: [
[
{ text: 'status', switch_inline_query_current_chat: `/status` },
{ text: 'config', switch_inline_query_current_chat: `/config` },
{ text: 'logs', switch_inline_query_current_chat: `/logs` }
]
]
};
ctx.reply("Available commands", { parse_mode: "HTML", reply_markup: kb });
}
//unstable
// if(ctx.text?.toLowerCase().includes("/logs")){
// const linesOfLog = 150;
// const logFilePath = "./logs/activity.log";
// fs.readFile(logFilePath, 'utf8', (err, data) => {
// if (err) {
// ctx.reply(`Error reading log file: ${err.message}`, { parse_mode: "HTML" });
// } else {
// const lines = data.trim().split(/\r?\n/);
// const lastLines = lines.slice(Math.max(lines.length - linesOfLog, 0)).join('\n');
// const message = `${lastLines}`;
// const chunks = message.match(/[\s\S]{1,4084}/g) || [];
// chunks.forEach((chunk) => {
// ctx.reply(`<code>${chunk}</code>`, { parse_mode: "HTML" });
// });
// }
// });
// }
});
}
private checkChatId(ctx: any): boolean {
if (ctx.chat?.id !== this.config.telegramChatId) {
ctx.reply("fuck off");
return false;
}
return true;
}
private objectToText(obj: object, excludeKeys: string[]): string {
let result = '';
for (const key in obj) {
if (obj.hasOwnProperty(key) && !excludeKeys.includes(key)) {
let value = obj[key];
if (typeof value === 'object' && value !== null) {
continue;
}
result += `${key}: <code>${value}</code>\n`;
}
}
return result;
}
public async sendTelegramMessage(message: string, mint: string, messageId?: number): Promise<Message.TextMessage | undefined> {
if(!this.config.useTelegram){
return null;
}
try {
let kb: InlineKeyboardMarkup = {
inline_keyboard: [
[
{ text: '🍔Dexscreener', url: `https://dexscreener.com/solana/${mint}?maker=${this.config.wallet.publicKey}` },
{ text: 'Rugcheck🔍', url: `https://rugcheck.xyz/tokens/${mint}` }
]
]
};
if (messageId) {
this.tg_bot.telegram.editMessageText(this.config.telegramChatId, messageId, undefined, message, {
parse_mode: "HTML", reply_markup: kb
});
return undefined;
} else {
return await this.tg_bot.telegram.sendMessage(this.config.telegramChatId, message, {
parse_mode: "HTML", reply_markup: kb
});
}
}
catch (e) {
return undefined;
}
}
}