-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (91 loc) · 3.97 KB
/
index.js
File metadata and controls
114 lines (91 loc) · 3.97 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
/**
* Telegram Horoscope Bot
* User sends zodiac sign (Hindi or English). Bot scrapes horoscope URL and returns plain text.
* Caches horoscope by date+sign; free plan: one rashi check per user per day.
*/
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const { getHoroscope } = require('./serper');
const { resolveRaashi, getSignName } = require('./raashi');
const TELEGRAM_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const SERPER_API_KEY = process.env.SERPER_API_KEY;
const PREMIUM_CONTACT_EMAIL = process.env.PREMIUM_CONTACT_EMAIL;
if (!TELEGRAM_TOKEN || !SERPER_API_KEY || !process.env.SERPER_SCRAPE_URL || !process.env.HOROSCOPE_BASE_URL || !PREMIUM_CONTACT_EMAIL) {
console.error('Missing env: TELEGRAM_BOT_TOKEN, SERPER_API_KEY, SERPER_SCRAPE_URL, HOROSCOPE_BASE_URL, PREMIUM_CONTACT_EMAIL');
process.exit(1);
}
const bot = new TelegramBot(TELEGRAM_TOKEN, { polling: true });
// Cache: "YYYY-MM-DD:sign" -> formatted horoscope text (one fetch per sign per day)
const horoscopeTextCache = new Map();
// Free plan: chatId -> last date (YYYY-MM-DD) we sent a horoscope; one per user per day
const lastHoroscopeSentDate = new Map();
const getTodayDate = () => new Date().toISOString().slice(0, 10);
const getHoroscopeCacheKey = (sign) => `${getTodayDate()}:${sign}`;
const getDisplayName = (chat) => {
const first = chat?.first_name?.trim();
const last = chat?.last_name?.trim();
if (first || last) return [first, last].filter(Boolean).join(' ');
if (chat?.username) return chat.username;
return 'there';
};
const escapeMd = (s) => (s || '').replace(/\*/g, '\\*').replace(/_/g, '\\_');
const PREMIUM_UPSELL_MESSAGE = (displayName) =>
`Hey ${escapeMd(displayName)}, thanks for actively using AstroBaba! You're currently on the free plan, which allows one rashi check per day. To get access to our premium features like:
1) Unlimited rashi checks
2) Convert English to Hindi/Marathi
3) Advanced analytics (love, career, etc.)
Email us at ${escapeMd(PREMIUM_CONTACT_EMAIL)} with the subject "Interested in premium features", and include your name, email and mobile number.`;
const WELCOME = `Welcome. Send your zodiac sign (Hindi or English) to get today's horoscope.`;
const ASK_SIGN = `Examples: मेष, Aries, Mesha, कन्या, Virgo, Leo, सिंह`;
const MAX_MESSAGE_LENGTH = 4096;
const truncateForTelegram = (text) => {
const t = (text || '').trim();
if (t.length <= MAX_MESSAGE_LENGTH) return t;
return t.slice(0, MAX_MESSAGE_LENGTH - 20) + '\n\n…(truncated)';
};
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, `${WELCOME}\n\n${ASK_SIGN}`);
});
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
const text = (msg.text || '').trim();
if (!text || text.startsWith('/')) return;
const resolved = resolveRaashi(text);
if (!resolved) {
await bot.sendMessage(chatId, `I didn't recognise that sign. ${ASK_SIGN}`);
return;
}
const today = getTodayDate();
const lastSent = lastHoroscopeSentDate.get(chatId);
if (lastSent === today) {
await bot.sendMessage(
chatId,
PREMIUM_UPSELL_MESSAGE(getDisplayName(msg.chat)),
{ parse_mode: 'Markdown' },
);
return;
}
lastHoroscopeSentDate.set(chatId, today);
const cacheKey = getHoroscopeCacheKey(resolved.sign);
const cachedText = horoscopeTextCache.get(cacheKey);
if (cachedText) {
await bot.sendMessage(chatId, cachedText);
return;
}
try {
await bot.sendMessage(chatId, `Fetching horoscope for ${getSignName(resolved.sign)}…`);
const plainText = await getHoroscope(SERPER_API_KEY, resolved.sign);
const toSend = truncateForTelegram(plainText);
horoscopeTextCache.set(cacheKey, toSend);
await bot.sendMessage(chatId, toSend);
} catch (err) {
console.error(err);
lastHoroscopeSentDate.delete(chatId);
await bot.sendMessage(
chatId,
'Could not fetch horoscope. Try again later.',
);
}
});
console.log('Horoscope bot is running (Serper + Telegram).');