Skip to content

Commit

Permalink
wip: telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
topether21 committed Dec 11, 2023
1 parent b890d4d commit 472242a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 67 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const {
const { get_fee_rate } = require('./fees')
const { post_scan_request, get_scan_request, get_user_limits } = require('./deezy')
const { generate_satributes_messages } = require('./satributes')
const { sendNotifications, TELEGRAM_BOT_ENABLED, PUSHOVER_ENABLED } = require('./notifications.js')
const { sendNotifications, PUSHOVER_ENABLED } = require('./notifications.js')
const { TELEGRAM_BOT_ENABLED, initCommands } = require('./telegram.js')
const { get_excluded_tags, get_included_tags, get_min_tag_sizes, sleep, get_tag_by_address, satoshi_to_BTC } = require('./utils.js')

const LOOP_SECONDS = process.env.LOOP_SECONDS ? parseInt(process.env.LOOP_SECONDS) : 10
Expand Down Expand Up @@ -286,7 +287,10 @@ Contact help@deezy.io for questions or to change your plan.
}

async function runLoop() {
if (TELEGRAM_BOT_ENABLED) console.log(`Telegram bot is enabled`)
if (TELEGRAM_BOT_ENABLED) {
console.log(`Telegram bot is enabled`)
await initCommands()
}
if (PUSHOVER_ENABLED) console.log(`Pushover bot is enabled`)
await sendNotifications(`Starting up sat hunter on ${process.env.ACTIVE_EXCHANGE}`)

Expand Down
57 changes: 6 additions & 51 deletions notifications.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,16 @@
const {
PUSHOVER_USER,
PUSHOVER_TOKEN,
PUSHOVER_PRIORITY,
TELEGRAM_BOT_TOKEN,
TELEGRAM_CHAT_ID
} = process.env;

const axios = require('axios');

const TelegramBot = require('node-telegram-bot-api');
const telegramBot = TELEGRAM_BOT_TOKEN ? new TelegramBot(TELEGRAM_BOT_TOKEN) : null;

const PUSHOVER_ENABLED = PUSHOVER_USER && PUSHOVER_TOKEN;
const PUSHOVER_ENDPOINT = 'https://api.pushover.net/1/messages.json';
const PUSHOVER_DEFAULT_PRIORITY = 0;
const TELEGRAM_BOT_ENABLED = telegramBot && TELEGRAM_CHAT_ID;
const TELEGRAM_CHAT_IDS = TELEGRAM_CHAT_ID ? TELEGRAM_CHAT_ID.split(',') : [];

const trySendPushover = async (message = undefined) => {
if(!PUSHOVER_ENABLED || !message) return;
const priority = PUSHOVER_PRIORITY ?? PUSHOVER_DEFAULT_PRIORITY;
const headers = { 'Content-Type': 'application/json' };
await axios.post(PUSHOVER_ENDPOINT, {
token: PUSHOVER_TOKEN,
user: PUSHOVER_USER,
message,
priority
}, { headers }).catch(err => {
console.log(err);
});
};

const trySendTelegram = async (message = undefined) => {
if(!TELEGRAM_BOT_ENABLED || !message) return;
for (const chatId of TELEGRAM_CHAT_IDS) {
let success = false
let retries = 0
while (!success && retries < 5) {
try {
await telegramBot.sendMessage(chatId, message)
success = true
} catch (err) {
console.log(err)
retries++
}
}
}
};
trySendTelegramMessage
} = require('./telegram');

const {
trySendPushover
} = require('./pushover');

const sendNotifications = async (message = undefined) => {
await trySendPushover(message);
await trySendTelegram(message);
await trySendTelegramMessage(message);
};

module.exports = {
PUSHOVER_ENABLED,
TELEGRAM_BOT_ENABLED,
sendNotifications
};
29 changes: 29 additions & 0 deletions pushover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const axios = require('axios');
const {
PUSHOVER_USER,
PUSHOVER_TOKEN,
PUSHOVER_PRIORITY
} = process.env;

const PUSHOVER_ENABLED = PUSHOVER_USER && PUSHOVER_TOKEN;
const PUSHOVER_ENDPOINT = 'https://api.pushover.net/1/messages.json';
const PUSHOVER_DEFAULT_PRIORITY = 0;

const trySendPushover = async (message = undefined) => {
if (!PUSHOVER_ENABLED || !message) return;
const priority = PUSHOVER_PRIORITY ?? PUSHOVER_DEFAULT_PRIORITY;
const headers = { 'Content-Type': 'application/json' };
await axios.post(PUSHOVER_ENDPOINT, {
token: PUSHOVER_TOKEN,
user: PUSHOVER_USER,
message,
priority
}, { headers }).catch(err => {
console.log(err);
});
};

module.exports = {
PUSHOVER_ENABLED,
trySendPushover
};
14 changes: 0 additions & 14 deletions setup-telegram-bot.js

This file was deleted.

76 changes: 76 additions & 0 deletions telegram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const {
TELEGRAM_BOT_TOKEN,
TELEGRAM_CHAT_ID
} = process.env;

const TelegramBot = require('node-telegram-bot-api');
const { get_user_limits } = require('./deezy');
const { satoshi_to_BTC } = require('./utils');
const telegramBot = TELEGRAM_BOT_TOKEN ? new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true }) : null;

const TELEGRAM_BOT_ENABLED = telegramBot && TELEGRAM_CHAT_ID;
const TELEGRAM_CHAT_IDS = TELEGRAM_CHAT_ID ? TELEGRAM_CHAT_ID.split(',') : [];

async function initCommands() {
await telegramBot.deleteMyCommands()
await telegramBot.setMyCommands([{
command: 'limits',
description: 'Get current limits and payment info'
}])
telegramBot.onText(/\/limits/, async (msg) => {

const chatId = msg.chat.id
const {
payment_address = "...",
amount: _amount = "0",
days = "...",
subscription_cost: _subscription_cost = "0",
one_time_cost = "0",
user_volume: _user_volume = "0",
} = await get_user_limits()

if (payment_address === "...") {
await telegramBot.sendMessage(chatId, "You have not set a payment address yet.")
return
}

const subscription_cost = satoshi_to_BTC(_subscription_cost)
const user_volume = satoshi_to_BTC(_user_volume)
const amount = satoshi_to_BTC(_amount)

const paymentDetails = `
Your current limits and payment info:
BTC Volume Permitted Every ${days} Days: ${amount}.
Subscription Cost: ${subscription_cost}.
Cost to purchase 1 additional BTC in scan volume: ${one_time_cost} satoshis.
You have scanned ${user_volume} BTC so far this billing period.
Payment Address:
`
await telegramBot.sendMessage(chatId, paymentDetails, { parse_mode: 'HTML' })
await telegramBot.sendMessage(chatId, payment_address)
});
}
const trySendTelegramMessage = async (message = undefined) => {
if (!TELEGRAM_BOT_ENABLED || !message) return;
for (const chatId of TELEGRAM_CHAT_IDS) {
let success = false
let retries = 0
while (!success && retries < 5) {
try {
await telegramBot.sendMessage(chatId, message)
success = true
} catch (err) {
console.log(err)
retries++
}
}
}
};

module.exports = {
TELEGRAM_BOT_ENABLED,
TELEGRAM_CHAT_IDS,
trySendTelegramMessage,
initCommands
}

0 comments on commit 472242a

Please sign in to comment.