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 8, 2023
1 parent ad5615c commit 68ba85c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 95 deletions.
2 changes: 1 addition & 1 deletion deezy.js
Original file line number Diff line number Diff line change
@@ -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']

Expand Down
10 changes: 8 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 @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}`)

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
};
41 changes: 0 additions & 41 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 68ba85c

Please sign in to comment.