Skip to content

Commit

Permalink
Sample Greet bot phase 1 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan committed Dec 23, 2024
1 parent ba84c49 commit 5eeb54b
Show file tree
Hide file tree
Showing 44 changed files with 636 additions and 48 deletions.
94 changes: 67 additions & 27 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[workspace]
members = [
"backend/bots/api",
"backend/bots/c2c_client",
"backend/bots/examples/greet",
"backend/canisters/airdrop_bot/api",
"backend/canisters/airdrop_bot/client",
"backend/canisters/airdrop_bot/impl",
Expand Down Expand Up @@ -106,6 +105,8 @@ members = [
"backend/external_canisters/sonic/api",
"backend/external_canisters/sonic/c2c_client",
"backend/integration_tests",
"backend/legacy_bots/api",
"backend/legacy_bots/c2c_client",
"backend/libraries/activity_notification_state",
"backend/libraries/candid_gen",
"backend/libraries/canister_agent_utils",
Expand Down Expand Up @@ -186,6 +187,7 @@ futures = "0.3.30"
getrandom = { version = "0.2.15", features = ["custom"] }
hex = "0.4.3"
hmac-sha256 = { version = "1.1.7", features = ["traits010"] }
http = "1.1.0"
ic-agent = "0.39.1"
ic-canister-sig-creation = "1.1.0"
ic-captcha = "1.0.0"
Expand All @@ -195,6 +197,7 @@ ic-cdk-macros = "0.17.0"
ic-cdk-timers = "0.11.0"
ic-certificate-verification = "2.4.0"
ic-certification = "2.5.0"
ic-http-certification = "2.5.0"
ic-ledger-types = "0.14.0"
ic_principal = "0.1.1"
ic-stable-structures = "0.6.7"
Expand Down
18 changes: 18 additions & 0 deletions backend/bots/examples/greet/can.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type BotApiCallResponse = variant {
Ok;
Err : variant {
Invalid : text;
CanisterError : variant {
NotAuthorized;
Frozen;
Other : text;
};
C2CError: record {
0 : nat32;
1 : text;
};
};
};

service : {
};
29 changes: 29 additions & 0 deletions backend/bots/examples/greet/cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "greet_bot_canister_impl"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
candid = { workspace = true }
canister_client = { path = "../../../libraries/canister_client" }
getrandom = { workspace = true }
http = { workspace = true }
ic-cdk = { workspace = true }
ic-cdk-timers = { workspace = true }
ic-http-certification = { workspace = true }
ic_principal = { workspace = true }
ic-stable-structures = { workspace = true }
jwt = { path = "../../../libraries/jwt" }
local_user_index_canister = { path = "../../../canisters/local_user_index/api" }
local_user_index_canister_c2c_client = { path = "../../../canisters/local_user_index/c2c_client" }
msgpack = { path = "../../../libraries/msgpack" }
rmp-serde = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
types = { path = "../../../libraries/types" }
37 changes: 37 additions & 0 deletions backend/bots/examples/greet/src/commands/greet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use local_user_index_canister::execute_bot_command::{self, BotApiCallError};
use types::{
bot_actions::{BotMessageAction, MessageContent},
BotAction, BotCommandClaims, TextContent,
};

use crate::execute::{execute_bot_command, InternalError, Message, SuccessResult};

pub async fn greet(bot: BotCommandClaims, access_token: &str) -> Result<SuccessResult, InternalError> {
let user_id = bot.initiator;
let content = MessageContent::Text(TextContent {
text: format!("hello @UserId({user_id})"),
});

let args = execute_bot_command::Args {
action: BotAction::SendMessage(BotMessageAction {
content: content.clone(),
finalised: true,
}),
jwt: access_token.to_string(),
};

match execute_bot_command(bot.bot_api_gateway, &args).await {
Ok(Ok(_)) => Ok(SuccessResult {
message: Some(Message {
id: bot.message_id,
content,
}),
}),
Ok(Err(error)) => match error {
BotApiCallError::C2CError(code, message) => Err(InternalError::C2CError(code, message)),
BotApiCallError::CanisterError(canister_error) => Err(InternalError::CanisterError(canister_error)),
BotApiCallError::Invalid(text) => Err(InternalError::Invalid(text)),
},
Err((code, message)) => Err(InternalError::C2CError(code as i32, message)),
}
}
1 change: 1 addition & 0 deletions backend/bots/examples/greet/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod greet;
19 changes: 19 additions & 0 deletions backend/bots/examples/greet/src/definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::collections::HashSet;

use types::{BotDefinition, MessagePermission, SlashCommandPermissions, SlashCommandSchema};

pub fn definition() -> BotDefinition {
BotDefinition {
description: "Ths bot provides a single command `greet`".to_string(),
commands: vec![SlashCommandSchema {
name: "greet".to_string(),
description: Some("This will greet the caller".to_string()),
params: vec![],
permissions: SlashCommandPermissions {
community: HashSet::new(),
chat: HashSet::new(),
message: HashSet::from_iter([MessagePermission::Text]),
},
}],
}
}
16 changes: 16 additions & 0 deletions backend/bots/examples/greet/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use candid::Principal;
use types::{Nanoseconds, TimestampMillis, TimestampNanos};

const NANOS_PER_MILLISECOND: Nanoseconds = 1_000_000;

pub fn now() -> TimestampMillis {
now_nanos() / NANOS_PER_MILLISECOND
}

pub fn now_nanos() -> TimestampNanos {
ic_cdk::api::time()
}

pub fn canister_id() -> Principal {
ic_cdk::id()
}
Loading

0 comments on commit 5eeb54b

Please sign in to comment.