aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py41
1 files changed, 23 insertions, 18 deletions
diff --git a/bot.py b/bot.py
index a059c9c..35884ff 100644
--- a/bot.py
+++ b/bot.py
@@ -5,32 +5,24 @@ import logging
5from dataclasses import dataclass 5from dataclasses import dataclass
6from http import HTTPStatus 6from http import HTTPStatus
7from config import BOT_TOKEN, TELEGRAM_WEBHOOK_URL, HEALTHCHECK_URL, FEDI_LOGIN_CALLBACK_URL, BOT_DOMAIN, BOT_PORT 7from config import BOT_TOKEN, TELEGRAM_WEBHOOK_URL, HEALTHCHECK_URL, FEDI_LOGIN_CALLBACK_URL, BOT_DOMAIN, BOT_PORT
8
9import uvicorn 8import uvicorn
10from starlette.applications import Starlette 9from starlette.applications import Starlette
11from starlette.requests import Request 10from starlette.requests import Request
12from starlette.responses import PlainTextResponse, Response 11from starlette.responses import PlainTextResponse, Response
13from starlette.routing import Route 12from starlette.routing import Route
14
15
16from telegram import Update 13from telegram import Update
17from telegram.ext import ( 14from telegram.ext import (
18 Application, 15 Application,
19 CallbackContext, 16 CallbackContext,
20 ContextTypes, 17 ContextTypes,
21 ExtBot, 18 ExtBot,
22 TypeHandler,
23)
24
25from telegram.ext import (
26 Application,
27 CallbackQueryHandler, 19 CallbackQueryHandler,
28 CommandHandler, 20 CommandHandler,
29 MessageHandler, 21 MessageHandler,
30 filters, 22 filters,
31 ConversationHandler 23 ConversationHandler,
24 TypeHandler,
32) 25)
33
34from callback import ( 26from callback import (
35 callback_generate_fedi_login_url, 27 callback_generate_fedi_login_url,
36 callback_skip_media, 28 callback_skip_media,
@@ -52,18 +44,20 @@ from command import (
52from config import ( 44from config import (
53 FEDI_LOGIN, 45 FEDI_LOGIN,
54 WAIT_LOCATION, 46 WAIT_LOCATION,
47 PROMPT_FEDI_LOGIN,
55 LOCATION_SEARCH_KEYWORD, 48 LOCATION_SEARCH_KEYWORD,
56 LOCATION_CONFIRMATION, 49 LOCATION_CONFIRMATION,
57 ADD_MEDIA, 50 ADD_MEDIA,
58 ADD_COMMENT, 51 ADD_COMMENT,
59 BOT_TOKEN, 52 BOT_TOKEN,
60 BOT_SCOPE 53 BOT_SCOPE,
54 MAIN_MENU
61) 55)
62from mastodon import Mastodon
63 56
64from dbstore.peewee_store import db, User 57from prompt.string import PROMPT_CHOOSE_ACTION
58from mastodon import Mastodon
59from dbstore.peewee_store import db, User, get_user_by_state
65 60
66# Enable logging
67logging.basicConfig( 61logging.basicConfig(
68 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO 62 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
69) 63)
@@ -74,10 +68,19 @@ logger = logging.getLogger(__name__)
74class FediLoginCallbackUpdate: 68class FediLoginCallbackUpdate:
75 code: str 69 code: str
76 state: str 70 state: str
71 user_id: str
77 72
78 73
79class FediLoginCallbackContext(CallbackContext[ExtBot, dict, dict, dict]): 74class FediLoginCallbackContext(CallbackContext[ExtBot, dict, dict, dict]):
80 pass 75 """
76 Custom CallbackContext class that makes `user_data` available for updates of type
77 `WebhookUpdate`.
78 """
79 @classmethod
80 def from_update(cls, update: object, application: "Application") -> "FediLoginCallbackContext":
81 if isinstance(update, FediLoginCallbackUpdate):
82 return cls(application=application, user_id=int(update.user_id))
83 return super().from_update(update, application)
81 84
82 85
83async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context: FediLoginCallbackContext) -> None: 86async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context: FediLoginCallbackContext) -> None:
@@ -101,13 +104,13 @@ async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context:
101 user.save() 104 user.save()
102 105
103 text = "You have successfully logged in to your Mastodon account!" 106 text = "You have successfully logged in to your Mastodon account!"
107 await context.bot.delete_message(chat_id=user.telegram_user_id, message_id=context.user_data[PROMPT_FEDI_LOGIN])
104 await context.bot.send_message(chat_id=user.telegram_user_id, text=text) 108 await context.bot.send_message(chat_id=user.telegram_user_id, text=text)
109 await context.bot.send_message(chat_id=user.telegram_user_id, text=PROMPT_CHOOSE_ACTION, reply_markup=MAIN_MENU)
105 110
106 111
107async def main() -> None: 112async def main() -> None:
108 context_types = ContextTypes(context=FediLoginCallbackContext) 113 context_types = ContextTypes(context=FediLoginCallbackContext)
109 # Here we set updater to None because we want our custom webhook server to handle the updates
110 # and hence we don't need an Updater instance
111 application = ( 114 application = (
112 Application.builder().updater(None).token(BOT_TOKEN).context_types(context_types).build() 115 Application.builder().updater(None).token(BOT_TOKEN).context_types(context_types).build()
113 ) 116 )
@@ -169,13 +172,15 @@ async def main() -> None:
169 try: 172 try:
170 code = request.query_params["code"] 173 code = request.query_params["code"]
171 state = request.query_params.get("state") 174 state = request.query_params.get("state")
175 user = get_user_by_state(state)
172 except KeyError: 176 except KeyError:
173 return PlainTextResponse( 177 return PlainTextResponse(
174 status_code=HTTPStatus.BAD_REQUEST, 178 status_code=HTTPStatus.BAD_REQUEST,
175 content="Mastodon callback request doesn't contain a valid OAuth code", 179 content="Mastodon callback request doesn't contain a valid OAuth code",
176 ) 180 )
177 181
178 await application.update_queue.put(FediLoginCallbackUpdate(state=state, code=code)) 182 await application.update_queue.put(FediLoginCallbackUpdate(state=state, code=code,
183 user_id=user["telegram_user_id"]))
179 return PlainTextResponse("Thank you for login! Now you can close the browser") 184 return PlainTextResponse("Thank you for login! Now you can close the browser")
180 185
181 async def healthcheck(_: Request) -> PlainTextResponse: 186 async def healthcheck(_: Request) -> PlainTextResponse:
Powered by cgit v1.2.3 (git 2.41.0)