diff options
Diffstat (limited to 'callback.py')
-rw-r--r-- | callback.py | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/callback.py b/callback.py index c7cb60c..4fe0540 100644 --- a/callback.py +++ b/callback.py | |||
@@ -7,13 +7,23 @@ from telegram.error import BadRequest | |||
7 | from telegram.ext import CallbackContext | 7 | from telegram.ext import CallbackContext |
8 | 8 | ||
9 | from command import * | 9 | from command import * |
10 | from dbstore.dbm_store import get_loc | 10 | from dbstore.peewee_store import get_poi_by_fsq_id |
11 | from foursquare.poi import OSM_ENDPOINT | 11 | from foursquare.poi import OSM_ENDPOINT |
12 | from foursquare.poi import query_poi | 12 | from foursquare.poi import query_poi |
13 | # from toot import mastodon_client | ||
14 | from config import BOT_SCOPE | 13 | from config import BOT_SCOPE |
14 | from dbstore.peewee_store import User, db | ||
15 | import uuid | ||
15 | 16 | ||
16 | mastodon_client = None | 17 | |
18 | def generate_uuid(): | ||
19 | return str(uuid.uuid4()) | ||
20 | |||
21 | |||
22 | def get_mastodon_client(user_id: int): | ||
23 | with db.connection_context(): | ||
24 | user = User.get(User.telegram_user_id == user_id) | ||
25 | if user.home_instance and user.access_key: | ||
26 | return Mastodon(access_token=user.access_key, api_base_url=user.home_instance) | ||
17 | 27 | ||
18 | 28 | ||
19 | def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon): | 29 | def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon): |
@@ -41,6 +51,7 @@ async def get_img_file_bytes(telegram_media_file): | |||
41 | async def process_media_group(context: CallbackContext): | 51 | async def process_media_group(context: CallbackContext): |
42 | context.job.data = cast(List[MsgDict], context.job.data) | 52 | context.job.data = cast(List[MsgDict], context.job.data) |
43 | 53 | ||
54 | mastodon_client = get_mastodon_client(context.user_data["user_id"]) | ||
44 | media_id = [] | 55 | media_id = [] |
45 | chat_id = context.job.data[0].get("chat_id") | 56 | chat_id = context.job.data[0].get("chat_id") |
46 | for media_dict in context.job.data: | 57 | for media_dict in context.job.data: |
@@ -61,9 +72,6 @@ async def process_media_group(context: CallbackContext): | |||
61 | 72 | ||
62 | 73 | ||
63 | async def callback_generate_fedi_login_url(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 74 | async def callback_generate_fedi_login_url(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
64 | # generate fedi OAuth login url | ||
65 | global mastodon_client | ||
66 | |||
67 | home_instance = update.effective_message.text | 75 | home_instance = update.effective_message.text |
68 | client_id, client_secret = Mastodon.create_app( | 76 | client_id, client_secret = Mastodon.create_app( |
69 | "Checkin.bot", | 77 | "Checkin.bot", |
@@ -71,13 +79,25 @@ async def callback_generate_fedi_login_url(update: Update, context: ContextTypes | |||
71 | redirect_uris="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL), | 79 | redirect_uris="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL), |
72 | api_base_url=home_instance, | 80 | api_base_url=home_instance, |
73 | ) | 81 | ) |
82 | print("client_id: {}".format(client_id)) | ||
83 | print("client_secret: {}".format(client_secret)) | ||
84 | |||
74 | m = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=home_instance) | 85 | m = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=home_instance) |
75 | 86 | ||
76 | # TODO | 87 | user_id = update.effective_user.id |
77 | # generate random string as OAuth state | 88 | state = generate_uuid() |
89 | |||
90 | db.connect() | ||
91 | u = User.get_or_none(telegram_user_id=user_id) | ||
92 | if u is None: | ||
93 | u = User.create(telegram_user_id=user_id, access_key="", home_instance=home_instance, | ||
94 | client_id=client_id, client_secret=client_secret, state=state) | ||
95 | u.save() | ||
96 | db.close() | ||
97 | |||
78 | oauth_url = m.auth_request_url(redirect_uris="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL), | 98 | oauth_url = m.auth_request_url(redirect_uris="{}{}".format(BOT_DOMAIN, FEDI_LOGIN_CALLBACK_URL), |
79 | scopes=BOT_SCOPE, | 99 | scopes=BOT_SCOPE, |
80 | state=update.effective_user.id) | 100 | state=state) |
81 | 101 | ||
82 | await update.message.reply_text(PROMPT_FEDI_LOGIN, | 102 | await update.message.reply_text(PROMPT_FEDI_LOGIN, |
83 | reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Login", url=oauth_url)]]), | 103 | reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Login", url=oauth_url)]]), |
@@ -93,9 +113,9 @@ async def callback_location_sharing(update: Update, context: ContextTypes.DEFAUL | |||
93 | context.user_data["latitude"] = update.message.venue.location.latitude | 113 | context.user_data["latitude"] = update.message.venue.location.latitude |
94 | context.user_data["longitude"] = update.message.venue.location.longitude | 114 | context.user_data["longitude"] = update.message.venue.location.longitude |
95 | 115 | ||
96 | poi = get_loc(context.user_data.get("fsq_id")) | 116 | poi = get_poi_by_fsq_id(context.user_data.get("fsq_id")) |
97 | content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) | 117 | content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) |
98 | status = mastodon_client.status_post(content, visibility=DEFAULT_TOOT_VISIBILITY, media_ids=[]) | 118 | status = get_mastodon_client(update.effective_user.id).status_post(content, visibility=DEFAULT_TOOT_VISIBILITY, media_ids=[]) |
99 | 119 | ||
100 | context.user_data[KEY_TOOT_STATUS_ID] = status["id"] | 120 | context.user_data[KEY_TOOT_STATUS_ID] = status["id"] |
101 | context.user_data[KEY_TOOT_STATUS_CONTENT] = content | 121 | context.user_data[KEY_TOOT_STATUS_CONTENT] = content |
@@ -165,12 +185,12 @@ async def callback_skip_location_keyword_search(update: Update, context: Context | |||
165 | async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int: | 185 | async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int: |
166 | poi_name = context.user_data.get("poi_name") | 186 | poi_name = context.user_data.get("poi_name") |
167 | if context.user_data.get("fsq_id") is not None: | 187 | if context.user_data.get("fsq_id") is not None: |
168 | poi = get_loc(context.user_data.get("fsq_id")) | 188 | poi = get_poi_by_fsq_id(context.user_data.get("fsq_id")) |
169 | content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) | 189 | content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) |
170 | else: | 190 | else: |
171 | content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), context.user_data.get("longitude")) | 191 | content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), context.user_data.get("longitude")) |
172 | 192 | ||
173 | status = mastodon_client.status_post(content, visibility=DEFAULT_TOOT_VISIBILITY, media_ids=[]) | 193 | status = get_mastodon_client(context.user_data["user_id"]).status_post(content, visibility=DEFAULT_TOOT_VISIBILITY, media_ids=[]) |
174 | 194 | ||
175 | context.user_data[KEY_TOOT_STATUS_ID] = status["id"] | 195 | context.user_data[KEY_TOOT_STATUS_ID] = status["id"] |
176 | context.user_data[KEY_TOOT_STATUS_CONTENT] = content | 196 | context.user_data[KEY_TOOT_STATUS_CONTENT] = content |
@@ -192,6 +212,7 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D | |||
192 | query = update.callback_query | 212 | query = update.callback_query |
193 | await query.answer() | 213 | await query.answer() |
194 | context.user_data["fsq_id"] = query.data | 214 | context.user_data["fsq_id"] = query.data |
215 | context.user_data["user_id"] = update.effective_user.id | ||
195 | await query.delete_message() | 216 | await query.delete_message() |
196 | 217 | ||
197 | context.user_data["chat_id"] = update.effective_chat.id | 218 | context.user_data["chat_id"] = update.effective_chat.id |
@@ -202,6 +223,7 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D | |||
202 | async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 223 | async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
203 | context.user_data["poi_name"] = update.effective_message.text | 224 | context.user_data["poi_name"] = update.effective_message.text |
204 | context.user_data["chat_id"] = update.effective_chat.id | 225 | context.user_data["chat_id"] = update.effective_chat.id |
226 | context.user_data["user_id"] = update.effective_user.id | ||
205 | 227 | ||
206 | return await _process_location_selection(context) | 228 | return await _process_location_selection(context) |
207 | 229 | ||
@@ -219,7 +241,7 @@ async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYP | |||
219 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) | 241 | await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) |
220 | 242 | ||
221 | comment = update.effective_message.text | 243 | comment = update.effective_message.text |
222 | mastodon_client.status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), | 244 | get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), |
223 | status=f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT)) | 245 | status=f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT)) |
224 | context.user_data[KEY_TOOT_STATUS_CONTENT] = f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT) | 246 | context.user_data[KEY_TOOT_STATUS_CONTENT] = f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT) |
225 | 247 | ||
@@ -243,6 +265,7 @@ async def callback_add_media(update: Update, context: CallbackContext): | |||
243 | if "not found" in str(e.message): | 265 | if "not found" in str(e.message): |
244 | pass | 266 | pass |
245 | 267 | ||
268 | mastodon_client = get_mastodon_client(update.effective_user.id) | ||
246 | status_id = context.user_data.get(KEY_TOOT_STATUS_ID) | 269 | status_id = context.user_data.get(KEY_TOOT_STATUS_ID) |
247 | status_content = context.user_data.get(KEY_TOOT_STATUS_CONTENT) | 270 | status_content = context.user_data.get(KEY_TOOT_STATUS_CONTENT) |
248 | 271 | ||