Twitter to Telegram Clone

node v14.14.0
version: master
endpointsharetweet
// https://www.reddit.com/r/ifttt/comments/3qhste/correct_way_to_escape_text/ // https://runkit.com/docs/endpoint // https://birdie0.github.io/discord-webhooks-guide/services/ifttt.html /** {"text": "<<<{{Text}}>>>", "username": "<<<{{UserName}}>>>", "link": "<<<{{LinkToTweet}}>>>", "created_at": "<<<{{CreatedAt}}>>>", "embed": "<<<{{TweetEmbedCode}}>>>" } **/ const runkitExpress = require("@runkit/runkit/express-endpoint/1.0.0"); const app = runkitExpress(module.exports); const bodyParser = require('body-parser'); app.use(bodyParser.json()); const got = require('got') const Telegram = require('telegraf/telegram') const bot = new Telegram(process.env.TELEGRAM_BOT_TOKEN) const channelID = '@ButNothingHappened' // Utils //const addSpaceAfterNextline = (str) => str.replace(/\n/g, "\n ") const filterLinksAndRevert = async (str, twitterLink) => { const twitterLinkList = [/twimg.com/, /twitter.com/, twitterLink] const twitterLinkRegex = /https:\/\/t\.co\/[a-zA-Z0-9]{10}/g const links = str.match(twitterLinkRegex) || [] const originalLinksPromise = await Promise.allSettled(links.map(e => got(e, { followRedirect: false }).then(res => res.headers.location))) const originalLinks = originalLinksPromise.map(e => { if (e.status === 'fulfilled' && e.value) { // It should not be an twitter image: telegram preview will handle that const isMatched = !twitterLinkList.every((reg) => e.value.match(reg) === null) if (isMatched) return "" return e.value } else { console.log(e.reason) return null } }) // null for not resolved, "" for empty, others for replace let formatted = str for (let i = 0; i < links.length; i++) { if (originalLinks[i] !== null) { formatted = formatted.replace(links[i], originalLinks[i]) } } return formatted } // Hide your path with some secret string app.post(`/${process.env.SECRET_PATH}`, async (request, response, next) => { const { text, link } = request.body console.log(text, link) // Example // 第一行\n“第二行”\n抱怨一下 ifttt 的的开发者套装要199刀\n一个链接和😈\nhttps://t.co/jOGikF2wUE let formatted = text // formatted = addSpaceAfterNextline(text) formatted = await filterLinksAndRevert(formatted, link) const template = `${formatted} <a href="${link}">参与讨论</a> powered by #Twitter` console.log(template) bot.sendMessage(channelID, template, { parse_mode: 'html' }).then(res => { // Always close up connection response.status(204).end() }).catch(e => { // Let ifttt knows we are in trouble console.log(e) response.status(500).end() }) })
Loading…

no comments

    sign in to comment