teloxide/examples/inline_bot/src/main.rs

84 lines
3.8 KiB
Rust
Raw Normal View History

use teloxide::{
payloads::AnswerInlineQuery,
prelude::*,
types::{
InlineQueryResult, InlineQueryResultArticle, InputMessageContent, InputMessageContentText,
},
Bot,
};
2021-08-19 06:54:32 +02:00
use tokio_stream::wrappers::UnboundedReceiverStream;
#[tokio::main]
async fn main() {
run().await;
}
async fn run() {
let bot = Bot::from_env().auto_send();
2021-08-19 20:00:58 +02:00
// Create a new dispatcher to handle incoming queries
Dispatcher::new(bot)
.inline_queries_handler(|rx: DispatcherHandlerRx<AutoSend<Bot>, InlineQuery>| {
2021-08-19 20:00:58 +02:00
UnboundedReceiverStream::new(rx).for_each_concurrent(None, |query| async move {
// First, create your actual response
let google_search = InlineQueryResultArticle::new(
// Each item needs a unique ID, as well as the response container for the items.
// These can be whatever, as long as they don't conflict.
"01".to_string(),
// What the user will actually see
"Google Search",
2021-08-19 20:00:58 +02:00
// What message will be sent when clicked/tapped
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://www.google.com/search?q={}",
2021-08-19 20:00:58 +02:00
query.update.query,
))),
);
// You can also construct them from the struct itself, if you want a little more control.
// Please refer to the documentation for more detailed information about each field.
// https://docs.rs/teloxide/0.5.1/teloxide/types/struct.InlineQueryResultArticle.html
let ddg_search = InlineQueryResultArticle {
id: "02".to_string(), // again, anything -- as long as it's unique in this context
title: "DuckDuckGo Search".to_string(),
input_message_content: InputMessageContent::Text(InputMessageContentText::new(
2021-08-19 20:00:58 +02:00
format!("https://duckduckgo.com/?q={}", query.update.query.to_string()),
)),
reply_markup: None,
url: Some("https://duckduckgo.com/about".to_string()), // Note: This is the url that will open if they click the thumbnail
hide_url: None,
description: Some("DuckDuckGo Search".to_string()),
thumb_url: Some(
"https://duckduckgo.com/assets/logo_header.v108.png".to_string(),
),
thumb_width: Some(64),
thumb_height: Some(64),
};
2021-08-19 06:54:32 +02:00
// Now put those responses into a "Result"
// https://docs.rs/teloxide/0.5.1/teloxide/payloads/struct.AnswerInlineQuery.html
let all_results = AnswerInlineQuery {
inline_query_id: "03".to_string(), // again, anything -- as long as it's unique in this context
results: vec![
InlineQueryResult::Article(google_search),
InlineQueryResult::Article(ddg_search),
],
cache_time: None,
is_personal: None,
next_offset: None,
switch_pm_text: None,
switch_pm_parameter: None,
};
2021-08-19 06:54:32 +02:00
2021-08-19 20:00:58 +02:00
// Send it off! One thing to note -- the ID we use here must be of the query we're responding to.
let response = query
.requester
2021-08-19 20:00:58 +02:00
.answer_inline_query(query.update.id.to_string(), all_results.results)
.send()
.await;
if response.is_err() {
dbg!(response);
}
})
2021-08-19 06:54:32 +02:00
})
.dispatch()
.await;
2021-08-19 06:54:32 +02:00
}