|
| 1 | +use crate::telegram_types::GetMe; |
| 2 | +use std::{ |
| 3 | + pin::Pin, |
| 4 | + task::{Context, Poll}, |
| 5 | +}; |
| 6 | +use tower::Service; |
| 7 | + |
| 8 | +/* |
| 9 | + async fn get_me(ctx: Content) -> GetMe { |
| 10 | + ctx.get_me().await |
| 11 | + } |
| 12 | +*/ |
| 13 | +#[derive(Debug, thiserror::Error)] |
| 14 | +#[error("{0}")] |
| 15 | +pub enum Error {} |
| 16 | + |
| 17 | +pub type Result<T, E = Error> = core::result::Result<T, E>; |
| 18 | + |
| 19 | +trait BotApi { |
| 20 | + type Option: Default; |
| 21 | + type Connected; |
| 22 | + |
| 23 | + fn connected(&self) -> Result<Self::Connected>; |
| 24 | + |
| 25 | + // Provide other function |
| 26 | +} |
| 27 | + |
| 28 | +/// Bot is generic over the intended api just like sqlx |
| 29 | +/// --- discord |
| 30 | +/// ---- --- telegram ---> unified api to talk to outside world |
| 31 | +/// --- x |
| 32 | +/// |
| 33 | +/// Bot is service and expected to run forever |
| 34 | +/// |
| 35 | +/// ```no_run |
| 36 | +/// let bot_instance = Bot::<Telegram>::new(|option| { |
| 37 | +/// option.url = "https://web.api.telegram"; |
| 38 | +/// // other configs |
| 39 | +/// option |
| 40 | +/// }); |
| 41 | +/// |
| 42 | +/// bot_instance |
| 43 | +/// .setup_listener(3000) |
| 44 | +/// .with_graceful_shutdown() |
| 45 | +/// .await?; |
| 46 | +/// ``` |
| 47 | +/// or be plugges as service example assuming we using axum |
| 48 | +/// |
| 49 | +/// ```no_run |
| 50 | +/// use axum::Router; |
| 51 | +/// |
| 52 | +/// let bot_instance = Bot::<Telegram>::new(|option| { |
| 53 | +/// option.url = "https://web.api.telegram"; |
| 54 | +/// // other configs |
| 55 | +/// option |
| 56 | +/// }); |
| 57 | +/// |
| 58 | +/// let app = Router::new().route("/webhook", post(bot_instance)); |
| 59 | +/// |
| 60 | +/// axum::serve(listener, app).await?; |
| 61 | +/// ``` |
| 62 | +#[derive(Debug, new)] |
| 63 | +pub struct Bot<A> { |
| 64 | + bot_api: A, |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Debug, Clone, new)] |
| 68 | +pub struct Telegram { |
| 69 | + inner: TelegramOption, |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Debug, Clone, Default)] |
| 73 | +pub struct TelegramOption {} |
| 74 | + |
| 75 | +mod telegram_types { |
| 76 | + #[derive(Debug, Clone)] |
| 77 | + pub struct Updates {} |
| 78 | + |
| 79 | + #[derive(Debug, Clone)] |
| 80 | + pub struct GetMe {} |
| 81 | +} |
| 82 | + |
| 83 | +impl BotApi for Telegram { |
| 84 | + type Option = TelegramOption; |
| 85 | + |
| 86 | + // assuming this is what telegram gives back when connected let hope |
| 87 | + type Connected = telegram_types::GetMe; |
| 88 | + |
| 89 | + fn connected(&self) -> Result<Self::Connected> { |
| 90 | + Ok(GetMe {}) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// This pattern don't work well if Self needs more than option look into it |
| 95 | +impl From<TelegramOption> for Telegram { |
| 96 | + fn from(value: TelegramOption) -> Self { |
| 97 | + Self::new(value) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl Bot<Telegram> { |
| 102 | + fn on<H>(self, arg: &str, handler: H) -> Result<Self> { |
| 103 | + // the idea we wanna register arg here with the handler to look it upm later |
| 104 | + |
| 105 | + Ok(self) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// This is expected to be generic all bot api to work with this let proceed |
| 110 | +pub trait Handler {} |
| 111 | + |
| 112 | +pub fn handler<H>(handler: H) |
| 113 | +where |
| 114 | + H: Handler, |
| 115 | +{ |
| 116 | + // we gonna drop it for now but you get the idea we wanna work with handler here |
| 117 | + drop(handler) |
| 118 | +} |
| 119 | + |
| 120 | +impl<F> Handler for F where F: FnOnce() {} |
| 121 | + |
| 122 | +impl<A> Service<telegram_types::Updates> for Bot<A> { |
| 123 | + type Response = (); |
| 124 | + type Error = Error; |
| 125 | + type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>; |
| 126 | + |
| 127 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 128 | + Poll::Ready(Ok(())) |
| 129 | + } |
| 130 | + |
| 131 | + fn call(&mut self, req: telegram_types::Updates) -> Self::Future { |
| 132 | + Box::pin(async move { |
| 133 | + // when update is recieved we wanna call self |
| 134 | + trace!("Recieved update: {:?}", req); |
| 135 | + Ok(()) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[cfg(test)] |
| 141 | +mod tests { |
| 142 | + use crate::{Bot, Telegram, TelegramOption, handler, telegram_types::Updates}; |
| 143 | + use anyhow::Result; |
| 144 | + use tower::ServiceExt as _; |
| 145 | + use tracing_subscriber::EnvFilter; |
| 146 | + |
| 147 | + #[tokio::test] |
| 148 | + async fn test_bot_with_long_polling_or_webhook() -> anyhow::Result<()> { |
| 149 | + tracing_subscriber::fmt() |
| 150 | + .with_env_filter(EnvFilter::new("trace")) |
| 151 | + .init(); |
| 152 | + |
| 153 | + trace!("Subscriber installed"); |
| 154 | + |
| 155 | + // assuming this is a async function with extractor in it you get the idea |
| 156 | + fn start_handler() { |
| 157 | + // let me = ctx.request::<GetMe>().await?; |
| 158 | + // let message = text!(chat_id, format!("Hello, I am {}", me.username)); |
| 159 | + |
| 160 | + // the reply function would definently be provided via extension |
| 161 | + // let result = ctx.reply(message).await?: |
| 162 | + // inside send function |
| 163 | + // send { |
| 164 | + // convert it into |
| 165 | + // let request = message.into(); |
| 166 | + |
| 167 | + // we didn't block here |
| 168 | + // let res = tokio::task::spawn(request).await; |
| 169 | + // return the res |
| 170 | + // res |
| 171 | + } |
| 172 | + let bot: Bot<Telegram> = Bot::new(Telegram::new(TelegramOption {})).on("/start", handler(start_handler))?; |
| 173 | + |
| 174 | + // This is how we wanna test our bot |
| 175 | + let res = bot.oneshot(Updates {}).await?; |
| 176 | + assert_eq!(res, ()); |
| 177 | + |
| 178 | + // What if we wanna serve it we are thinking Bot could be injected as service like in axum as a service |
| 179 | + // or |
| 180 | + |
| 181 | + // as teloxide dispatching method or long poll we will explore both |
| 182 | + Ok(()) |
| 183 | + } |
| 184 | +} |
0 commit comments