The provided sample includes:
- Generating GitHub API access tokens via OAuth 2.0.
- Authenticating and connecting to the GitHub API via Linx.
- Get a bot user token in Slack
- Post messages to Slack using Bot User for GitHub commits for a time period.
- Post messages to Slack using Bot User for GitHub issues for a time period.
This sample shows how Linx automatically post messages to Slack. Once this GitHub-Slack integration is active, the sample posts messages to Slack Channel.
- GitHub-Linx integration guide
- GitHub API authentication documentation
- GitHub API documentation
- Create New App in Slack
- Slack API documentation
- Test apis from browser
- Slack reference for block kit
- Simpler Slack sample get started
- Linx Designer
- Github account
- Slack account
This solution was developed in the Linx Designer v5.21.0.0
- Register a new 'OAuth application' on GitHub
- Configure the 'Authorization callback URL' to be:
http://localhost:8080/oauth/token
- Save your app.
- Generate your client secret.
- Copy the Client ID and Client Secret.
-
Open the solution in your Linx Designer.
-
Edit the $.Settings values:
GitHubClientId
: Your GitHub app’s Client IDGitHubClientSecret
: Your GitHub app’s Client SecretGitHubOwner
: Insert the owner of your repository. E.g From the link https://github.com/linx-software, linx-software is the owner.
-
Save the Solution.
Generate access tokens:
- Start the debugger on the
RESTHost service
inGeneric_OAuth2
Folder in the Linx Designer. - Make a request in your browser to
http://localhost:8080/oauth/authorize
- You will be redirected to the GitHub OAuth 2.0 access consent screen.
- Authorize the connected application.
- View success message.
Note that access token generated is stored in the file's path defined in
TokenLogPath
in Solution's $.Settings
-
Choose from Scratch.
- Enter App Name, e.g linx-bot-github
- Choose a workspace and click on create app
-
Under 'Add features and functionality'
- Click on Bots
-
Under 'First, assign a scope to your bot token'
- Click on Review Scopes to Add
-
Go to
Scopes
section- Click on Add an OAuth Scope
- Choose the following from the list:
- im:history
- chat:write
- channels:history
- im:write
- chat:write.public
- channels:read
- groups:read
- im:read
- mpim:read
- Click on Add an OAuth Scope
- Choose the following from the list:
-
As the above is done, a message
You can now show tabs on App HomeManage which tabs your user sees in your app’s home. Go to App Home
will appear. -
Scroll the page up and go to the section
OAuth Tokens for Your Workspace
-
Click on the button Install to workspace and click on Allow
-
Once successful, you will be directed to the
OAuth Tokens for Your Workspace
section -
Click on the Copy Button to copy the Bot User OAuth Token. Note that it starts with
xoxb-
SlackBotUserOAuth
: Paste the token copied aboveSlackChannel
: Enter channel Id or channel name. How to find your Channel Id in Slack
After your app has been successfully authorized, the access token is stored in a local text file.
Requests are authenticated via a Bearer access token included in the Authorization
header of each request.
The token is retrieved from the file before each request is made.
When making requests to the GitHub, you must also include the following header:
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 YaBrowser/16.3.0.7146 Yowser/2.5 Safari/537.36
Requests are authenticated via a Bearer access token included in the Authorization
header of each request.
When making requests to the GitHub, you must also include the following header:
Accept: application/x-www-form-urlencoded , application/json
Lists repositories for the specified organization.
https://docs.github.com/en/rest/reference/repos#list-organization-repositories
The Repo Commits API supports listing, viewing, and comparing commits in a repository.
https://docs.github.com/en/rest/reference/repos#list-commits
Parameters:
Parameter | Type | |
---|---|---|
repo | string query | Repositories' name |
since | string query | Only show notifications updated after the given time. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. |
until | string query | Only commits before this date will be returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. |
per_page | integer query | Results per page (max 100). Default: 30. |
List issues in a repository.
https://docs.github.com/en/rest/reference/issues#list-repository-issues
Parameters:
Parameter | Type | |
---|---|---|
since | string query | Only show notifications updated after the given time. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ. |
state | string query | Indicates the state of the issues to return. Can be either open, closed, or all. Default: open |
per_page | integer query | Results per page (max 100). Default: 30. |
A function to test the authentication is working. Gets the authenticated user info. Calls the api https://api.github.com/user
A function to test if you've entered the right token and the right channel. Calls the api https://slack.com/api/auth.test.
Reads commits from GitHub and populates cutomized types.
- Parameters:
- Same parameters as in
GetCommits
above.
- Same parameters as in
- Result:
commitList
: List of customized commits
Messages can be sent to Slack with different parameters. In this section, we build blocks from commitList, that are sent as JSON to Slack.
- Parameters:
- Same parameters as in
GetCustomizedCommitsForRepos
above. commitList
fromGetCustomizedCommitsForRepo
' above
- Same parameters as in
- Result:
blocks
: String type that stores the JSON blocks
Messages can be sent to Slack with different parameters. In this section, we build blocks from issue, that are sent as JSON to Slack.
- Parameters:
issue
: Linx type
- Result:
blocks
: String type that stores the JSON blocks
A function that gets issues, builds blocks and posts message to slack using the PostMessageAPI above.
A function that gets commits, builds blocks and posts message to slack using the PostMessageAPI above.
Calls the Post message API https://api.slack.com/methods/chat.postMessage
- Parameters:
blocks
: String type in JSON format
Calls the Get API https://api.slack.com/methods/conversations.list. Lists all channels in a Slack team.
Gets the Channel's name.
In the Demo Folder,
- To post commits info to Slack, click on the function
PostCommitsToChannelForChannelName
. Enter parameters as follows:Per_page
: 1Since
: start date of commits (e.g Yesterday's date or any other date before ‘until date below’: 2021-05-26)Until
: end date of commits (e.g Today's date : 2021-05-27)
- To post issues info to Slack, click on the function
PostIssuesToChannelForChannelName
. GitHub Issues API reference provides a list of paramaters that can be used with the API call. In our sample, we are using the following hard-coded values:- state : all
Other parameters are as follows: Per_page
: 1Since
: start date of commits (e.g Yesterday's date : 2021-05-26)Channel
: Enter channel name to search for from list of channels
- state : all
If you want to post message to a private channel or and receiving channel_not_found message, you should invite the bot to the channel. A bot can't post in a private channel if it's not a member.
For questions please ask the Linx community or use the Slack channel.