-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad5615c
commit 68ba85c
Showing
6 changed files
with
120 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |