#[macro_use]
extern crate log;
use std::env;
use mobot::{api::SendMessageRequest, *};
use mobot_derive::BotState;
#[derive(Clone, Default, BotState)]
struct ChatState {
counter: usize,
}
async fn handle_chat_message(e: Event, state: State<ChatState>) -> Result<Action, anyhow::Error> {
let mut state = state.get().write().await;
let chat_id = e.update.chat_id()?;
state.counter += 1;
e.api
.send_message(
&SendMessageRequest::new(chat_id, format!("Pong {}", state.counter))
.with_reply_markup(api::ReplyMarkup::reply_keyboard_remove()),
)
.await?;
e.api
.send_message(
&SendMessageRequest::new(chat_id, "Try again?").with_reply_markup(
api::ReplyMarkup::inline_keyboard_markup(vec![
vec![
api::InlineKeyboardButton::from("Again!").with_callback_data("again"),
api::InlineKeyboardButton::from("Stop!").with_callback_data("stop"),
],
vec![
api::InlineKeyboardButton::from("Boo!").with_callback_data("boo"),
api::InlineKeyboardButton::from("Blah!").with_callback_data("blah"),
],
]),
),
)
.await?;
Ok(Action::Done)
}
async fn handle_chat_callback(e: Event, _: State<ChatState>) -> Result<Action, anyhow::Error> {
let response = format!("Okay: {}", e.update.data().unwrap_or("no callback data"));
e.acknowledge_callback(Some(response)).await?;
e.remove_inline_keyboard().await?;
Ok(Action::Done)
}
#[tokio::main]
async fn main() {
mobot::init_logger();
info!("Starting pingbot...");
let client = Client::new(env::var("TELEGRAM_TOKEN").unwrap());
Router::new(client)
.add_route(Route::Default, handlers::log_handler)
.add_route(Route::Message(Matcher::Any), handle_chat_message)
.add_route(Route::CallbackQuery(Matcher::Any), handle_chat_callback)
.start()
.await;
}