-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from adibmbrk/master
Add automated summary report as an example
- Loading branch information
Showing
7 changed files
with
143 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../Slack automated summary report.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
org = "wso2" | ||
name = "automated_summary_report" | ||
version = "0.1.0" | ||
distribution = "2201.9.0" |
21 changes: 21 additions & 0 deletions
21
examples/automated-summary-report/Slack automated summary report.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Automated summary report | ||
|
||
This use case demonstrates how the Slack API can be utilized to generate a summarized report of daily stand up chats in the general channel. | ||
|
||
## Prerequisites | ||
|
||
1. Generate a Slack token to authenticate the connector as described in the [Setup guide](https://central.ballerina.io/ballerinax/slack/latest#prerequisites). | ||
|
||
2. For each example, create a `Config.toml` file with the related configuration. Here's an example of what your `Config.toml` file should look: | ||
|
||
```toml | ||
token = "<token>" | ||
``` | ||
|
||
## Run the example | ||
|
||
Execute the following command to run the example: | ||
|
||
```ballerina | ||
bal run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import ballerina/io; | ||
import ballerina/log; | ||
import ballerinax/slack; | ||
|
||
configurable string token = ?; | ||
|
||
# Holds slack channel information. | ||
type ChannelType record { | ||
string id; | ||
}; | ||
|
||
# Holds response struture of the channels list. | ||
type Channels record {| | ||
boolean ok; | ||
ChannelType[] channels; | ||
|}; | ||
|
||
# Holds information of each text | ||
type TextType record { | ||
string text; | ||
}; | ||
|
||
# Holds the response structure of the conversation history. | ||
type History record { | ||
boolean ok; | ||
TextType[] texts; | ||
}; | ||
|
||
// Initialize the Slack client with the provided token. | ||
final slack:Client slack = check new Client({ | ||
auth: { | ||
token: value | ||
} | ||
}); | ||
|
||
public function main() returns error? { | ||
// Fetch the list of channels. | ||
json channelResponse = check slack->/conversations\.list(); | ||
Channels channels = check channelResponse.cloneWithType(); | ||
|
||
// Array to store the latest text messages from each channel. | ||
string[] latestText; | ||
|
||
// Iterate through each channel to get the latest message. | ||
foreach ChannelType channel in channels.channels { | ||
// Fetch the conversation history for the current channel. | ||
json historyResponse = check slack->/conversations\.history({channel: channel.id}); | ||
History history = check historyResponse.cloneWithType(); | ||
|
||
// Get the latest text message from the conversation history. | ||
TextType[] texts = history.texts; | ||
latestText.push(texts[0].text); | ||
} | ||
|
||
// Construct the stand-up report message. | ||
string textMessage = string `Automated Stand Up Report: ${"\n"}${ | ||
<string>from [int, string] [index, text] in latestText.enumerate() | ||
select string `${index + 1}. ${text}${"\n"}` | ||
}`; | ||
|
||
// Post the stand-up report message to the "general" channel. | ||
json|error postMessageResult = slack->/chat\.postMessage.post({channel: "general", text: textMessage}); | ||
|
||
if postMessageResult is error { | ||
log:printError("Failed to post message to Slack", postMessageResult); | ||
} else { | ||
log:printInfo("Message posted successfully"); | ||
} | ||
} |