mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-11 04:21:12 +01:00
commit
d9e91c93e5
7 changed files with 40 additions and 26 deletions
|
@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- `StatefulListener`.
|
||||
- Emit not only errors but also warnings and general information from teloxide, when set up by `enable_logging!`.
|
||||
- Use `i64` instead of `i32` for `user_id` in `html::user_mention` and `markdown::user_mention`.
|
||||
- Updated to `teloxide-core` `v0.3.0` (see it's [changelog](https://github.com/teloxide/teloxide-core/blob/master/CHANGELOG.md#030---2021-07-05) for more)
|
||||
|
||||
### Fixed
|
||||
|
||||
|
|
10
Cargo.toml
10
Cargo.toml
|
@ -63,14 +63,14 @@ full = [
|
|||
]
|
||||
|
||||
[dependencies]
|
||||
#teloxide-core = { version = "0.2.1", default-features = false }
|
||||
teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", rev = "897ba7c941b651cf6b7e614b7d373d14426ed1da", default-features = false }
|
||||
teloxide-core = { version = "0.3.0", default-features = false }
|
||||
#teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", rev = "...", default-features = false }
|
||||
teloxide-macros = { version = "0.4", optional = true }
|
||||
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
tokio = { version = "1.2", features = ["fs"] }
|
||||
tokio = { version = "1.8", features = ["fs"] }
|
||||
tokio-util = "0.6"
|
||||
tokio-stream = "0.1"
|
||||
|
||||
|
@ -94,14 +94,14 @@ sqlx = { version = "0.5", optional = true, default-features = false, features =
|
|||
redis = { version = "0.20", features = ["tokio-comp"], optional = true }
|
||||
serde_cbor = { version = "0.11", optional = true }
|
||||
bincode = { version = "1.3", optional = true }
|
||||
frunk = { version = "0.3", optional = true }
|
||||
frunk = { version = "0.4", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
smart-default = "0.6.0"
|
||||
rand = "0.8.3"
|
||||
pretty_env_logger = "0.4.0"
|
||||
lazy_static = "1.4.0"
|
||||
tokio = { version = "1.2.0", features = ["fs", "rt-multi-thread", "macros"] }
|
||||
tokio = { version = "1.8", features = ["fs", "rt-multi-thread", "macros"] }
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
|
|
@ -11,6 +11,7 @@ teloxide = { path = "../../", features = ["macros", "auto-send"] }
|
|||
log = "0.4.8"
|
||||
pretty_env_logger = "0.4.0"
|
||||
tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] }
|
||||
chrono = "0.4"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use std::{convert::TryInto, error::Error, str::FromStr};
|
||||
use std::{error::Error, str::FromStr};
|
||||
|
||||
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||
|
||||
use teloxide::types::ChatPermissions;
|
||||
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
|
||||
use teloxide::{prelude::*, types::{ChatPermissions, Me}, utils::command::BotCommand};
|
||||
|
||||
// Derive BotCommand to parse text with a command into this enumeration.
|
||||
//
|
||||
|
@ -24,12 +23,12 @@ enum Command {
|
|||
Kick,
|
||||
#[command(description = "ban user in chat.")]
|
||||
Ban {
|
||||
time: u32,
|
||||
time: u64,
|
||||
unit: UnitOfTime,
|
||||
},
|
||||
#[command(description = "mute user in chat.")]
|
||||
Mute {
|
||||
time: u32,
|
||||
time: u64,
|
||||
unit: UnitOfTime,
|
||||
},
|
||||
Help,
|
||||
|
@ -54,18 +53,18 @@ impl FromStr for UnitOfTime {
|
|||
}
|
||||
|
||||
// Calculates time of user restriction.
|
||||
fn calc_restrict_time(time: u32, unit: UnitOfTime) -> u32 {
|
||||
fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration {
|
||||
match unit {
|
||||
UnitOfTime::Hours => time * 3600,
|
||||
UnitOfTime::Minutes => time * 60,
|
||||
UnitOfTime::Seconds => time,
|
||||
UnitOfTime::Hours => Duration::hours(time as i64),
|
||||
UnitOfTime::Minutes => Duration::minutes(time as i64),
|
||||
UnitOfTime::Seconds => Duration::seconds(time as i64),
|
||||
}
|
||||
}
|
||||
|
||||
type Cx = UpdateWithCx<AutoSend<Bot>, Message>;
|
||||
|
||||
// Mute a user with a replied message.
|
||||
async fn mute_user(cx: &Cx, time: u32) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
async fn mute_user(cx: &Cx, time: Duration) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match cx.update.reply_to_message() {
|
||||
Some(msg1) => {
|
||||
cx.requester
|
||||
|
@ -74,7 +73,12 @@ async fn mute_user(cx: &Cx, time: u32) -> Result<(), Box<dyn Error + Send + Sync
|
|||
msg1.from().expect("Must be MessageKind::Common").id,
|
||||
ChatPermissions::default(),
|
||||
)
|
||||
.until_date((cx.update.date + time as i32).try_into().unwrap())
|
||||
.until_date(
|
||||
DateTime::<Utc>::from_utc(
|
||||
NaiveDateTime::from_timestamp(cx.update.date as i64, 0),
|
||||
Utc,
|
||||
) + time,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
None => {
|
||||
|
@ -102,7 +106,7 @@ async fn kick_user(cx: &Cx) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|||
}
|
||||
|
||||
// Ban a user with replied message.
|
||||
async fn ban_user(cx: &Cx, time: u32) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
async fn ban_user(cx: &Cx, time: Duration) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match cx.update.reply_to_message() {
|
||||
Some(message) => {
|
||||
cx.requester
|
||||
|
@ -110,7 +114,12 @@ async fn ban_user(cx: &Cx, time: u32) -> Result<(), Box<dyn Error + Send + Sync>
|
|||
cx.update.chat_id(),
|
||||
message.from().expect("Must be MessageKind::Common").id,
|
||||
)
|
||||
.until_date((cx.update.date + time as i32).try_into().unwrap())
|
||||
.until_date(
|
||||
DateTime::<Utc>::from_utc(
|
||||
NaiveDateTime::from_timestamp(cx.update.date as i64, 0),
|
||||
Utc,
|
||||
) + time,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
None => {
|
||||
|
@ -142,6 +151,7 @@ async fn run() {
|
|||
|
||||
let bot = Bot::from_env().auto_send();
|
||||
|
||||
let bot_name: String = panic!("Your bot's name here");
|
||||
let Me { user: bot_user, .. } = bot.get_me().await.unwrap();
|
||||
let bot_name = bot_user.username.expect("Bots must have usernames");
|
||||
teloxide::commands_repl(bot, bot_name, action).await;
|
||||
}
|
||||
|
|
|
@ -16,8 +16,8 @@ log = "0.4.8"
|
|||
pretty_env_logger = "0.4.0"
|
||||
derive_more = "0.99.9"
|
||||
|
||||
frunk = "0.3.1"
|
||||
frunk_core = "0.3.1"
|
||||
frunk = "0.4"
|
||||
frunk_core = "0.4"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
|
|
@ -8,7 +8,7 @@ use tokio::sync::mpsc;
|
|||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||
use warp::Filter;
|
||||
|
||||
use reqwest::StatusCode;
|
||||
use reqwest::{StatusCode, Url};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -30,7 +30,7 @@ pub async fn webhook(bot: AutoSend<Bot>) -> impl update_listeners::UpdateListene
|
|||
// Heroku host example .: "heroku-ping-pong-bot.herokuapp.com"
|
||||
let host = env::var("HOST").expect("have HOST env variable");
|
||||
let path = format!("bot{}", teloxide_token);
|
||||
let url = format!("https://{}/{}", host, path);
|
||||
let url = Url::parse(&format!("https://{}/{}", host, path)).unwrap();
|
||||
|
||||
bot.set_webhook(url).await.expect("Cannot setup a webhook");
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use tokio::sync::mpsc;
|
|||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||
use warp::Filter;
|
||||
|
||||
use reqwest::StatusCode;
|
||||
use reqwest::{StatusCode, Url};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -21,9 +21,11 @@ async fn handle_rejection(error: warp::Rejection) -> Result<impl warp::Reply, In
|
|||
}
|
||||
|
||||
pub async fn webhook(bot: AutoSend<Bot>) -> impl update_listeners::UpdateListener<Infallible> {
|
||||
let url = Url::parse("Your HTTPS ngrok URL here. Get it by `ngrok http 80`").unwrap();
|
||||
|
||||
// You might want to specify a self-signed certificate via .certificate
|
||||
// method on SetWebhook.
|
||||
bot.set_webhook("Your HTTPS ngrok URL here. Get it by `ngrok http 80`")
|
||||
bot.set_webhook(url)
|
||||
.await
|
||||
.expect("Cannot setup a webhook");
|
||||
|
||||
|
|
Loading…
Reference in a new issue