channel.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. registerJoystreamTypes()
  12. // Create the API and wait until ready
  13. const api = await ApiPromise.create({
  14. provider: new WsProvider()
  15. })
  16. let lastchannelnotif = await getcurrentChannelId(api)
  17. //let lastchannelnotif = 25
  18. const unsubscribe = await api.rpc.chain.subscribeNewHeads(async (header) => {
  19. const currentchannelid = await getcurrentChannelId(api)
  20. console.log('Latest channelid is :',currentchannelid)
  21. if (currentchannelid>lastchannelnotif) {
  22. const newchannel = []
  23. for (lastchannelnotif+1;lastchannelnotif<currentchannelid;lastchannelnotif++) {
  24. const channel = await (await api.query.contentWorkingGroup.channelById(lastchannelnotif+1)).toJSON()
  25. const channeltitle = channel[0].title
  26. const memberid = channel[0].owner
  27. const channelownerhandler = await getmemberHandle(api, memberid)
  28. console.log(`Posting channel id: ${lastchannelnotif+1} to Telegram`)
  29. newchannel.push(`<b>New channel id created:</b>${lastchannelnotif+1}\r\n<b>Channel Title:</b><a href="https://testnet.joystream.org/#/media/channels/${lastchannelnotif+1}"> ${channeltitle}</a>\r\n<b>Member ID:</b> ${memberid}\r\n<b>Member Handler:</b> <a href="https://testnet.joystream.org/#/members/${channelownerhandler}">${channelownerhandler}</a>`)
  30. }
  31. bot.sendMessage(chatid, newchannel.join("\r\n\r\n"), { parse_mode: 'html' })
  32. }
  33. })
  34. }
  35. const getmemberHandle = async (api,memberid) => {
  36. const memberprofile = await api.query.members.memberProfile(memberid)
  37. const handler = memberprofile.raw.handle.toJSON()
  38. return handler
  39. }
  40. const getcurrentChannelId = async (api) => {
  41. const nextchannelid = await api.query.contentWorkingGroup.nextChannelId()
  42. const currentchannelid = nextchannelid.toNumber()-1
  43. return currentchannelid
  44. }
  45. main()