#![deny(missing_docs)]
use std::env;
use teloxide::dispatching::update_listeners;
use teloxide::dispatching::update_listeners::UpdateListener;
use teloxide::requests::Requester;
use teloxide::RequestError;
#[cfg(feature = "either")]
#[doc(hidden)]
pub use crate::either::Either;
#[cfg(feature = "either")]
#[doc(hidden)]
mod either;
#[cfg(feature = "webhook")]
pub mod webhook;
#[cfg(test)]
mod tests;
pub enum Listener {
Polling,
#[cfg(feature = "webhook")]
Webhook(webhook::HTTPConfig),
}
impl Listener {
#[must_use]
pub fn from_env() -> Self {
Self::from_env_with_prefix("TELOXIDE_")
}
#[must_use]
#[allow(unused_variables)]
pub fn from_env_with_prefix(prefix: &str) -> Self {
if let (Ok(base), Ok(path), Ok(addr)) = (
env::var(format!("{}WEBHOOK_URL", prefix)),
env::var(format!("{}WEBHOOK_PATH", prefix)),
env::var(format!("{}BIND_ADDR", prefix)),
) {
#[cfg(not(feature = "webhook"))]
{
tracing::error!("webhook support not enabled, fallback to polling");
Self::Polling
}
#[cfg(feature = "webhook")]
Self::Webhook(webhook::HTTPConfig {
base_url: base.parse().expect("invalid base url"),
path,
addr: addr.parse().expect("invalid bind address"),
})
} else {
Self::Polling
}
}
#[cfg(feature = "webhook")]
#[allow(clippy::future_not_send)]
pub async fn build<R>(
self,
bot: R,
) -> Either<impl UpdateListener<R::Err>, impl UpdateListener<R::Err>>
where
R: Requester<Err = RequestError> + Send + 'static,
<R as Requester>::GetUpdates: Send,
{
match self {
Listener::Polling => Either::Left(update_listeners::polling_default(bot).await),
Listener::Webhook(config) => Either::Right(webhook::listener(bot, config).await),
}
}
#[cfg(not(feature = "webhook"))]
#[allow(clippy::future_not_send)]
pub async fn build<R>(self, bot: R) -> impl UpdateListener<R::Err>
where
R: Requester<Err = RequestError> + Send + 'static,
<R as Requester>::GetUpdates: Send,
{
match self {
Listener::Polling => update_listeners::polling_default(bot).await,
}
}
}