Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Discord automated event reminder example #12

Merged
merged 9 commits into from
Jul 5, 2024
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ Now, utilize the available connector operations.
```bash
bal run
```

## Examples

The `Discord` connector provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-discord/tree/main/examples/), covering the following use cases:

1. [Automated Event Reminders](https://github.com/ballerina-platform/module-ballerinax-discord/tree/main/examples/automated-event-reminders)


## Build from the source

### Prerequisites
Expand Down
6 changes: 6 additions & 0 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ Now, utilize the available connector operations.
```bash
bal run
```

## Examples

The `Discord` connector provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-discord/tree/main/examples/), covering the following use cases:

1. [Automated Event Reminders](https://github.com/ballerina-platform/module-ballerinax-discord/tree/main/examples/automated-event-reminders)
6 changes: 6 additions & 0 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ Now, utilize the available connector operations.
```bash
bal run
```

## Examples

The `Discord` connector provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-discord/tree/main/examples/), covering the following use cases:

1. [Automated Event Reminders](https://github.com/ballerina-platform/module-ballerinax-discord/tree/main/examples/automated-event-reminders)
51 changes: 51 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Examples

The `ballerinax/discord` connector provides practical examples illustrating usage in various scenarios. Explore the below examples, covering use cases like automated event announcements and automated role assignment based on reactions.

1. [Automated Event Reminders](/examples/automated-event-reminders/Discord%20automated%20event%20reminders.md)

## Prerequisites

1. Generate Discord credentials to authenticate the connector as described in the [Setup guide](https://central.ballerina.io/ballerinax/discord/latest#setup-guide).

2. For each example, create a `Config.toml` file the related configuration. Here's an example of how your `Config.toml` file should look:

```toml
clientId="<client_id>"
clientSecret="<client_secret>"
scopes=["<scope1>", "<scope2>"]
```

## Running an Example

Execute the following commands to build an example from the source:

* To build an example:

```bash
bal build
```

* To run an example:

```bash
bal run
```

## Building the Examples with the Local Module

**Warning**: Due to the absence of support for reading local repositories for single Ballerina files, the Bala of the module is manually written to the central repository as a workaround. Consequently, the bash script may modify your local Ballerina repositories.

Execute the following commands to build all the examples against the changes you have made to the module locally:

* To build all the examples:

```bash
./build.sh build
```

* To run all the examples:

```bash
./build.sh run
```
1 change: 1 addition & 0 deletions examples/automated-event-reminders/.github/README.md
5 changes: 5 additions & 0 deletions examples/automated-event-reminders/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "wso2"
name = "discord_automated_event_reminders"
version = "0.1.0"
distribution = "2201.9.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Discord automated event reminders

This example demonstrates how to automate event announcements and reminders in a Discord server using the Ballerina Discord connector.

## Prerequisites

### 1. Setup Discord account

Refer to the [Setup guide](https://central.ballerina.io/ballerinax/discord/latest#setup-guide) to set up your Discord account, if you do not have one.

### 2. Configuration

Create a `Config.toml` file in the example root directory and update your Discord account related configurations as follows:

```toml
clientId = "<YOUR_CLIENT_ID>"
clientSecret = "<YOUR_CLIENT_SECRET>"
scopes = ["<YOUR>", "<REQUIRED>", "<SCOPES>"]
```

## Run the example

Execute the following command to run the example:

```ballerina
bal run
```
112 changes: 112 additions & 0 deletions examples/automated-event-reminders/main.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/log;
import ballerina/task;
import ballerinax/discord;

configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string[] scopes = ?;

string guildId = "YOUR_GUILD_ID";

# Represents a Discord Channel.
public type Channel record {
# ID of the channel
string id;
};

# Represents a Discord Event.
public type Event record {
# ID of the event
string id;
# Name of the event
string name;
};

discord:ConnectionConfig discordConfig = {
auth: {
clientId,
clientSecret,
scopes
}
};

discord:Client discord = check new (discordConfig);

public function main() returns error? {
// Registers the scheduled event.
discord:guild_id_scheduledevents_body payload = {
name: "Community Meeting",
description: "Join us for our monthly community meeting!",
scheduled_start_time: "2024-12-31T23:59:59Z",
scheduled_end_time: "2024-12-31T23:59:59Z",
privacy_level: 2,
entity_type: 1,
entity_metadata: {
location: "Virtual"
}
};

Event|error eventResponse = discord->/guilds/[guildId]/scheduled\-events.post(payload);
if eventResponse is error {
log:printError("Error creating the scheduled event: ", eventResponse);
return;
}
ayeshLK marked this conversation as resolved.
Show resolved Hide resolved

// Schedules the daily recurring task to send the event reminders to all the related channels.
task:JobId _ = check task:scheduleJobRecurByFrequency(new EventReminderJob(eventResponse.id), 60 * 60 * 24);
}

# A Ballerina scheduled job that sends a given event reminder message to a list of Discord channels.
class EventReminderJob {
*task:Job;

private final string eventName;

public function init(string eventName) {
self.eventName = eventName;
}

public function execute() {
anydata[]|error channelsResponse = discord->/guilds/[guildId]/channels();
if channelsResponse is error {
log:printError("Error getting the channels: ", channelsResponse);
return;
}

Channel[]|error channelsResult = channelsResponse.ensureType();
if channelsResult is error {
log:printError("Error getting the channels: ", channelsResult);
return;
}

foreach Channel channel in channelsResult {
any|error reminderResponse = discord->/channels/[channel.id]/messages.post({
Content\-Type: "application/x-www-form-urlencoded"
},
{
content: string `⏰ Reminder: Our community meeting '${self.eventName}' is coming up soon! Don't forget to join us.`
}
);
if reminderResponse is error {
log:printError("Error sending the reminder message: ", reminderResponse);
return;
}
}
}
}
78 changes: 78 additions & 0 deletions examples/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'java'

def graalvmFlag = ""

task testExamples {
if (project.hasProperty("balGraalVMTest")) {
graalvmFlag = "--graalvm"
}
doLast {
try {
exec {
workingDir project.projectDir
println("Working dir: ${workingDir}")
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'cmd', "/c", "chmod +x ./build.sh && ./build.sh run && exit %%ERRORLEVEL%%"
} else {
commandLine 'sh', "-c", "chmod +x ./build.sh && ./build.sh run"
}
}
} catch (Exception e) {
println("Example Build failed: " + e.message)
throw e
}
}
}

task buildExamples {
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(":discord-examples:test")) {
buildExamples.enabled = false
} else {
testExamples.enabled = false
}
}
doLast {
try {
exec {
workingDir project.projectDir
println("Working dir: ${workingDir}")
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'cmd', "/c", "chmod +x ./build.sh && ./build.sh build && exit %%ERRORLEVEL%%"
} else {
commandLine 'sh', "-c", "chmod +x ./build.sh && ./build.sh build"
}
}
} catch (Exception e) {
println("Example Build failed: " + e.message)
throw e
}
}
}

buildExamples.dependsOn ":discord-ballerina:build"
testExamples.dependsOn ":discord-ballerina:build"

// TODO: Enable the examples build once https://github.com/ballerina-platform/ballerina-library/issues/6135 is fixed
// test.dependsOn testExamples
// build.dependsOn buildExamples
63 changes: 63 additions & 0 deletions examples/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

BAL_EXAMPLES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BAL_CENTRAL_DIR="$HOME/.ballerina/repositories/central.ballerina.io"
BAL_HOME_DIR="$BAL_EXAMPLES_DIR/../ballerina"

set -e

case "$1" in
build)
BAL_CMD="build"
;;
run)
BAL_CMD="run"
;;
*)
echo "Invalid command provided: '$1'. Please provide 'build' or 'run' as the command."
exit 1
;;
esac

# Read Ballerina package name
BAL_PACKAGE_NAME=$(awk -F'"' '/^name/ {print $2}' "$BAL_HOME_DIR/Ballerina.toml")

# Push the package to the local repository
cd "$BAL_HOME_DIR" &&
bal pack &&
bal push --repository=local

# Remove the cache directories in the repositories
cacheDirs=$(ls -d $BAL_CENTRAL_DIR/cache-* 2>/dev/null) || true
for dir in "${cacheDirs[@]}"; do
[ -d "$dir" ] && rm -r "$dir"
done
echo "Successfully cleaned the cache directories"

# Create the package directory in the central repository, this will not be present if no modules are pulled
mkdir -p "$BAL_CENTRAL_DIR/bala/ballerinax/$BAL_PACKAGE_NAME"

# Update the central repository
BAL_DESTINATION_DIR="$HOME/.ballerina/repositories/central.ballerina.io/bala/ballerinax/$BAL_PACKAGE_NAME"
BAL_SOURCE_DIR="$HOME/.ballerina/repositories/local/bala/ballerinax/$BAL_PACKAGE_NAME"
[ -d "$BAL_DESTINATION_DIR" ] && rm -r "$BAL_DESTINATION_DIR"
[ -d "$BAL_SOURCE_DIR" ] && cp -r "$BAL_SOURCE_DIR" "$BAL_DESTINATION_DIR"
echo "Successfully updated the local central repositories"

echo "$BAL_DESTINATION_DIR"
echo "$BAL_SOURCE_DIR"

# Loop through examples in the examples directory
cd "$BAL_EXAMPLES_DIR"
for dir in $(find "$BAL_EXAMPLES_DIR" -type d -maxdepth 1 -mindepth 1); do
# Skip the build directory
if [[ "$dir" == *build ]]; then
continue
fi
(cd "$dir" && bal "$BAL_CMD" --offline && cd ..);
done

# Remove generated JAR files
find "$BAL_HOME_DIR" -maxdepth 1 -type f -name "*.jar" | while read -r JAR_FILE; do
rm "$JAR_FILE"
done
Loading