From 68ba85c661c6dd5a0bf3443c121085f7a4b0ba19 Mon Sep 17 00:00:00 2001 From: topether21 Date: Fri, 8 Dec 2023 15:43:22 -0600 Subject: [PATCH] wip: telegram bot --- deezy.js | 2 +- index.js | 10 ++++-- notifications.js | 57 ++++---------------------------- pushover.js | 29 +++++++++++++++++ setup-telegram-bot.js | 41 ----------------------- telegram.js | 76 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 pushover.js delete mode 100644 setup-telegram-bot.js create mode 100644 telegram.js diff --git a/deezy.js b/deezy.js index fa77b05..2e2ba2b 100644 --- a/deezy.js +++ b/deezy.js @@ -1,6 +1,6 @@ const axios = require('axios') -const BASE_URL = 'https://api.deezy.io/v1' +const BASE_URL = 'https://api-testnet.deezy.io/v1' const VALID_SPLIT_TRIGGERS = ['NEVER', 'ALWAYS', 'NO_SATS'] diff --git a/index.js b/index.js index ae2f78b..ed529a4 100644 --- a/index.js +++ b/index.js @@ -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 @@ -176,6 +177,7 @@ async function run() { const unspents = await get_utxos() console.log(`Found ${unspents.length} utxos in wallet.`) const utxos = unspents.concat(bump_utxos) + utxos.push("78fc7160ff046b0d859db32003cce8bdfbc1d6eb676764a7f45bcc0a2fc6fadf:0"); if (utxos.length === 0) { return } @@ -229,6 +231,7 @@ async function run() { const scan_request_id = scan_request_ids[i] console.log(`Checking status of scan request with id: ${scan_request_id}`) const info = await get_scan_request({ scan_request_id }) + console.log(`---> Scan request with id: ${scan_request_id} has status: ${info.status}`) if (info.status === 'FAILED_LIMITS_EXCEEDED') { const { payment_address, @@ -281,7 +284,10 @@ async function run() { } 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}`) diff --git a/notifications.js b/notifications.js index 7ea941e..2104b66 100644 --- a/notifications.js +++ b/notifications.js @@ -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 }; \ No newline at end of file diff --git a/pushover.js b/pushover.js new file mode 100644 index 0000000..96ca6a8 --- /dev/null +++ b/pushover.js @@ -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 +}; \ No newline at end of file diff --git a/setup-telegram-bot.js b/setup-telegram-bot.js deleted file mode 100644 index 5bebe23..0000000 --- a/setup-telegram-bot.js +++ /dev/null @@ -1,41 +0,0 @@ -const TelegramBot = require('node-telegram-bot-api') -const { get_user_limits } = require('./deezy') -if (!process.env.TELEGRAM_BOT_TOKEN) { - throw new Error('You must set TELEGRAM_BOT_TOKEN in the .env file') -} -const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, { polling: true }) - -console.log(`Send a message in Telegram to your bot to receive the TELEGRAM_CHAT_ID`) - -bot.on('message', (msg) => { - const chatId = msg.chat.id - console.log(`Received message from Telegram! Add the following to your .env:`) - console.log(`TELEGRAM_CHAT_ID=${chatId}`) - bot.sendMessage(chatId, `Welcome to Deezy\'s Sat Hunter Bot. Hope you're ready to hunt down some rare sats :)`) -}) - -// Listen for /limits command -// WIP: not working yet -// bot.onText(/\/limits/, async (msg) => { -// const chatId = msg.chat.id -// const { -// payment_address, -// days, -// subscription_cost: _subscription_cost, -// one_time_cost, -// user_volume: _user_volume, -// } = await get_user_limits() - -// const subscription_cost = satoshi_to_BTC(_subscription_cost) -// const user_volume = satoshi_to_BTC(_user_volume) - -// const paymentDetails = ` -// Your current limits and payment info: -// Payment Address: ${payment_address} -// BTC Volume Permitted Every ${days} Days: -// 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. -// ` -// bot.sendMessage(chatId, paymentDetails) -// }) \ No newline at end of file diff --git a/telegram.js b/telegram.js new file mode 100644 index 0000000..f71fbd8 --- /dev/null +++ b/telegram.js @@ -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 +} \ No newline at end of file