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 e6344f9
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 68 deletions.
54 changes: 54 additions & 0 deletions __tests__/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { get_payment_details } = require('./../commands');
const { get_user_limits } = require('./../deezy');
const { satoshi_to_BTC } = require('./../utils');

jest.mock('./../deezy');
jest.mock('./../utils');

describe('get_payment_details', () => {
it('should return payment details', async () => {
get_user_limits.mockResolvedValue({
payment_address: 'test_address',
amount: '100',
days: '30',
subscription_cost: '200',
one_time_cost: '300',
user_volume: '400',
});

satoshi_to_BTC.mockImplementation((value) => value);

const result = await get_payment_details();

expect(result).toEqual({
payment_details: `
Your current limits and payment info:
BTC Volume Permitted Every 30 Days: 100.
Subscription Cost: 200.
Cost to purchase 1 additional BTC in scan volume: 300 satoshis.
You have scanned 400 BTC so far this billing period.
Payment Address:
`,
payment_address: 'test_address',
});
});


it('should return empty string when payment_address is empty', async () => {
get_user_limits.mockResolvedValue({
payment_address: '...',
amount: '100',
days: '30',
subscription_cost: '200',
one_time_cost: '300',
user_volume: '400',
});

satoshi_to_BTC.mockImplementation((value) => value);

const result = await get_payment_details();

expect(result).toBe("");
});
});
59 changes: 59 additions & 0 deletions __tests__/deezy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const axios = require('axios');
const deezy = require('./../deezy');
jest.mock('axios');

describe('deezy', () => {

beforeEach(() => {
process.env.DEEZY_API_KEY = "00000000000000000000000000000000"
});

afterEach(() => {
jest.clearAllMocks();
});

it('should throw an error when DEEZY_API_KEY is not set', async () => {
delete process.env.DEEZY_API_KEY;

await expect(deezy.post_scan_request({ utxo: 'mockUtxo' })).rejects.toThrow('DEEZY_API_KEY must be set');
await expect(deezy.get_scan_request({ scan_request_id: 'mockId' })).rejects.toThrow('DEEZY_API_KEY must be set');
await expect(deezy.get_user_limits()).rejects.toThrow('DEEZY_API_KEY must be set');
});

it('should throw an error when RARE_SAT_ADDRESS is not set', async () => {
delete process.env.RARE_SAT_ADDRESS;

await expect(deezy.post_scan_request({ utxo: 'mockUtxo' })).rejects.toThrow('RARE_SAT_ADDRESS must be set');
});

it('should post a scan request', async () => {
process.env.RARE_SAT_ADDRESS = "bc1qknfqya5asgwgkl5de36wqvcg65vsrvvs3x0fy0"
const mockResponse = { data: 'mockData' };
axios.post.mockResolvedValue(mockResponse);

const result = await deezy.post_scan_request({ utxo: 'mockUtxo' });

expect(axios.post).toHaveBeenCalled();
expect(result).toEqual(mockResponse.data);
});

it('should get a scan request', async () => {
const mockResponse = { data: 'mockData' };
axios.get.mockResolvedValue(mockResponse);

const result = await deezy.get_scan_request({ scan_request_id: 'mockId' });

expect(axios.get).toHaveBeenCalled();
expect(result).toEqual(mockResponse.data);
});

it('should get user limits', async () => {
const mockResponse = { data: 'mockData' };
axios.get.mockResolvedValue(mockResponse);

const result = await deezy.get_user_limits();

expect(axios.get).toHaveBeenCalled();
expect(result).toEqual(mockResponse.data);
});
});
39 changes: 39 additions & 0 deletions __tests__/telegram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
process.env.TELEGRAM_BOT_ENABLED = 'true';
process.env.TELEGRAM_BOT_TOKEN = 'test_token';

const telegram = require('./../telegram');
const TelegramBot = require('node-telegram-bot-api');

jest.mock('node-telegram-bot-api');
jest.mock('./../commands');

describe('telegram', () => {
let mockBot;

beforeEach(() => {
mockBot = {
deleteMyCommands: jest.fn(),
setMyCommands: jest.fn(),
onText: jest.fn(),
sendMessage: jest.fn(),
};
TelegramBot.mockImplementation(() => mockBot);
process.env.TELEGRAM_BOT_TOKEN = 'test_token';
process.env.TELEGRAM_CHAT_ID = 'test_chat_id';
});

afterEach(() => {
jest.clearAllMocks();
});

it('should not send a message if TELEGRAM_BOT_ENABLED is false', async () => {
delete process.env.TELEGRAM_BOT_TOKEN;
await telegram.trySendTelegramMessage('test message');
expect(mockBot.sendMessage).not.toHaveBeenCalled();
});

it('should not send a message if message is undefined', async () => {
await telegram.trySendTelegramMessage();
expect(mockBot.sendMessage).not.toHaveBeenCalled();
});
});
39 changes: 39 additions & 0 deletions commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { get_user_limits } = require("./deezy")
const { satoshi_to_BTC } = require("./utils")

const get_payment_details = async () => {
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 === "...") {
return ""
}

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

const payment_details = `
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:
`

return {
payment_details, payment_address
}
}

module.exports = {
get_payment_details
}
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 [email protected] 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
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "node -r dotenv/config index.js",
"setup-telegram": "node -r dotenv/config setup-telegram-bot.js",
"test": "jest",
"hunter:limits": "node -r dotenv/config scripts/get_payment_details",
"coverage": "jest --coverage",
"report": "jest --coverage && open coverage/lcov-report/index.html"
},
Expand Down Expand Up @@ -39,4 +40,4 @@
"devDependencies": {
"jest": "^29.7.0"
}
}
}
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
};
7 changes: 7 additions & 0 deletions scripts/get_payment_details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { get_payment_details } = require('./../commands');

get_payment_details().then(({ payment_details, payment_address }) => {
console.log(payment_details + payment_address);
}).catch(err => {
console.error(err);
});
14 changes: 0 additions & 14 deletions setup-telegram-bot.js

This file was deleted.

Loading

0 comments on commit e6344f9

Please sign in to comment.