telegram bot

This commit is contained in:
Ben
2023-07-07 11:30:38 -04:00
parent 896125c08b
commit 5a49c83ed3
11 changed files with 1393 additions and 31 deletions
+1 -2
View File
@@ -14,7 +14,7 @@ export class BitcoinRpcService {
private _newBlock$: BehaviorSubject<IMiningInfo> = new BehaviorSubject(undefined);
public newBlock$ = this._newBlock$.pipe(filter(block => block != null));
constructor(configService: ConfigService) {
constructor(private readonly configService: ConfigService) {
const url = configService.get('BITCOIN_RPC_URL');
const user = configService.get('BITCOIN_RPC_USER');
const pass = configService.get('BITCOIN_RPC_PASSWORD');
@@ -65,7 +65,6 @@ export class BitcoinRpcService {
console.log(`BLOCK SUBMISSION RESPONSE: ${res}`);
console.log(hexdata);
console.log(JSON.stringify(res));
process.exit();
} catch (e) {
console.log(`BLOCK SUBMISSION RESPONSE ERROR: ${e}`);
}
+13 -2
View File
@@ -8,6 +8,7 @@ import { ClientService } from '../ORM/client/client.service';
import { BitcoinRpcService } from './bitcoin-rpc.service';
import { BlockTemplateService } from './block-template.service';
import { StratumV1JobsService } from './stratum-v1-jobs.service';
import { TelegramService } from './telegram.service';
@Injectable()
@@ -17,7 +18,8 @@ export class StratumV1Service implements OnModuleInit {
private readonly bitcoinRpcService: BitcoinRpcService,
private readonly blockTemplateService: BlockTemplateService,
private readonly clientService: ClientService,
private readonly clientStatisticsService: ClientStatisticsService
private readonly clientStatisticsService: ClientStatisticsService,
private readonly telegramService: TelegramService
) {
}
@@ -35,7 +37,16 @@ export class StratumV1Service implements OnModuleInit {
const promiseSocket = new PromiseSocket(s);
const client = new StratumV1Client(promiseSocket, new StratumV1JobsService(), this.blockTemplateService, this.bitcoinRpcService, this.clientService, this.clientStatisticsService);
const client = new StratumV1Client(
promiseSocket,
new StratumV1JobsService(),
this.blockTemplateService,
this.bitcoinRpcService,
this.clientService,
this.clientStatisticsService,
this.telegramService
);
const clientCount = await this.clientService.connectedClientCount();
+54
View File
@@ -0,0 +1,54 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { validate } from 'bitcoin-address-validation';
import * as TelegramBot from 'node-telegram-bot-api';
import { TelegramSubscriptionsService } from 'src/ORM/telegram-subscriptions/telegram-subscriptions.service';
@Injectable()
export class TelegramService implements OnModuleInit {
private bot: TelegramBot;
constructor(
private readonly configService: ConfigService,
private readonly telegramSubscriptionsService: TelegramSubscriptionsService
) {
const token: string = this.configService.get('TELEGRAM_BOT_TOKEN');
if (token.length < 1) {
console.log('No Telegram token found');
return;
}
this.bot = new TelegramBot(token, { polling: true });
console.log('Telegram bot init');
}
async onModuleInit(): Promise<void> {
this.bot.onText(/\/subscribe/, async (msg) => {
const address = msg.text.split('/subscribe ')[1];
if (validate(address) == false) {
this.bot.sendMessage(msg.chat.id, "Invalid address.");
return;
}
await this.telegramSubscriptionsService.saveSubscription(msg.chat.id, address);
this.bot.sendMessage(msg.chat.id, "Subscribed!");
});
this.bot.onText(/\/start/, (msg) => {
this.bot.sendMessage(msg.chat.id, "Welcome to the public-pool bot. /subscribe <address> to get notified.");
});
this.bot.on('message', (msg) => {
console.log(msg);
});
}
public async notifySubscribersBlockFound(address: string) {
const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address);
subscribers.forEach(subscriber => {
this.bot.sendMessage(subscriber.telegramChatId, 'You found a block!');
});
}
}