123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- const { registerJoystreamTypes } = require('@joystream/types');
- const { ApiPromise, WsProvider } = require('@polkadot/api');
- const TelegramBot = require('node-telegram-bot-api');
- const token = 'yourowntoken';
- const bot = new TelegramBot(token);
- const chatid = 'yourownchat';
- async function main () {
-
- registerJoystreamTypes()
-
- const api = await ApiPromise.create({
- provider: new WsProvider()
- })
-
- let lastpostnotif = await getcurrentPostId(api)
- let lastcatnotif = await getcurrentCatId(api)
- let lastthreadnotif = await getcurrentThreadId(api)
-
- const unsubscribe = await api.rpc.chain.subscribeNewHeads(async (header) => {
- const currentpost = await getcurrentPostId(api)
- const currentcat = await getcurrentCatId(api)
- const currentthread = await getcurrentThreadId(api)
-
- const block = header.number.toNumber()
- console.log(`Current block: ${block}, Latest postid: ${currentpost}, Latest categoryid: ${currentcat}, Latest threadid:${currentthread}`)
-
-
- if (currentcat>lastcatnotif){
- for (lastcatnotif+1; lastcatnotif<currentcat; lastcatnotif++){
- const categorytitle = await getcategoryTitle(api,lastcatnotif+1)
- console.log('Notify category',lastcatnotif+1, 'to Telegram')
- 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' })
- }
- }
-
-
- if (currentthread>lastthreadnotif){
- const newthread = []
- for (lastthreadnotif+1; lastthreadnotif<currentthread; lastthreadnotif++){
- const threadid = await api.query.forum.threadById(lastthreadnotif+1)
- const threadtitle = threadid.title
- const currentcategory = threadid.category_id
- const categorytitle = await getcategoryTitle(api,currentcategory)
- const authoraddress = threadid.author_id.toJSON()
- const memberraw = await api.query.members.memberIdsByRootAccountId(authoraddress)
- const memberid = memberraw[0].toNumber()
- const handler = await getmemberHandle(api, memberid)
- console.log('Notify thread',lastthreadnotif+1, 'to Telegram')
-
- 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>" `)
- }
- bot.sendMessage(chatid, newthread.join("\r\n\r\n"), { parse_mode: 'html' })
- }
-
-
- if (currentpost>lastpostnotif) {
- console.log(currentpost-lastpostnotif, ' new posts')
- const newpost = []
-
- for (lastpostnotif+1; lastpostnotif<currentpost; lastpostnotif++) {
-
- const postbyid = await api.query.forum.postById(lastpostnotif+1)
- const postpos = postbyid.nr_in_thread
- const message = postbyid.current_text
-
- const excerpt = message.substring(0,100)
- const currentthreadid = postbyid.thread_id.toNumber()
- const threadid = await api.query.forum.threadById(currentthreadid)
- const threadtitle = threadid.title
- const currentcategory = threadid.category_id
- const categorytitle = await getcategoryTitle(api,currentcategory)
- const authoraddress = postbyid.author_id.toJSON()
- const memberraw = await api.query.members.memberIdsByRootAccountId(authoraddress)
- const memberid = memberraw[0].toNumber()
- const handler = await getmemberHandle(api, memberid)
- console.log('Notify post',lastpostnotif+1, 'to Telegram')
-
- 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`)
- }
-
- bot.sendMessage(chatid, newpost.join("\r\n\r\n"), { parse_mode: 'html' })
-
-
- }
-
- });
- }
- const getcategoryTitle = async (api, categoryid) => {
- const category = await api.query.forum.categoryById(categoryid)
- const categorytitle = category.title
- return categorytitle
- }
- const getcurrentPostId = async (api) => {
- const nextpostid = await api.query.forum.nextPostId()
- const currentpostid = nextpostid.toNumber()-1
- return currentpostid
- }
- const getcurrentThreadId = async (api) => {
- const nextthreadid = await api.query.forum.nextThreadId()
- const currentthreadid = nextthreadid.toNumber()-1
- return currentthreadid
- }
- const getcurrentCatId = async (api) => {
- const nextcatid = await api.query.forum.nextCategoryId()
- const currentcatid = nextcatid.toNumber()-1
- return currentcatid
- }
- const getmemberHandle = async (api,memberid) => {
- const memberprofile = await api.query.members.memberProfile(memberid)
- const handler = memberprofile.raw.handle.toJSON()
- return handler
- }
- main()
|