Compare commits

...

11 Commits
new ... master

Author SHA1 Message Date
74f178b986 Update Dockerfile
All checks were successful
continuous-integration/drone/push Build is passing
remove libssl1.1 as it no longer ships in debian
2023-09-22 11:27:17 +00:00
3446579da5 Update .drone.yml
Some checks failed
continuous-integration/drone/push Build is failing
fix pipeline type now that kubernetes is deprecated, remove deploy step since flux handles cd now
2023-09-22 11:02:26 +00:00
Andreas Larsen
01c5ea5fea bump helm chart version
Some checks reported errors
continuous-integration/drone/push Build was killed
continuous-integration/drone Build was killed
2023-05-18 19:17:10 +02:00
Andreas Larsen
a4c767d17c rename values.yml to yaml for helm to pick it up
Some checks reported errors
continuous-integration/drone/push Build was killed
2023-05-18 19:13:37 +02:00
2131fd59b0
chart: make secret external
All checks were successful
continuous-integration/drone/push Build is passing
2022-09-15 21:56:49 +02:00
49026a2d17
log: remove logging of unhandled messages
All checks were successful
continuous-integration/drone/push Build is passing
2022-09-15 21:34:52 +02:00
03a46667d2
ci: add caching to docker build
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is passing
2022-08-04 23:01:25 +02:00
9cd9d004e7
cd: updated dronefile to include deploy with helm
Some checks failed
continuous-integration/drone/push Build is failing
2022-08-04 22:56:05 +02:00
544705deb5
add help command
All checks were successful
continuous-integration/drone/push Build is passing
2022-08-04 18:30:09 +02:00
Andreas Larsen
269630115b
fix alreadyhitleet check and add time command
All checks were successful
continuous-integration/drone/push Build is passing
2022-08-04 18:19:27 +02:00
419335f78f
use message time for leet check instead of current time
All checks were successful
continuous-integration/drone/push Build is passing
2022-08-03 18:01:49 +02:00
8 changed files with 54 additions and 41 deletions

View File

@ -1,6 +1,5 @@
kind: pipeline kind: pipeline
type: kubernetes name: leetbot
name: default
clone: clone:
skip_verify: true skip_verify: true
@ -19,3 +18,4 @@ steps:
insecure: true insecure: true
dockerfile: Dockerfile dockerfile: Dockerfile
tags: latest tags: latest
cache_from: registry.local/northcode/leetbot

View File

@ -1,7 +1,7 @@
FROM debian FROM debian
RUN apt update RUN apt update
RUN apt install -y libssl1.1 openssl ca-certificates RUN apt install -y openssl ca-certificates
ADD target/release/telegram-leetbot /bin/. ADD target/release/telegram-leetbot /bin/.

View File

@ -7,7 +7,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes # This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version. # to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/) # Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0 version: 0.1.1
# This is the version number of the application being deployed. This version number should be # This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to # incremented each time you make changes to the application. Versions are not expected to

View File

@ -19,7 +19,7 @@ spec:
name: leetbot name: leetbot
envFrom: envFrom:
- secretRef: - secretRef:
name: {{ .Release.Name }}-token name: {{ .Values.app.secret_name }}
- configMapRef: - configMapRef:
name: {{ .Release.Name }}-cm name: {{ .Release.Name }}-cm
{{- if .Values.storage.enabled }} {{- if .Values.storage.enabled }}

View File

@ -1,7 +0,0 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-token
data:
TELEGRAM_BOT_TOKEN: {{ .Values.app.token | b64enc }}
DB_URL: {{ .Values.app.db | b64enc }}

View File

@ -2,8 +2,7 @@ image:
repository: registry.local/northcode/leetbot repository: registry.local/northcode/leetbot
app: app:
token: blabla secret_name: leetbot-token
db: sqlite:leet.db
tz: Europe/Oslo tz: Europe/Oslo
storage: storage:

View File

@ -1,17 +1,17 @@
use chrono::{Local, NaiveTime}; use chrono::NaiveTime;
#[must_use]
pub fn text_is_leet(text: &str) -> bool { pub fn text_is_leet(text: &str) -> bool {
text.eq_ignore_ascii_case("leet") || text == "l33t" || text == "1337" text.eq_ignore_ascii_case("leet") || text == "l33t" || text == "1337"
} }
pub fn time_is_leet() -> bool { #[must_use]
let now = Local::now().time(); pub fn time_is_leet(time: NaiveTime) -> bool {
let leet = NaiveTime::from_hms(13,37,30); let leet = NaiveTime::from_hms(13,37,30);
let allowed_timegap = 30; let allowed_timegap = 30;
let distance = now - leet; let distance = time - leet;
distance.num_seconds().abs() <= allowed_timegap distance.num_seconds().abs() <= allowed_timegap
} }

View File

@ -1,7 +1,7 @@
mod lib; mod lib;
use crate::lib::*; use crate::lib::{text_is_leet, time_is_leet};
use chrono::{NaiveDateTime, Local}; use chrono::{NaiveDateTime, NaiveTime, Local};
use sqlx::AnyPool; use sqlx::AnyPool;
use sqlx::any::AnyPoolOptions; use sqlx::any::AnyPoolOptions;
use teloxide::{prelude::*, RequestError, dispatching::UpdateFilterExt, utils::command::BotCommands}; use teloxide::{prelude::*, RequestError, dispatching::UpdateFilterExt, utils::command::BotCommands};
@ -48,22 +48,20 @@ enum LeetError {
DbError(#[from] sqlx::Error) DbError(#[from] sqlx::Error)
} }
async fn check_leet(username: &str, pool: &AnyPool) -> Result<(), LeetError> { async fn check_leet(username: &str, sent_time: NaiveDateTime, pool: &AnyPool) -> Result<(), LeetError> {
if ! time_is_leet() { if ! time_is_leet(sent_time.time()) {
return Err(LeetError::NotLeet); return Err(LeetError::NotLeet);
} }
let now = Local::now().naive_local();
let latest_leet : Option<(i32, String, NaiveDateTime)> = sqlx::query_as("select * from leet_log where username = $1 order by log_time desc") let latest_leet : Option<(i32, String, NaiveDateTime)> = sqlx::query_as("select * from leet_log where username = $1 order by log_time desc")
.bind(username) .bind(username)
.fetch_optional(pool) .fetch_optional(pool)
.await?; .await?;
if let Some(latest_leet) = latest_leet { if let Some(latest_leet) = latest_leet {
let distance = now - latest_leet.2; let distance = sent_time - latest_leet.2;
if distance.num_hours() < 24 { if distance.num_hours() < 1 {
return Err(LeetError::AlreadyHitLeet); return Err(LeetError::AlreadyHitLeet);
} }
} }
@ -71,16 +69,10 @@ async fn check_leet(username: &str, pool: &AnyPool) -> Result<(), LeetError> {
Ok(()) Ok(())
} }
async fn log_leet(username: &str, pool: &AnyPool) -> Result<(), sqlx::Error> { async fn log_leet(username: &str, sent_time: NaiveDateTime, pool: &AnyPool) -> Result<(), sqlx::Error> {
let sql_now = match pool.any_kind() { sqlx::query("insert into leet_log (username, log_time) values ($1, $2)")
sqlx::any::AnyKind::Postgres => "now()",
sqlx::any::AnyKind::Sqlite => "datetime()"
};
let query_sql = format!("insert into leet_log (username, log_time) values ($1, {})", sql_now);
sqlx::query(&query_sql)
.bind(username) .bind(username)
.bind(sent_time)
.execute(pool) .execute(pool)
.await?; .await?;
@ -107,7 +99,11 @@ fn message_ok() -> Result<(), MessageHandlerError> {
#[command(rename = "lowercase", description = "score commands")] #[command(rename = "lowercase", description = "score commands")]
enum LeetCommand { enum LeetCommand {
#[command(description = "get scores")] #[command(description = "get scores")]
Scores Scores,
#[command(description = "get current time info")]
Time,
#[command(description = "show help about leetbot")]
Help
} }
#[derive(Error,Debug)] #[derive(Error,Debug)]
@ -140,7 +136,11 @@ async fn leet_message_handler(message: Message, bot: AutoSend<Bot>, pool: AnyPoo
.username.clone() .username.clone()
.ok_or(MessageHandlerError::NoUsername)?; .ok_or(MessageHandlerError::NoUsername)?;
let leet_check = check_leet(&username, &pool).await; let message_sent_time = message.date
.with_timezone(&Local)
.naive_local();
let leet_check = check_leet(&username, message_sent_time, &pool).await;
if let Err(e) = leet_check { if let Err(e) = leet_check {
match e { match e {
@ -154,7 +154,7 @@ async fn leet_message_handler(message: Message, bot: AutoSend<Bot>, pool: AnyPoo
return message_ok(); return message_ok();
} }
log_leet(&username, &pool).await?; log_leet(&username, message_sent_time, &pool).await?;
let scores = get_scores(&pool).await?; let scores = get_scores(&pool).await?;
@ -178,6 +178,28 @@ async fn leet_command_handler(message: Message, cmd: LeetCommand, bot: AutoSend<
let msg = format!("Leet scores:\n{}", scores_str); let msg = format!("Leet scores:\n{}", scores_str);
bot.send_message(message.chat.id, msg).await?; bot.send_message(message.chat.id, msg).await?;
},
LeetCommand::Time => {
let message_time = message.date
.with_timezone(&Local)
.naive_local();
let current_time = Local::now().time();
let leet_time = NaiveTime::from_hms(13,37,30);
let distance_to_leet = leet_time - current_time;
let msg = format!("My current time is: {}
message was sent at: {}
leet is at: {}, in {} seconds",
current_time,
message_time,
leet_time,
distance_to_leet.num_seconds()
);
bot.send_message(message.chat.id, msg).await?;
},
LeetCommand::Help => {
bot.send_message(message.chat.id, LeetCommand::descriptions().to_string()).await?;
} }
} }
@ -212,8 +234,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Dispatcher::builder(bot, handler) Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![db_pool]) .dependencies(dptree::deps![db_pool])
.default_handler(|upd| async move { .default_handler(|_upd| async move {
println!("unhandled update: {:?}", upd);
}) })
.error_handler(LoggingErrorHandler::with_custom_text("error during dispatch")) .error_handler(LoggingErrorHandler::with_custom_text("error during dispatch"))
.enable_ctrlc_handler() .enable_ctrlc_handler()