aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py51
1 files changed, 24 insertions, 27 deletions
diff --git a/bot.py b/bot.py
index 79fba5d..a059c9c 100644
--- a/bot.py
+++ b/bot.py
@@ -61,6 +61,7 @@ from config import (
61) 61)
62from mastodon import Mastodon 62from mastodon import Mastodon
63 63
64from dbstore.peewee_store import db, User
64 65
65# Enable logging 66# Enable logging
66logging.basicConfig( 67logging.basicConfig(
@@ -72,39 +73,35 @@ logger = logging.getLogger(__name__)
72@dataclass 73@dataclass
73class FediLoginCallbackUpdate: 74class FediLoginCallbackUpdate:
74 code: str 75 code: str
75 state: int 76 state: str
76 77
77 78
78class FediLoginCallbackContext(CallbackContext[ExtBot, dict, dict, dict]): 79class FediLoginCallbackContext(CallbackContext[ExtBot, dict, dict, dict]):
79 @classmethod 80 pass
80 def from_update(
81 cls,
82 update: object,
83 application: "Application",
84 ) -> "FediLoginCallbackContext":
85 if isinstance(update, FediLoginCallbackUpdate):
86 return cls(application=application, user_id=update.state)
87 return super().from_update(update, application)
88 81
89 82
90async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context: FediLoginCallbackContext) -> None: 83async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context: FediLoginCallbackContext) -> None:
91 # query client_id and client_secret from in memory database 84 state = update.state
92 client_id = "" 85
93 client_secret = "" 86 with db.connection_context():
94 home_instance = "" 87 user = User.get(User.state == state)
95
96 mastodon_client = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=home_instance)
97 access_token = mastodon_client.log_in(
98 code=update.code,
99 redirect_uri="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL),
100 scopes=BOT_SCOPE
101 )
102 88
103 # TODO 89 client_id = user.client_id
104 # save access_token to database 90 client_secret = user.client_secret
91 home_instance = user.home_instance
92
93 if len(user.access_key) == 0:
94 mastodon_client = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=home_instance)
95 access_token = mastodon_client.log_in(
96 code=update.code,
97 redirect_uri="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL),
98 scopes=BOT_SCOPE
99 )
100 user.access_key = access_token
101 user.save()
105 102
106 text = "You have successfully logged in to your Mastodon account!" 103 text = "You have successfully logged in to your Mastodon account!"
107 await context.bot.send_message(chat_id=update.state, text=text) 104 await context.bot.send_message(chat_id=user.telegram_user_id, text=text)
108 105
109 106
110async def main() -> None: 107async def main() -> None:
@@ -112,7 +109,7 @@ async def main() -> None:
112 # Here we set updater to None because we want our custom webhook server to handle the updates 109 # Here we set updater to None because we want our custom webhook server to handle the updates
113 # and hence we don't need an Updater instance 110 # and hence we don't need an Updater instance
114 application = ( 111 application = (
115 Application.builder().token(BOT_TOKEN).context_types(context_types).build() 112 Application.builder().updater(None).token(BOT_TOKEN).context_types(context_types).build()
116 ) 113 )
117 114
118 checkin_handler = ConversationHandler( 115 checkin_handler = ConversationHandler(
@@ -171,7 +168,7 @@ async def main() -> None:
171 """ 168 """
172 try: 169 try:
173 code = request.query_params["code"] 170 code = request.query_params["code"]
174 state = int(request.query_params.get("state")) 171 state = request.query_params.get("state")
175 except KeyError: 172 except KeyError:
176 return PlainTextResponse( 173 return PlainTextResponse(
177 status_code=HTTPStatus.BAD_REQUEST, 174 status_code=HTTPStatus.BAD_REQUEST,
Powered by cgit v1.2.3 (git 2.41.0)