mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-11 04:21:12 +01:00
Implement webhooks
This commit is contained in:
parent
e0ab813554
commit
6e486f0d78
3 changed files with 49 additions and 10 deletions
|
@ -47,6 +47,8 @@ serde_with_macros = "1.0.1"
|
||||||
|
|
||||||
teloxide-macros = "0.2.1"
|
teloxide-macros = "0.2.1"
|
||||||
|
|
||||||
|
warp = "0.2.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
smart-default = "0.6.0"
|
smart-default = "0.6.0"
|
||||||
rand = "0.7.3"
|
rand = "0.7.3"
|
||||||
|
|
|
@ -101,14 +101,24 @@
|
||||||
//! [webhook]: https://en.wikipedia.org/wiki/Webhook
|
//! [webhook]: https://en.wikipedia.org/wiki/Webhook
|
||||||
|
|
||||||
use futures::{stream, Stream, StreamExt};
|
use futures::{stream, Stream, StreamExt};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use warp::Filter;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bot::Bot,
|
bot::Bot,
|
||||||
|
prelude::ResponseResult,
|
||||||
requests::Request,
|
requests::Request,
|
||||||
types::{AllowedUpdate, Update},
|
types::{AllowedUpdate, InputFile, Update},
|
||||||
RequestError,
|
RequestError,
|
||||||
};
|
};
|
||||||
use std::{convert::TryInto, sync::Arc, time::Duration};
|
use reqwest::Url;
|
||||||
|
use std::{
|
||||||
|
convert::{Infallible, TryInto},
|
||||||
|
net::SocketAddr,
|
||||||
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
/// A generic update listener.
|
/// A generic update listener.
|
||||||
pub trait UpdateListener<E>: Stream<Item = Result<Update, E>> {
|
pub trait UpdateListener<E>: Stream<Item = Result<Update, E>> {
|
||||||
|
@ -198,7 +208,34 @@ pub fn polling(
|
||||||
.flatten()
|
.flatten()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO implement webhook (this actually require webserver and probably we
|
pub async fn webhook(
|
||||||
// should add cargo feature that adds webhook)
|
bot: Arc<Bot>,
|
||||||
//pub fn webhook<'a>(bot: &'a cfg: WebhookConfig) -> Updater<impl
|
addr: SocketAddr,
|
||||||
// Stream<Item=Result<Update, ???>> + 'a> {}
|
url: Url,
|
||||||
|
certificate: Option<InputFile>,
|
||||||
|
max_connections: Option<i32>,
|
||||||
|
allowed_updates: Option<Vec<AllowedUpdate>>,
|
||||||
|
) -> ResponseResult<impl UpdateListener<Infallible>> {
|
||||||
|
let mut webhook = bot.set_webhook(url.to_string());
|
||||||
|
webhook.certificate = certificate;
|
||||||
|
webhook.max_connections = max_connections;
|
||||||
|
webhook.allowed_updates = allowed_updates;
|
||||||
|
webhook.send().await?;
|
||||||
|
|
||||||
|
let (tx, rx) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
|
let server = warp::post().and(warp::path(url)).and(warp::body::json()).map(
|
||||||
|
move |update: Update| {
|
||||||
|
tx.send(Ok(update.clone()))
|
||||||
|
.expect("Cannot send an update from webhook");
|
||||||
|
""
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
// TODO: Tls
|
||||||
|
warp::serve(server).run(addr).await;
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(rx)
|
||||||
|
}
|
||||||
|
|
|
@ -29,10 +29,10 @@ use std::sync::Arc;
|
||||||
pub struct SetWebhook {
|
pub struct SetWebhook {
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
bot: Arc<Bot>,
|
bot: Arc<Bot>,
|
||||||
url: String,
|
pub(crate) url: String,
|
||||||
certificate: Option<InputFile>,
|
pub(crate) certificate: Option<InputFile>,
|
||||||
max_connections: Option<i32>,
|
pub(crate) max_connections: Option<i32>,
|
||||||
allowed_updates: Option<Vec<AllowedUpdate>>,
|
pub(crate) allowed_updates: Option<Vec<AllowedUpdate>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
|
|
Loading…
Reference in a new issue