-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
636 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
type BotApiCallResponse = variant { | ||
Ok; | ||
Err : variant { | ||
Invalid : text; | ||
CanisterError : variant { | ||
NotAuthorized; | ||
Frozen; | ||
Other : text; | ||
}; | ||
C2CError: record { | ||
0 : nat32; | ||
1 : text; | ||
}; | ||
}; | ||
}; | ||
|
||
service : { | ||
}; |
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,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" } |
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,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)), | ||
} | ||
} |
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 @@ | ||
pub mod greet; |
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,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]), | ||
}, | ||
}], | ||
} | ||
} |
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,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() | ||
} |
Oops, something went wrong.