Skip to content

Commit

Permalink
Merge pull request #47 from Sleitnick/feature/notifications
Browse files Browse the repository at this point in the history
Notifications
  • Loading branch information
Sleitnick authored Mar 28, 2024
2 parents c391af5 + 3859725 commit 1997927
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rbxcloud"
version = "0.9.0"
version = "0.10.0"
description = "CLI and SDK for the Roblox Open Cloud APIs"
authors = ["Stephen Leitnick"]
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Possible use-cases:
| :x: | Instances |
| :white_check_mark: | Subscriptions |
| :x: | Inventory |
| :x: | User Notifications |
| :white_check_mark: | User Notifications |

- :white_check_mark: = Supported
- :x: = Not yet supported
Expand All @@ -42,7 +42,7 @@ The goal of this project is to support all API endpoints that Roblox provides.
### Aftman
Run the `aftman add` command within your project directory. This will add `rbxcloud` to the project's `aftman.toml` file (or create one if it doesn't yet exist).
```sh
$ aftman add Sleitnick/rbxcloud@0.9.0
$ aftman add Sleitnick/rbxcloud@0.10.0
```

### From Release
Expand All @@ -59,7 +59,7 @@ The library built for the CLI tool is available to use directly in Rust projects
To use `rbxcloud` in a Rust project, simply add `rbxcloud` to the `Cargo.toml` dependency list.
```toml
[dependencies]
rbxcloud = "0.9.0"
rbxcloud = "0.10.0"
```

Alternatively, use `cargo add`.
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/cli-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ There are a few different ways to install the `rbxcloud` CLI.
### [Aftman](https://github.com/LPGhatguy/aftman) <small>(Preferred)</small>
Run the `aftman add` command within your project directory. This will add `rbxcloud` to the project's `aftman.toml` file (or create one if it doesn't yet exist).
```sh
$ aftman add Sleitnick/rbxcloud@0.9.0
$ aftman add Sleitnick/rbxcloud@0.10.0
```

Next, run `aftman install` to install `rbxcloud`.
Expand All @@ -17,7 +17,7 @@ Add `rbxcloud` under the `[tools]` section of your `foreman.toml` file.
```toml
# foreman.toml
[tools]
rbxcloud = { github = "Sleitnick/rbxcloud", version = "0.9.0" }
rbxcloud = { github = "Sleitnick/rbxcloud", version = "0.10.0" }
```

Next, run `foreman install` to install `rbxcloud`.
Expand Down
21 changes: 21 additions & 0 deletions docs/cli/cli-notification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Notification API

## Send Notification
Send a notification to a user.
```
Usage: rbxcloud notification send [OPTIONS] --universe-id <UNIVERSE_ID> --user-id <USER_ID> --payload <PAYLOAD> --api-key <API_KEY>
Options:
-u, --universe-id <UNIVERSE_ID> Universe ID
-U, --user-id <USER_ID> User ID
-P, --payload <PAYLOAD> Payload
-p, --pretty Pretty-print the JSON response
-a, --api-key <API_KEY> Roblox Open Cloud API Key [env: RBXCLOUD_API_KEY=]
-h, --help Print help
```

### Example
For an example payload, see the [Roblox documentation](https://create.roblox.com/docs/cloud/reference/UserNotification#Create-User-Notification).
```
$ rbxcloud notification send -p -u 12345 -U 6789 -P PAYLOAD_JSON -a MY_KEY
```
2 changes: 1 addition & 1 deletion docs/cli/cli-subscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Getting Subscription Info
```
Usage: rbxcloud.exe subscription get [OPTIONS] --universe-id <UNIVERSE_ID> --product <PRODUCT> --subscription <SUBSCRIPTION> --api-key <API_KEY>
Usage: rbxcloud subscription get [OPTIONS] --universe-id <UNIVERSE_ID> --product <PRODUCT> --subscription <SUBSCRIPTION> --api-key <API_KEY>
Options:
-u, --universe-id <UNIVERSE_ID> Universe ID
Expand Down
2 changes: 1 addition & 1 deletion docs/lib/lib-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
To use `rbxcloud` in a Rust project, simply add `rbxcloud` to the `Cargo.toml` dependency list.
```toml
[dependencies]
rbxcloud = "0.9.0"
rbxcloud = "0.10.0"
```

Alternatively, use `cargo add`.
Expand Down
66 changes: 66 additions & 0 deletions examples/send-notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::collections::HashMap;

use rbxcloud::rbx::{
error::Error,
types::{RobloxUserId, UniverseId},
v2::{
notification::{
JoinExperience, Notification, NotificationPayload, NotificationResponse,
NotificationSource, NotificationType, Parameter,
},
Client,
},
};

async fn send_notification() -> Result<NotificationResponse, Error> {
// Inputs:
let api_key = "MY_API_KEY";
let message_id = "MY_MESSAGE_ID";
let universe_id = 9876543210;
let user_id = 308165;

let client = Client::new(api_key);
let notification_client = client.notification(UniverseId(universe_id));

let notification = Notification {
source: NotificationSource {
universe: format!("universe/{}", universe_id),
},
payload: NotificationPayload {
message_id: message_id.to_string(),
notification_type: NotificationType::TypeUnspecified,
join_experience: Some(JoinExperience {
launch_data: "Some launch data here".to_string(),
}),
analytics_data: Some(HashMap::from([(
"category".to_string(),
"Bronze egg hatched".to_string(),
)])),
parameters: Some(HashMap::from([(
"key".to_string(),
Parameter {
string_value: Some("bronze egg".to_string()),
int64_value: None,
},
)])),
},
};

notification_client
.send(RobloxUserId(user_id), notification)
.await
}

#[tokio::main]
async fn main() {
let send_result = send_notification().await;

match send_result {
Ok(result) => {
println!("Notification sent: {:?}", result);
}
Err(e) => {
eprintln!("{e:?}");
}
}
}
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ nav:
- OrderedDataStore: cli/cli-ordered-datastore.md
- Group: cli/cli-group.md
- Subscription: cli/cli-subscription.md
- Notification: cli/cli-notification.md
- Rust Lib:
- Install: lib/lib-install.md

Expand Down
9 changes: 7 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ mod datastore_cli;
mod experience_cli;
mod group_cli;
mod messaging_cli;
mod notification_cli;
mod ordered_datastore_cli;
mod subscription_cli;

use clap::{Parser, Subcommand};

use self::{
assets_cli::Assets, datastore_cli::DataStore, experience_cli::Experience, group_cli::Group,
messaging_cli::Messaging, ordered_datastore_cli::OrderedDataStore,
subscription_cli::Subscription,
messaging_cli::Messaging, notification_cli::Notification,
ordered_datastore_cli::OrderedDataStore, subscription_cli::Subscription,
};

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -43,6 +44,9 @@ pub enum Command {

/// Access the Roblox Subscription API
Subscription(Subscription),

/// Access the Roblox Notification API
Notification(Notification),
}

impl Cli {
Expand All @@ -55,6 +59,7 @@ impl Cli {
Command::OrderedDatastore(command) => command.run().await,
Command::Group(command) => command.run().await,
Command::Subscription(command) => command.run().await,
Command::Notification(command) => command.run().await,
}
}
}
74 changes: 74 additions & 0 deletions src/cli/notification_cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use clap::{Args, Subcommand};
use rbxcloud::rbx::{
types::{RobloxUserId, UniverseId},
v2::Client,
};

#[derive(Debug, Subcommand)]
pub enum NotificationCommands {
/// Send a notification to a user
Send {
/// Universe ID
#[clap(short, long, value_parser)]
universe_id: u64,

/// User ID
#[clap(short = 'U', long, value_parser)]
user_id: u64,

/// Payload
#[clap(short = 'P', long, value_parser)]
payload: String,

/// Pretty-print the JSON response
#[clap(short, long, value_parser, default_value_t = false)]
pretty: bool,

/// Roblox Open Cloud API Key
#[clap(short, long, value_parser, env = "RBXCLOUD_API_KEY")]
api_key: String,
},
}

#[derive(Debug, Args)]
pub struct Notification {
#[clap(subcommand)]
command: NotificationCommands,
}

impl Notification {
pub async fn run(self) -> anyhow::Result<Option<String>> {
match self.command {
NotificationCommands::Send {
universe_id,
user_id,
payload,
pretty,
api_key,
} => {
let client = Client::new(&api_key);
let notification_client = client.notification(UniverseId(universe_id));

let notification = serde_json::from_str::<
rbxcloud::rbx::v2::notification::Notification,
>(&payload)?;

let res = notification_client
.send(RobloxUserId(user_id), notification)
.await;

match res {
Ok(subscription_info) => {
let r = if pretty {
serde_json::to_string_pretty(&subscription_info)?
} else {
serde_json::to_string(&subscription_info)?
};
Ok(Some(r))
}
Err(err) => Err(anyhow::anyhow!(err)),
}
}
}
}
}
31 changes: 30 additions & 1 deletion src/rbx/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ use self::{
ListGroupMembershipsParams, ListGroupMembershipsResponse, ListGroupRolesParams,
ListGroupRolesResponse,
},
notification::{Notification, NotificationParams, NotificationResponse},
subscription::{GetSubscriptionParams, GetSubscriptionResponse, SubscriptionView},
};
pub mod group;
pub(crate) mod http_err;
pub mod notification;
pub mod subscription;

use crate::rbx::error::Error;

use super::types::{GroupId, UniverseId};
use super::types::{GroupId, RobloxUserId, UniverseId};

/// Access into the Roblox Open Cloud APIs.
///
Expand All @@ -40,6 +42,11 @@ pub struct SubscriptionClient {
pub api_key: String,
}

pub struct NotificationClient {
pub api_key: String,
pub universe_id: UniverseId,
}

impl GroupClient {
pub async fn get_info(&self) -> Result<GetGroupResponse, Error> {
group::get_group(&GetGroupParams {
Expand Down Expand Up @@ -107,6 +114,21 @@ impl SubscriptionClient {
}
}

impl NotificationClient {
pub async fn send(
&self,
user_id: RobloxUserId,
notification: Notification,
) -> Result<NotificationResponse, Error> {
notification::send_notification(&NotificationParams {
api_key: self.api_key.clone(),
user_id,
notification,
})
.await
}
}

impl Client {
pub fn new(api_key: &str) -> Client {
Client {
Expand All @@ -126,4 +148,11 @@ impl Client {
api_key: self.api_key.clone(),
}
}

pub fn notification(&self, universe_id: UniverseId) -> NotificationClient {
NotificationClient {
api_key: self.api_key.clone(),
universe_id,
}
}
}
Loading

0 comments on commit 1997927

Please sign in to comment.