forum.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { registerJoystreamTypes } = require('@joystream/types');
  2. const { ApiPromise, WsProvider } = require('@polkadot/api');
  3. const TelegramBot = require('node-telegram-bot-api');
  4. // replace the value below with the Telegram token you receive from @BotFather
  5. const token = 'yourowntoken';
  6. // Create a bot that uses 'polling' to fetch new updates
  7. const bot = new TelegramBot(token);
  8. //get chat id here https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
  9. const chatid = 'yourownchat';
  10. async function main () {
  11. // register types before creating the api
  12. registerJoystreamTypes()
  13. // Create the API and wait until ready
  14. const api = await ApiPromise.create({
  15. provider: new WsProvider()
  16. })
  17. //set (initial) lastpostnotification sent by bot,
  18. let lastpostnotif = await getcurrentPostId(api)
  19. let lastcatnotif = await getcurrentCatId(api)
  20. let lastthreadnotif = await getcurrentThreadId(api)
  21. //subscribing to new heads of the chain
  22. const unsubscribe = await api.rpc.chain.subscribeNewHeads(async (header) => {
  23. const currentpost = await getcurrentPostId(api)
  24. const currentcat = await getcurrentCatId(api)
  25. const currentthread = await getcurrentThreadId(api)
  26. const block = header.number.toNumber()
  27. console.log(`Current block: ${block}, Latest postid: ${currentpost}, Latest categoryid: ${currentcat}, Latest threadid:${currentthread}`)
  28. //category forum checking
  29. if (currentcat>lastcatnotif){
  30. for (lastcatnotif+1; lastcatnotif<currentcat; lastcatnotif++){
  31. const categorytitle = await getcategoryTitle(api,lastcatnotif+1)
  32. console.log('Notify category',lastcatnotif+1, 'to Telegram')
  33. bot.sendMessage(chatid, `New category created: <b><a href="https://testnet.joystream.org/#/forum/categories/${lastcatnotif+1}">${categorytitle}</a>, Category ID: ${lastcatnotif+1}</b>`, { parse_mode: 'html' })
  34. }
  35. }
  36. //thread forum checking
  37. if (currentthread>lastthreadnotif){
  38. const newthread = []
  39. for (lastthreadnotif+1; lastthreadnotif<currentthread; lastthreadnotif++){
  40. const threadid = await api.query.forum.threadById(lastthreadnotif+1)
  41. const threadtitle = threadid.title
  42. const currentcategory = threadid.category_id
  43. const categorytitle = await getcategoryTitle(api,currentcategory)
  44. const authoraddress = threadid.author_id.toJSON()
  45. const memberraw = await api.query.members.memberIdsByRootAccountId(authoraddress)
  46. const memberid = memberraw[0].toNumber()
  47. const handler = await getmemberHandle(api, memberid)
  48. console.log('Notify thread',lastthreadnotif+1, 'to Telegram')
  49. //sent to array
  50. newthread.push(`New thread created: <a href="https://testnet.joystream.org/#/forum/threads/${lastthreadnotif+1}">"${threadtitle}"</a> by <a href="https://testnet.joystream.org/#/members/${handler}">${handler}</a> (id:${memberid}) in category "<a href="https://testnet.joystream.org/#/forum/categories/${currentcategory}">${categorytitle}</a>" `)
  51. }
  52. bot.sendMessage(chatid, newthread.join("\r\n\r\n"), { parse_mode: 'html' })
  53. }
  54. //forum post checking
  55. if (currentpost>lastpostnotif) {
  56. console.log(currentpost-lastpostnotif, ' new posts')
  57. const newpost = []
  58. //loop notification for every new post published since lastnotif
  59. for (lastpostnotif+1; lastpostnotif<currentpost; lastpostnotif++) {
  60. //begin chaining query info
  61. const postbyid = await api.query.forum.postById(lastpostnotif+1)
  62. const postpos = postbyid.nr_in_thread
  63. const message = postbyid.current_text
  64. //limit characters for message on telegram
  65. const excerpt = message.substring(0,100)
  66. const currentthreadid = postbyid.thread_id.toNumber()
  67. const threadid = await api.query.forum.threadById(currentthreadid)
  68. const threadtitle = threadid.title
  69. const currentcategory = threadid.category_id
  70. const categorytitle = await getcategoryTitle(api,currentcategory)
  71. const authoraddress = postbyid.author_id.toJSON()
  72. const memberraw = await api.query.members.memberIdsByRootAccountId(authoraddress)
  73. const memberid = memberraw[0].toNumber()
  74. const handler = await getmemberHandle(api, memberid)
  75. console.log('Notify post',lastpostnotif+1, 'to Telegram')
  76. //sent to array
  77. newpost.push(`🤩<b> New post (id:${lastpostnotif+1}) by <a href="https://testnet.joystream.org/#/members/${handler}">${handler}</a> (id:${memberid}) in category "<a href="https://testnet.joystream.org/#/forum/categories/${currentcategory}">${categorytitle}</a>" at:\r\n<a href="https://testnet.joystream.org/#/forum/threads/${currentthreadid}?replyIdx=${postpos}">"${threadtitle}"</a></b><i>\r\n"${excerpt}..."</i>\r\n`)
  78. }
  79. //console.log(newpost.join("\r\n\r\n"))
  80. bot.sendMessage(chatid, newpost.join("\r\n\r\n"), { parse_mode: 'html' })
  81. //update lastnotif
  82. // lastpostnotif=currentpost
  83. }
  84. });
  85. }
  86. const getcategoryTitle = async (api, categoryid) => {
  87. const category = await api.query.forum.categoryById(categoryid)
  88. const categorytitle = category.title
  89. return categorytitle
  90. }
  91. const getcurrentPostId = async (api) => {
  92. const nextpostid = await api.query.forum.nextPostId()
  93. const currentpostid = nextpostid.toNumber()-1
  94. return currentpostid
  95. }
  96. const getcurrentThreadId = async (api) => {
  97. const nextthreadid = await api.query.forum.nextThreadId()
  98. const currentthreadid = nextthreadid.toNumber()-1
  99. return currentthreadid
  100. }
  101. const getcurrentCatId = async (api) => {
  102. const nextcatid = await api.query.forum.nextCategoryId()
  103. const currentcatid = nextcatid.toNumber()-1
  104. return currentcatid
  105. }
  106. const getmemberHandle = async (api,memberid) => {
  107. const memberprofile = await api.query.members.memberProfile(memberid)
  108. const handler = memberprofile.raw.handle.toJSON()
  109. return handler
  110. }
  111. main()