Skip to content

Commit

Permalink
Add optional PostHog support + better commands
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Jan 8, 2024
1 parent 7f849f6 commit d08d4bd
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 41 deletions.
151 changes: 131 additions & 20 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ keywords = [
log = "0.4"
rand = "0.8"
url = "2.4.1"
axum = "0.7.3"
indoc = "2.0.4"
dashmap = "5.5"
axum = "0.6.20"
dotenvy = "0.15.7"
serde_with = "3.3"
reqwest = "0.11.22"
posthog-rs = "0.2.2"
serde_json = "1.0.108"
humantime-serde = "1.1"
pretty_env_logger = "0.5"
Expand Down
4 changes: 4 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# You can put it below or set the WLD_CAPTCHA_BOT_TOKEN env var
# bot_token = "..."

# You can optionally set up PostHog to track bot usage.
# To enable, put your PostHog API token below or set the WLD_CAPTCHA_POSTHOG_TOKEN env var
# posthog_token = "..."

# The URL to the server running this bot. It must be accessible from the internet.
# You can put it below or set the WLD_CAPTCHA_APP_URL env var
# app_url = "..."
Expand Down
27 changes: 13 additions & 14 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::{
#[derive(BotCommands)]
#[command(rename_rule = "lowercase", description = "Available commands:")]
pub enum Command {
#[command(description = "Display this text")]
#[command(description = "Explain how this bot works, and how to use it.")]
Help,
#[command(description = "Pong!")]
Ping,
#[command(description = "Start the bot")]
#[command(description = "Check that the bot is online and has the right permissions.")]
Check,
#[command(description = "Initial help when talking to the bot for the first time.")]
Start,
}

Expand All @@ -43,14 +43,9 @@ pub async fn command_handler(
};

match command {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
.reply_to_message_id(msg.id)
.await?;
},
Command::Ping => {
Command::Check => {
if msg.chat.is_private() {
bot.send_message(msg.chat.id, "pong")
bot.send_message(msg.chat.id, "You can only use this bot in public groups. Please add me to a public group (with admin permissions) and try again.")
.reply_to_message_id(msg.id)
.await?;
return Ok(());
Expand All @@ -62,17 +57,21 @@ pub async fn command_handler(
.is_administrator();

if is_admin {
bot.send_message(msg.chat.id, "Bot has admin permissions and is ready to go!")
bot.send_message(msg.chat.id, "Bot has admin permissions and is ready to go! Once someone joins the group, they'll be asked to prove they're human with World ID before they can send messages.")
.reply_to_message_id(msg.id)
.await?;
} else {
bot.send_message(msg.chat.id, "Bot doesn't have admin permissions! Please, give it admin permissions and try again.").reply_to_message_id(msg.id)
.await?;
}
},
Command::Start => {
Command::Help | Command::Start => {
if msg.chat.is_private() {
bot.send_message(msg.chat.id, "Hello! To use this bot, add it to a group and give it admin permissions. Then, use /ping to check if it's working.").reply_to_message_id(msg.id).await?;
bot.send_message(msg.chat.id, r#"
Welcome to the World ID Telegram bot!
You can use me to protect your group from spammers and bots. To get started, add me to your (public) group and give me admin permissions. When someone joins your group, they'll be asked to prove they're human with World ID before they can send messages.
"#).reply_to_message_id(msg.id).await?;
}
},
};
Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct AppConfig {
pub bot_token: String,
#[serde(flatten, default)]
pub groups_config: GroupsConfig,
pub posthog_token: Option<String>,
}

impl AppConfig {
Expand All @@ -26,6 +27,12 @@ impl AppConfig {
.build()?
.try_deserialize()
}

pub fn posthog(&self) -> Option<posthog_rs::Client> {
self.posthog_token
.as_ref()
.map(|token| posthog_rs::client(token.as_str()))
}
}

#[serde_as]
Expand Down
Loading

0 comments on commit d08d4bd

Please sign in to comment.