From b6f0eb82fb6e65656c959e9947566acf9868b3b3 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:48:56 +0200 Subject: [PATCH] implement telegram-bot-api StatefulSet and ClusterIP service --- k8s/05_tg-stateful-set.yml | 13 +++++++++++++ k8s/06_tg-service.yml | 14 ++++++++++++++ src/bot/bot.rs | 12 ++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 k8s/06_tg-service.yml diff --git a/k8s/05_tg-stateful-set.yml b/k8s/05_tg-stateful-set.yml index debffd6..3e81f97 100644 --- a/k8s/05_tg-stateful-set.yml +++ b/k8s/05_tg-stateful-set.yml @@ -23,6 +23,19 @@ spec: ports: - containerPort: 8081 name: tg + env: + - name: API_ID + valueFrom: + secretKeyRef: + name: secret + key: BOT_API_ID + - name: API_HASH + valueFrom: + secretKeyRef: + name: secret + key: BOT_API_HASH + - name: HTTP_PORT + value: "8081" volumeMounts: - name: tg-data mountPath: /app diff --git a/k8s/06_tg-service.yml b/k8s/06_tg-service.yml new file mode 100644 index 0000000..78f3976 --- /dev/null +++ b/k8s/06_tg-service.yml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: tg-service + namespace: mk-dl-bot +spec: + selector: + app: mk-dl-bot + service: tg + type: ClusterIP + ports: + - name: tg + protocol: TCP + port: 8081 diff --git a/src/bot/bot.rs b/src/bot/bot.rs index fa6c511..cdebd41 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -1,9 +1,10 @@ use anyhow; use rust_i18n::t; -use std::str; +use url::Url; +use std::str::{self, FromStr}; use std::time::Duration; use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler}; -use teloxide::types::{Me, MessageKind, MessageNewChatMembers, UpdateKind}; +use teloxide::types::{InputFile, InputMediaVideo, Me, MessageKind, MessageNewChatMembers, UpdateKind}; use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; use tracing::{event, Level}; @@ -24,7 +25,9 @@ use super::start::{cmd_start, handle_my_chat_member}; pub async fn bot_main(db: DbPool) -> anyhow::Result<()> { event!(Level::INFO, "start"); - let bot = Bot::new(unwrap_env("BOT_TOKEN")); + let bot = Bot::new(unwrap_env("BOT_TOKEN")) + .set_api_url(Url::from_str(&unwrap_env("BOT_API_URL"))?); + let listener = Polling::builder(bot.clone()) .timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT"))) .limit(parse_env("POLLING_LIMIT")) @@ -129,7 +132,8 @@ enum Command { } async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult { - bot.send_message(msg.chat.id, t!("test_response")).await?; + //bot.send_message(msg.chat.id, t!("test_response")).await?; + bot.send_video(msg.chat.id, InputFile::url(Url::from_str("http://mkdlbot.ddns.net/output3.mp4")?)).await?; Ok(()) }