Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
node_modules
node_modules
.vercel
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@yotie:registry=https://npm.pkg.github.com
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@


## Example
![screenshot](./docs/screenshot.png)
![screenshot](./docs/screenshot.png)
19 changes: 19 additions & 0 deletions api/_utils/scrape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cheerio from 'cheerio';
import fetch from 'node-fetch';

export default async function scrape(linktree) {
const html = await fetch(linktree);
const htmlString = await html.text();
const $ = cheerio.load(htmlString);
const links = [];

$('.link a').each((_, el) => {
const { linkTitle, linkUrl } = $(el).data();
links.push({
name: linkTitle,
link: linkUrl
});
});

return links;
}
59 changes: 59 additions & 0 deletions api/announce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fetch from 'node-fetch';
import { post } from '@yotie/micron';
import scrape from './_utils/scrape';

const {
LINKTREE,
SLACK_WEBHOOK_URL: slack_webhook
} = process.env;

export default post(async ({ ok, error }) => {
const links = await scrape(LINKTREE);

const buttons = links.map(({ name, link }) => {
return {
type: "section",
text: {
"type": "mrkdwn",
"text": `To Watch on ${name}, click this button 👉🏾`
},
"accessory": {
type: "button",
url: link,
text: {
"type": "plain_text",
"text": "Watch Now",
"emoji": true
},
"value": "click_me_123"
}
}
});

const body = {
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "@here we're going live, make sure to join us 👀."
}
},
...buttons
]
};

console.log('body', JSON.stringify(body));


const slack = await fetch(slack_webhook, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
});

console.log(slack);
if (!slack.ok) return error({ msg: 'something went wrong' });

return ok();
});
9 changes: 9 additions & 0 deletions api/scrape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { get } from '@yotie/micron';
import scrape from './_utils/scrape';

const { LINKTREE } = process.env;

export default get(async ({ ok }) => {
const links = await scrape(LINKTREE)
return ok(links);
});
56 changes: 56 additions & 0 deletions api/slash/live.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fetch from 'node-fetch';
import { Message, Blocks, Elements} from 'slack-block-builder';
import { post } from '@yotie/micron';
import scrape from '../_utils/scrape';

const {
LINKTREE,
SLACK_WEBHOOK_URL: slack_webhook
} = process.env;

export default post(async ({ body, ok, error }) => {
const links = await scrape(LINKTREE);
const buttons = links.map(({ link, name}) =>
Elements.Button().text(name).url(link));

const { text, response_url } = body;
const marker = text.indexOf(' ');
const link = text.slice(0, marker).trim();
const subject = text.slice(marker, text.length).trim();

const message = Message()
.blocks(
Blocks.Section()
.text(`Today we'll be talking about: *${subject}* \n Join the live here: ${link}`),
Blocks.Section()
.text(`Dont forget to also follow us on social media to stay up-to-date on the lastest from the community.`),
Blocks.Divider(),
Blocks.Actions()
.elements(...buttons)
)
.buildToJSON();

const broadcast = await fetch(slack_webhook, {
method: 'POST',
body: message,
headers: { 'Content-Type': 'application/json' },
});
if (!broadcast.ok) {
console.log(await broadcast.json())
return error({ msg: 'something went wrong' });
}


const confirm = Message().text(`We successfully broadcasted the announcement for *${subject}*`)
.asUser().buildToJSON();

const slackConfirm = await fetch(response_url, {
method: 'POST',
body: confirm,
headers: { 'Content-Type': 'application/json' },
});

if (!slackConfirm.ok) return error({ msg: 'something went wrong' });

return ok();
});
13 changes: 3 additions & 10 deletions now.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
{
"version": 2,
"name": "welcome-bot",
"alias": ["tichaiti-welcome-bot.now.sh"],
"builds": [
{ "src": "src/index.js", "use": "@now/node" }
],
"routes": [
{ "src": "/", "dest": "src/index.js" }
],
"env": {
"SLACK_TOKEN": "@slack-token",
"GENERAL_CHANNEL": "C4M75GB39"
"GENERAL_CHANNEL": "C4M75GB39",
"LINKTREE": "https://linktr.ee/tichaiti",
"SLACK_WEBHOOK_URL": "@slack-webhook"
}
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
"license": "ISC",
"dependencies": {
"@slack/web-api": "^5.0.1",
"@yotie/micron": "^2.0.0-rc3",
"cheerio": "^1.0.0-rc.3",
"micro": "^9.3.4",
"micro-dev": "^3.0.0"
"micro-dev": "^3.0.0",
"node-fetch": "^2.6.1",
"slack-block-builder": "^1.6.0",
"slack-message-builder": "^1.2.1"
}
}
Loading