#![allow(unused_imports, unused_variables, reason = "Not implemented yet.")]
mod common;
mod inline;
mod message;
use std::sync::Arc;
use crate::{config::BotConfig, util::Database, Config};
use anyhow::{Context, Result};
use argh::FromArgs;
use frankenstein::{client_reqwest::Bot, AsyncTelegramApi, Error, GetUpdatesParams, UpdateContent};
use log::{debug, error, info};
use semantic_search::ApiClient;
use tokio::sync::Mutex;
type BotResult<T> = Result<T, Error>;
#[derive(FromArgs, PartialEq, Eq, Debug)]
#[argh(subcommand, name = "tg", help_triggers("-h", "--help"))]
pub struct Telegram {
}
impl Telegram {
pub async fn execute(&self, config: Config) -> Result<()> {
let mut db = Database::open(".sense/index.db3", false)
.await
.with_context(|| "Failed to open database, consider indexing first.")?;
let api = ApiClient::new(&config.api.key, config.api.model)?;
let BotConfig { token, .. } = &config.bot;
if token.is_empty() {
anyhow::bail!("No token provided for the Telegram bot.");
}
let bot = Bot::new(token); let me = bot.get_me().await?.result;
info!("Initializing stickers...");
common::init_stickers(&bot, &me, &mut db, &config.bot).await?;
info!("Initialized stickers, start handling updates...");
let bot = Box::leak(Box::new(bot));
let me = Box::leak(Box::new(me));
let api = Box::leak(Box::new(api));
let bot_config = Box::leak(Box::new(config.bot));
let whitelist = &bot_config.whitelist;
let db = Arc::new(Mutex::new(db));
let mut update_params = GetUpdatesParams::builder().build();
loop {
match bot.get_updates(&update_params).await {
Ok(updates) => {
for update in updates.result {
debug!("Received update: {update:?}");
update_params.offset.replace((update.update_id + 1).into());
match update.content {
UpdateContent::Message(msg) => {
let Some(sender) = &msg.from else {
continue;
};
let sender = sender.id;
if !whitelist.is_empty() && !whitelist.contains(&sender) {
continue;
}
tokio::spawn(message::message_handler(
bot,
me,
msg,
db.clone(),
api,
bot_config,
));
}
UpdateContent::InlineQuery(query) => {
let sender = query.from.id;
if !whitelist.is_empty() && !whitelist.contains(&sender) {
continue;
}
tokio::spawn(inline::inline_handler(
bot,
me,
query,
db.clone(),
api,
bot_config,
));
}
_ => {}
}
}
}
Err(error) => {
error!("Failed to get updates: {error:?}");
}
};
}
}
}