-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram-yield-bot.js
More file actions
203 lines (165 loc) · 6.84 KB
/
telegram-yield-bot.js
File metadata and controls
203 lines (165 loc) · 6.84 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
/**
* X-Money Telegram Yield Alert Bot
* Posts high-yield DeFi opportunities to subscribers
* Monetization: Affiliate links, premium alerts
*/
const TelegramBot = require('node-telegram-bot-api');
const axios = require('axios');
// Bot token - user must set this
const TOKEN = process.env.TELEGRAM_BOT_TOKEN || 'YOUR_BOT_TOKEN_HERE';
// DefiLlama API for yield data
const YIELD_API = 'https://yields.llama.fi/pools';
let bot;
let subscribers = new Set();
// Initialize bot
function initBot() {
if (TOKEN === 'YOUR_BOT_TOKEN_HERE') {
console.log('⚠️ Set TELEGRAM_BOT_TOKEN environment variable');
console.log('Create bot via @BotFather on Telegram');
return null;
}
bot = new TelegramBot(TOKEN, { polling: true });
// Handle /start command
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
subscribers.add(chatId);
bot.sendMessage(chatId,
`🚀 *X-Money Yield Alerts*
Get notified about high-yield DeFi opportunities!
*Commands:*
/yields - Top 5 yield opportunities
/eth - Ethereum yields
/base - Base network yields
/stable - Stablecoin yields
/subscribe - Get daily alerts
/unsubscribe - Stop alerts
💰 *Premium:* Get real-time alerts + exclusive deals
Contact: @xmoney_bot
_Powered by X-Money | Not financial advice_`,
{ parse_mode: 'Markdown' }
);
});
// Handle /yields command
bot.onText(/\/yields/, async (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, '⏳ Fetching latest yields...');
try {
const { data } = await axios.get(YIELD_API);
const top5 = data.data
.filter(p => p.tvlUsd > 100000) // Min $100k TVL
.sort((a, b) => b.apy - a.apy)
.slice(0, 5);
let message = '🔥 *Top 5 Yield Opportunities*\n\n';
top5.forEach((pool, i) => {
message += `${i + 1}. *${pool.symbol}* - ${pool.apy.toFixed(1)}% APY\n`;
message += ` Chain: ${pool.chain} | TVL: $${(pool.tvlUsd / 1e6).toFixed(1)}M\n`;
message += ` Project: ${pool.project}\n\n`;
});
message += '\n💡 *Want real-time alerts?* Upgrade to Premium!';
bot.sendMessage(chatId, message, { parse_mode: 'Markdown' });
} catch (err) {
bot.sendMessage(chatId, '❌ Error fetching yields. Try again later.');
}
});
// Handle /stable command
bot.onText(/\/stable/, async (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, '⏳ Fetching stablecoin yields...');
try {
const { data } = await axios.get(YIELD_API);
const stable = data.data
.filter(p =>
p.tvlUsd > 500000 &&
(p.symbol.includes('USDC') || p.symbol.includes('USDT') || p.symbol.includes('DAI'))
)
.sort((a, b) => b.apy - a.apy)
.slice(0, 5);
let message = '💵 *Top Stablecoin Yields*\n\n';
stable.forEach((pool, i) => {
message += `${i + 1}. *${pool.symbol}* - ${pool.apy.toFixed(1)}% APY\n`;
message += ` Chain: ${pool.chain} | TVL: $${(pool.tvlUsd / 1e6).toFixed(1)}M\n\n`;
});
bot.sendMessage(chatId, message, { parse_mode: 'Markdown' });
} catch (err) {
bot.sendMessage(chatId, '❌ Error fetching yields.');
}
});
// Handle /eth command
bot.onText(/\/eth/, async (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, '⏳ Fetching Ethereum yields...');
try {
const { data } = await axios.get(YIELD_API);
const eth = data.data
.filter(p => p.chain === 'Ethereum' && p.tvlUsd > 1000000)
.sort((a, b) => b.apy - a.apy)
.slice(0, 5);
let message = '🔷 *Top Ethereum Yields*\n\n';
eth.forEach((pool, i) => {
message += `${i + 1}. *${pool.symbol}* - ${pool.apy.toFixed(1)}% APY\n`;
message += ` TVL: $${(pool.tvlUsd / 1e6).toFixed(1)}M | ${pool.project}\n\n`;
});
bot.sendMessage(chatId, message, { parse_mode: 'Markdown' });
} catch (err) {
bot.sendMessage(chatId, '❌ Error fetching yields.');
}
});
// Handle /base command
bot.onText(/\/base/, async (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, '⏳ Fetching Base network yields...');
try {
const { data } = await axios.get(YIELD_API);
const base = data.data
.filter(p => p.chain === 'Base' && p.tvlUsd > 100000)
.sort((a, b) => b.apy - a.apy)
.slice(0, 5);
let message = '🔵 *Top Base Network Yields*\n\n';
base.forEach((pool, i) => {
message += `${i + 1}. *${pool.symbol}* - ${pool.apy.toFixed(1)}% APY\n`;
message += ` TVL: $${(pool.tvlUsd / 1e6).toFixed(1)}M | ${pool.project}\n\n`;
});
if (base.length === 0) {
message = 'No Base yields found with >$100k TVL';
}
bot.sendMessage(chatId, message, { parse_mode: 'Markdown' });
} catch (err) {
bot.sendMessage(chatId, '❌ Error fetching yields.');
}
});
console.log('✅ Telegram bot initialized');
return bot;
}
// Daily alert broadcast (for subscribers)
async function broadcastDailyAlert() {
if (!bot || subscribers.size === 0) return;
try {
const { data } = await axios.get(YIELD_API);
const top3 = data.data
.filter(p => p.tvlUsd > 1000000)
.sort((a, b) => b.apy - a.apy)
.slice(0, 3);
let message = '📊 *Daily Yield Alert*\n\n';
top3.forEach((pool, i) => {
message += `${i + 1}. *${pool.symbol}* - ${pool.apy.toFixed(1)}% APY\n`;
});
message += '\nUse /yields for more details';
subscribers.forEach(chatId => {
bot.sendMessage(chatId, message, { parse_mode: 'Markdown' }).catch(() => {});
});
console.log(`📢 Broadcast sent to ${subscribers.size} subscribers`);
} catch (err) {
console.error('Broadcast failed:', err.message);
}
}
// Start the bot
if (require.main === module) {
bot = initBot();
if (bot) {
// Schedule daily alerts at 9 AM UTC
setInterval(broadcastDailyAlert, 24 * 60 * 60 * 1000);
console.log('🤖 X-Money Yield Bot running...');
console.log(`👥 Subscribers: 0`);
}
}
module.exports = { initBot, broadcastDailyAlert, subscribers };