telegram.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. require('dotenv').config();
  2. import TelegramBot from 'node-telegram-bot-api';
  3. import { MemberModel } from './db';
  4. import connect from './db';
  5. import botService from './botService';
  6. import AccountTransferTokens from './commands/transferTokens';
  7. // const typeTelegramBot = process.argv[2];
  8. console.log('================ start ================');
  9. console.log('PRODUCTION', process.env.PRODUCTION);
  10. // console.log('typeTelegramBot', typeTelegramBot);
  11. connect();
  12. /* const telegramBotToken = typeTelegramBot === 'private' ? process.env.TELEGRAM_BOT_TOKEN : process.env.TELEGRAM_GROUP_BOT_TOKEN;
  13. const token =
  14. (process.env.PRODUCTION === 'true'
  15. ? telegramBotToken
  16. : process.env.TELEGRAM_BOT_TEST_TOKEN) || ''; */
  17. const token =
  18. (process.env.PRODUCTION === 'true'
  19. ? process.env.TELEGRAM_BOT_TOKEN
  20. : process.env.TELEGRAM_BOT_TEST_TOKEN) || '';
  21. const groupBotToken = process.env.TELEGRAM_GROUP_BOT_TOKEN;
  22. const bot = new TelegramBot(token, { polling: true });
  23. const groupBot = new TelegramBot(groupBotToken, { polling: true });
  24. bot.on('polling_error', console.log);
  25. groupBot.on('polling_error', console.log);
  26. const setFaucetCommand = {
  27. command: '/faucet',
  28. description: 'get 101 token',
  29. };
  30. bot.setMyCommands([
  31. {
  32. command: '/sethandle',
  33. description: 'set your handle name',
  34. },
  35. {
  36. command: '/lookup',
  37. description: 'return the score of the user',
  38. },
  39. { ...setFaucetCommand },
  40. ]);
  41. groupBot.setMyCommands([{ ...setFaucetCommand }]);
  42. const isPrivate = (message: TelegramBot.Message) =>
  43. message.chat.type === 'private';
  44. const deleteMessage = (_bot, message: TelegramBot.Message) =>
  45. setTimeout(
  46. () => _bot.deleteMessage(message.chat.id, message.message_id.toString()),
  47. 1000 * 60 * 5
  48. );
  49. function init(_bot) {
  50. botService(_bot, {
  51. send: async (message: TelegramBot.Message, text: string) => {
  52. const result = await _bot.sendMessage(message.chat.id, text, {
  53. parse_mode: 'Markdown',
  54. });
  55. if (!isPrivate(message)) {
  56. deleteMessage(_bot, result);
  57. }
  58. return result;
  59. },
  60. commandPrefix: '/',
  61. client: _bot,
  62. getId: (message: TelegramBot.Message) => message.from?.id,
  63. getName: (message: TelegramBot.Message) => message.from?.first_name,
  64. getChatId: (message: TelegramBot.Message) => message.chat.id,
  65. getText: (message: TelegramBot.Message) => message.text,
  66. getDate: (message: TelegramBot.Message) => message.date,
  67. isPrivate,
  68. deleteMessage,
  69. dbId: 'tgId',
  70. log: (...args: any) => console.log('Telegram: ', ...args),
  71. });
  72. }
  73. init(bot);
  74. init(groupBot);
  75. // send notification only once a day
  76. setInterval(async () => {
  77. const transferTokens = new AccountTransferTokens();
  78. const balance = await transferTokens.getBalance();
  79. // console.log('balance', balance);
  80. if (balance.toBigInt() < 1000) {
  81. const dateLastNotify = new Date().getTime() - 1000 * 60 * 60 * 24;
  82. console.log('dateLastNotify', dateLastNotify);
  83. const notifyMembers = await MemberModel.find({
  84. enableNotify: { $lt: dateLastNotify },
  85. });
  86. console.log('notifyMembers', notifyMembers);
  87. notifyMembers.forEach(async (m) => {
  88. await bot.sendMessage(
  89. m.tgId.toString(),
  90. `Bot balance alert! Current balance - ${balance}`
  91. );
  92. await MemberModel.updateOne(
  93. { tgId: m.tgId },
  94. { $set: { lastCommand: '', enableNotify: new Date().getTime() } }
  95. );
  96. });
  97. }
  98. }, 1000 * 60);