Skip to content

Commit

Permalink
(Feat) Changed bot type to Throttle. Preventing update listener errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xairaven committed Jul 31, 2024
1 parent 9f89524 commit d87a07f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 8 additions & 5 deletions media_fetch_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::errors::user_input::UserInputError;
use crate::link_type::LinkType;
use rust_i18n::t;
use teloxide::{prelude::*, utils::command::BotCommands};
use teloxide::adaptors::throttle::{Limits};
use teloxide::types::{ParseMode};
use std::{process};
use pretty_env_logger::env_logger::Target;
use teloxide::adaptors::Throttle;

mod bot_commands;
mod bot_config;
Expand Down Expand Up @@ -39,7 +41,8 @@ async fn main() {

log::info!("Starting bot...");

let bot = Bot::new(&bot_config.token);
let bot = Bot::new(&bot_config.token)
.throttle(Limits::default());

Dispatcher::builder(bot, Update::filter_message().endpoint(handle_message))
.dependencies(dptree::deps![bot_config.name,
Expand All @@ -51,7 +54,7 @@ async fn main() {
.await;
}

async fn handle_message(bot: Bot, msg: Message,
async fn handle_message(bot: Throttle<Bot>, msg: Message,
bot_name: String,
tiktok_api_key: Option<String>,
instagram_api_key: Option<String>) -> ResponseResult<()> {
Expand Down Expand Up @@ -87,7 +90,7 @@ async fn handle_message(bot: Bot, msg: Message,
}
}

async fn handle_command(bot: Bot, msg: Message, cmd: BotCommand) -> ResponseResult<()> {
async fn handle_command(bot: Throttle<Bot>, msg: Message, cmd: BotCommand) -> ResponseResult<()> {
match cmd {
BotCommand::Help => bot.send_message(msg.chat.id, t!(&BotCommand::Help.to_string()))
.parse_mode(ParseMode::Html)
Expand All @@ -101,7 +104,7 @@ async fn handle_command(bot: Bot, msg: Message, cmd: BotCommand) -> ResponseResu
}

async fn handle_tiktok_link(link: &str, api_key: Option<String>,
bot: &Bot, msg: &Message) -> ResponseResult<()> {
bot: &Throttle<Bot>, msg: &Message) -> ResponseResult<()> {
let results
= tiktok::handler::get_results(api_key, link.to_string()).await;

Expand All @@ -111,7 +114,7 @@ async fn handle_tiktok_link(link: &str, api_key: Option<String>,
}

async fn handle_instagram_link(link: &str, api_key: Option<String>,
bot: &Bot, msg: &Message) -> ResponseResult<()> {
bot: &Throttle<Bot>, msg: &Message) -> ResponseResult<()> {
let results = instagram::handler::get_results(api_key, link.to_string()).await;

rapid_api::send_results(results, bot, msg, link).await?;
Expand Down
3 changes: 2 additions & 1 deletion media_fetch_bot/src/rapid_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use teloxide::adaptors::Throttle;
use teloxide::Bot;
use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::{Message, Requester, ResponseResult};
Expand All @@ -11,7 +12,7 @@ pub mod media_format;
pub mod raw_media;

type RapidApiResults = Result<(String, HashMap<MediaFormat, Vec<InputMedia>>), ErrorType>;
pub async fn send_results(results: RapidApiResults, bot: &Bot, msg: &Message, link: &str)
pub async fn send_results(results: RapidApiResults, bot: &Throttle<Bot>, msg: &Message, link: &str)
-> ResponseResult<()> {
match results {
Ok(tuple) => {
Expand Down

0 comments on commit d87a07f

Please sign in to comment.