Skip to main content

echobot/
echobot.rs

1use ferrisgram::error::{GroupIteration, Result};
2use ferrisgram::ext::filters::message;
3use ferrisgram::ext::handlers::{CommandHandler, MessageHandler};
4use ferrisgram::ext::{Context, Dispatcher, Updater};
5use ferrisgram::types::LinkPreviewOptions;
6use ferrisgram::Bot;
7use std::env;
8
9#[allow(unused)]
10#[tokio::main]
11async fn main() {
12    let bot_token = match env::var("FERRIS_BOT_TOKEN") {
13        Ok(s) => s,
14        Err(err) => panic!("failed to start bot: {}", err),
15    };
16    // This function creates a new bot instance and the error is handled accordingly
17    let bot = match Bot::new(&bot_token, None).await {
18        Ok(bot) => bot,
19        Err(error) => panic!("failed to create bot: {}", error),
20    };
21
22    // dispatcher is a part of internal functionality of updater
23    // you may use it for adding handlers.
24    let mut dispatcher = &mut Dispatcher::new(&bot);
25
26    // add_handler method maps the provided handler in group 0 automatically
27    dispatcher.add_handler(CommandHandler::new("start", start));
28
29    // add_handler_to_group is used to map the provided handler to a group manually.
30    // note that handler groups are processed in ascending order.
31    dispatcher.add_handler_to_group(
32        MessageHandler::new(
33            echo,
34            // This will restrict our echo function to the messages which
35            // contain either text or a caption.
36            message::Text::filter().or(message::Caption::filter()),
37        ),
38        1,
39    );
40
41    let mut updater = Updater::new(&bot, dispatcher);
42
43    // This method will start long polling through the getUpdates method
44    updater.start_polling(true).await;
45}
46
47// This is our callable function for the command handler that we declared earlier
48// It will be triggered when someone send /start to the bot.
49async fn start(bot: Bot, ctx: Context) -> Result<GroupIteration> {
50    // Same logic as chat applies on unwrapping effective message here.
51    let msg = ctx.effective_message.unwrap();
52    let mut link_preview_options = LinkPreviewOptions::new();
53    link_preview_options.is_disabled = Some(true);
54    // Ferrisgram offers some custom helpers which make your work easy
55    // Here we have used one of those helpers known as msg.reply
56    msg.reply(
57        &bot,
58        "Hey! I am an echo bot built using [Ferrisgram](https://github.com/ferrisgram/ferrisgram).
59I will repeat your messages.",
60    )
61    // this method will ensure that our text will be sent with markdown formatting.
62    .parse_mode("markdown".to_string())
63    .link_preview_options(link_preview_options)
64    // You must use this send() method in order to send the request to the API
65    .send()
66    .await?;
67
68    // GroupIteration::EndGroups will end iteration of groups for an update.
69    // This means that rest of the pending groups and their handlers won't be checked
70    // for this particular update.
71    Ok(GroupIteration::EndGroups)
72}
73
74// This is our callable function for our message handler which will be used to
75// repeat the text.
76async fn echo(bot: Bot, ctx: Context) -> Result<GroupIteration> {
77    // Command Handler recieves message updates which have chat as a compulsory field.
78    // Hence we can unwrap effective chat without checking if it is none.
79    let chat = ctx.effective_chat.unwrap();
80    // Same logic as chat applies on unwrapping effective message here.
81    let msg = ctx.effective_message.unwrap();
82    // Every api method creates a builder which contains various parameters of that respective method.
83    bot.copy_message(chat.id, chat.id, msg.message_id)
84        // You must use this send() method in order to send the request to the API
85        .send()
86        .await?;
87
88    // GroupIteration::EndGroups will end iteration of groups for an update.
89    // This means that rest of the pending groups and their handlers won't be checked
90    // for this particular update.
91    Ok(GroupIteration::EndGroups)
92}