inline/
inline.rs

1use teloxide::{
2    prelude::*,
3    types::{
4        InlineQueryResult, InlineQueryResultArticle, InputMessageContent, InputMessageContentText,
5    },
6};
7
8#[tokio::main]
9async fn main() {
10    pretty_env_logger::init();
11    log::info!("Starting inline bot...");
12
13    let bot = Bot::from_env();
14
15    let handler = Update::filter_inline_query().branch(dptree::endpoint(
16        |bot: Bot, q: InlineQuery| async move {
17            // First, create your actual response
18            let google_search = InlineQueryResultArticle::new(
19                // Each item needs a unique ID, as well as the response container for the
20                // items. These can be whatever, as long as they don't
21                // conflict.
22                "01".to_string(),
23                // What the user will actually see
24                "Google Search",
25                // What message will be sent when clicked/tapped
26                InputMessageContent::Text(InputMessageContentText::new(format!(
27                    "https://www.google.com/search?q={}",
28                    q.query,
29                ))),
30            );
31            // While constructing them from the struct itself is possible, it is preferred
32            // to use the builder pattern if you wish to add more
33            // information to your result. Please refer to the documentation
34            // for more detailed information about each field. https://docs.rs/teloxide/latest/teloxide/types/struct.InlineQueryResultArticle.html
35            let ddg_search = InlineQueryResultArticle::new(
36                "02".to_string(),
37                "DuckDuckGo Search".to_string(),
38                InputMessageContent::Text(InputMessageContentText::new(format!(
39                    "https://duckduckgo.com/?q={}",
40                    q.query
41                ))),
42            )
43            .description("DuckDuckGo Search")
44            .thumbnail_url("https://duckduckgo.com/assets/logo_header.v108.png".parse().unwrap())
45            .url("https://duckduckgo.com/about".parse().unwrap()); // Note: This is the url that will open if they click the thumbnail
46
47            let results = vec![
48                InlineQueryResult::Article(google_search),
49                InlineQueryResult::Article(ddg_search),
50            ];
51
52            // Send it off! One thing to note -- the ID we use here must be of the query
53            // we're responding to.
54            let response = bot.answer_inline_query(q.id.clone(), results).send().await;
55            if let Err(err) = response {
56                log::error!("Error in handler: {err:?}");
57            }
58            respond(())
59        },
60    ));
61
62    Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
63}