Hello!
Today I decided to make a project that will allow me to post my blog posts on LinkedIn. I have been thinking of doing this for a while but I never got around to doing it. I decided to do it today as I never used the LinkedIn API and RSS and I was very interested in making something from them.
ㅤ | ㅤ |
---|---|
What is RSS?
RSS stands for Really Simple Syndication. It's a web feed that allows users and applications to access updates to websites in a standardized, computer-readable format.
What is LinkedIn?
LinkedIn is a business and employment-oriented social networking service that operates via websites and mobile apps. Launched on May 5, 2003, it is mainly used for professional networking, including employers posting jobs and job seekers posting their CVs.
Now you might ask me why would I want to post my blog posts on LinkedIn.
Sharing your blogs on LinkedIn is one of the best ways to increase traffic and engagement in your brand or business. Also, it's a great way to show what you have been working on and what you are interested in, to potential employers.
For this, I decided to work with Github Actions as I have never used it before and I wanted to learn more about it. I also used the RSS feed from my blog and the LinkedIn API to make this project.
What are Github Actions?
Github Actions is a feature of Github that allows you to automate your workflow. You can create your actions and use them in your workflow. You can also use actions that are already made by the community.
So I decided to make a workflow that will run every Monday at 13:00 UTC and will post my latest blog post to LinkedIn.
Unfortunately, there wasn't an action that was already made that would do this so I had to make my own action.
Now I didn't have any idea how to make a Github Action.
Thankfully there was a guide to make a hello-world action and I followed it and from that made my own action.
The guide can be found here Github Actions Javascript
Now I had an action that would run and print "Hello World" but I needed to make it post my blog post to LinkedIn.
I started by making a function that would get the RSS feed from my blog and get the latest post.
For it, I used the RSS-parser npm package.
const core = require('@actions/core');
const github = require('@actions/github');
const RSSParser = require("rss-parser");
try {
const parse = async url => {
const feed = await new RSSParser().parseURL(url);
console.log(feed.title);
console.log(`${feed.items[0].title} - ${feed.items[0].link}\n${feed.items[0].contentSnippet}\n\n`);
};
const feed_list = core.getInput('feed_list');;
console.log("Parsing " + feed_list);
parse(feed_list);
} catch (error) {
core.setFailed(error.message);
}
Now I had a function that would get the RSS feed from my blog and get the latest post.
Now I needed to make a function that would post the blog post to LinkedIn.
Thankfully there was a project by @gfiocco.
The project can be found here linkedin-post
I used this project as the documentation for the LinkedIn API was very confusing and not very detailed.
I used the project and made a function that would post the blog post to LinkedIn.
const core = require("@actions/core");
const github = require("@actions/github");
const RSSParser = require("rss-parser");
const https = require("https");
// Publish content on LinkedIn
function postShare(
accessToken,
ownerId,
title,
text,
shareUrl,
shareThumbnailUrl
) {
return new Promise((res, rej) => {
let hostname = "api.linkedin.com";
let path = "/v2/shares";
let method = "POST";
let body = {
owner: "urn:li:person:" + ownerId,
subject: title,
text: {
text: text, // max 1300 characters
},
content: {
contentEntities: [
{
entityLocation: shareUrl,
thumbnails: [
{
resolvedUrl: shareThumbnailUrl,
},
],
},
],
title: title,
},
distribution: {
linkedInDistributionTarget: {},
},
};
let headers = {
Authorization: "Bearer " + accessToken,
"cache-control": "no-cache",
"X-Restli-Protocol-Version": "2.0.0",
"Content-Type": "application/json",
"x-li-format": "json",
"Content-Length": Buffer.byteLength(JSON.stringify(body)),
};
_request(method, hostname, path, headers, JSON.stringify(body))
.then((r) => {
res(r);
})
.catch((e) => rej(e));
});
}
So now I had a function that would get the RSS feed from my blog and get the latest post and a function that would post the blog post to LinkedIn!
Now I needed to make a workflow that would run every Monday at 13:00 UTC and would run the action.
I made a workflow that would run every Monday at 13:00 UTC using crontab and would run the action.
What is Crontab?
Crontab is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use crontab to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
name: 📤 Linkedin Blog Post Workflow
on:
schedule:
- cron: '0 13 * * 1'
workflow_dispatch:
jobs:
linkedin_rss_job:
runs-on: ubuntu-latest
name: 📤 Post Latest RSS Post to Linkedin
steps:
- name: 🧱 Checkout
uses: actions/checkout@v3
- name: 📫 Get the Latest Post / Post On Linkedin
uses: Arisamiga/Linkedin-RSS@master
with:
feed_list: ${{ secrets.feed_list }}
ln_access_token: ${{ secrets.ln_access_token }}
embed_image: ${{ secrets.embed_image }}
Now I had a workflow that would run every Monday at 13:00 UTC and would run the action.
Note: When adding Personal Access Tokens or any other private information make sure to add them as secrets.
How to make a Github Secret can be found here Github Secrets
Information on How to get a LinkedIn Access Token and How to use this action can be found in the Actions repository:
⭐ https://github.com/Arisamiga/Linkedin-RSS
I hope you enjoyed this post and Thank you for reading!
Top comments (0)