lib.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use poem::http::StatusCode;
  2. use telegram_bot::TelegramBot;
  3. use poem::web::{Data,IntoResponse, Json};
  4. use poem::{handler, Response};
  5. use redis;
  6. use telegram_bot::telegram_types::TelegramUpdate;
  7. use sea_orm::DatabaseConnection;
  8. use google_sheets4 as sheets4;
  9. use sheets4::{Sheets, hyper_rustls, hyper_util};
  10. #[derive(Clone)]
  11. pub struct AppState {
  12. pub conn: DatabaseConnection,
  13. pub templates: tera::Tera,
  14. pub redis_client: redis::Client,
  15. pub tg_bot: TelegramBot,
  16. pub gs_hub: Sheets<hyper_rustls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>>,
  17. pub sheet_id: String,
  18. }
  19. #[handler]
  20. pub async fn tg_bot_handler(state: Data<&AppState>, req: Json<TelegramUpdate>) -> impl IntoResponse {
  21. let tg_bot = &state.tg_bot;
  22. if tg_bot.check_if_handled(req.update_id).unwrap() {
  23. return Response::builder()
  24. .status(StatusCode::ALREADY_REPORTED)
  25. .body(());
  26. }
  27. msg_handler::handle_message(
  28. &req.message, &tg_bot, &state.conn, &state.gs_hub, &state.sheet_id
  29. ).await;
  30. match tg_bot.mark_as_handled(req.update_id) {
  31. Ok(_) => {
  32. return Response::builder()
  33. .status(StatusCode::OK)
  34. .body(());
  35. },
  36. Err(_) => return Response::builder()
  37. .status(StatusCode::INTERNAL_SERVER_ERROR)
  38. .body(())
  39. }
  40. }