index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. require('dotenv').config();
  2. const axios = require('axios').default;
  3. const TelegramBot = require('node-telegram-bot-api'),
  4. mongoose = require('mongoose');
  5. require('./db');
  6. const members = mongoose.model('members');
  7. const token = process.env.TELEGRAM_BOT_TOKEN;
  8. const bot = new TelegramBot(token, { polling: true });
  9. let cachedFmInfo = null;
  10. let lastUpdateFmDate = 0;
  11. async function getFmInfo() {
  12. if (!cachedFmInfo || new Date().getTime - lastUpdateFmDate > 1000 * 600) {
  13. console.log('get new fmData');
  14. cachedFmInfo = await axios.get(
  15. 'https://raw.githubusercontent.com/Joystream/founding-members/main/data/fm-info.json'
  16. );
  17. lastUpdateFmDate = new Date().getTime();
  18. }
  19. return cachedFmInfo;
  20. }
  21. bot.on('polling_error', console.log);
  22. bot.setMyCommands([
  23. {
  24. command: '/sethandle',
  25. description: 'set your handle name',
  26. },
  27. {
  28. command: '/lookup',
  29. description: 'return the score of the user',
  30. },
  31. ]);
  32. bot.on('message', async (msg) => {
  33. const chatId = msg.chat.id;
  34. const fromId = msg.from.id;
  35. let member = null;
  36. try {
  37. member = await members.findOne({ tgId: fromId });
  38. } catch (e) {
  39. console.error(e);
  40. bot.sendMessage(chatId, 'Error =( please try later');
  41. }
  42. console.log('message => ', msg);
  43. if (msg.text === '/start') {
  44. console.log('welcome', fromId);
  45. bot.sendMessage(
  46. chatId,
  47. 'Welcome! Using this bot, you can get information about founding members.' +
  48. '\n\nTo view your statistics, you need enter your name via the /sethandle command to save it OR use the command "/lookup handle". ' +
  49. '*Please note that the handle is case sensitive!*.',
  50. /* + '\n\nData is updated every 10 minutes' */ { parse_mode: 'markdown' }
  51. );
  52. }
  53. if (/\/lookup(.*)/.test(msg.text)) {
  54. const match = msg.text.match(/\/lookup (.*)/);
  55. const handle = match ? match[1] : member?.handle;
  56. if (handle) {
  57. const fmInfo = await getFmInfo();
  58. const memberData = fmInfo.data.scores.totalScores.find(
  59. (m) => m.memberHandle === handle
  60. );
  61. if (memberData) {
  62. const memberDataStr =
  63. `Direct Score = *${memberData.totalDirectScore}*\n` +
  64. `Referral Score = *${memberData.totalReferralScore}*\n` +
  65. `Total Score = *${memberData.totalScore}*\n\n` +
  66. memberData.directScores
  67. .map((m, index) => `Period ${index} = *${m}*`)
  68. .join('\n');
  69. bot.sendMessage(chatId, memberDataStr, { parse_mode: 'markdown' });
  70. } else {
  71. bot.sendMessage(
  72. chatId,
  73. `Don't find member ${handle}. Please note that the handle is case sensitive.`
  74. );
  75. }
  76. } else {
  77. bot.sendMessage(chatId, 'Please, set your handle.');
  78. }
  79. }
  80. if (msg.text === '/sethandle') {
  81. if (member === null) {
  82. member = {
  83. tgId: fromId,
  84. date: msg.date,
  85. lastCommand: 'sethandle',
  86. };
  87. await members.create(member);
  88. } else {
  89. await members.updateOne(
  90. { tgId: fromId },
  91. { $set: { lastCommand: 'sethandle' } }
  92. );
  93. }
  94. bot.sendMessage(
  95. chatId,
  96. 'Write your handle. Please note that the handle is case sensitive.'
  97. );
  98. } else if (member && member.lastCommand === 'sethandle') {
  99. await members.updateOne(
  100. { tgId: fromId },
  101. { $set: { lastCommand: null, handle: msg.text } }
  102. );
  103. bot.sendMessage(chatId, 'Good! Now you can get statistics =)');
  104. }
  105. // bot.sendMessage(chatId, 'Sorry! Technical work in progress!');
  106. });