aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'callback.py')
-rw-r--r--callback.py47
1 files changed, 35 insertions, 12 deletions
diff --git a/callback.py b/callback.py
index a1b4b2d..ecce2bf 100644
--- a/callback.py
+++ b/callback.py
@@ -7,8 +7,7 @@ from telegram.ext import CallbackContext
7 7
8from command import * 8from command import *
9from dbstore.peewee_store import get_poi_by_fsq_id 9from dbstore.peewee_store import get_poi_by_fsq_id
10from foursquare.poi import OSM_ENDPOINT 10from foursquare.poi import query_poi, query_poi_by_fsq_id, OSM_ENDPOINT
11from foursquare.poi import query_poi
12from config import BOT_SCOPE, ENCRYPT_KEY 11from config import BOT_SCOPE, ENCRYPT_KEY
13from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_PUBLIC, TOOT_VISIBILITY_UNLISTED 12from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_PUBLIC, TOOT_VISIBILITY_UNLISTED
14import uuid 13import uuid
@@ -24,7 +23,10 @@ def get_mastodon_client(user_id: int):
24 with db.connection_context(): 23 with db.connection_context():
25 user = User.get(User.telegram_user_id == user_id) 24 user = User.get(User.telegram_user_id == user_id)
26 if user.home_instance and user.access_key: 25 if user.home_instance and user.access_key:
27 return Mastodon(access_token=decrypt(user.access_key, ENCRYPT_KEY), api_base_url=user.home_instance) 26 feature_set = "pleroma" if user.home_instance_type == "pleroma" else "mainline"
27
28 return Mastodon(access_token=decrypt(user.access_key, ENCRYPT_KEY),
29 api_base_url=user.home_instance, feature_set=feature_set, version_check_mode='none', )
28 30
29 31
30def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon): 32def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon):
@@ -94,11 +96,12 @@ async def callback_generate_fedi_login_url(update: Update, context: ContextTypes
94 u.save() 96 u.save()
95 97
96 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),
97 scopes=BOT_SCOPE, 99 scopes=BOT_SCOPE,
98 state=state) 100 state=state)
99 101
100 msg = await update.message.reply_text(PROMPT_FEDI_LOGIN, 102 msg = await update.message.reply_text(PROMPT_FEDI_LOGIN,
101 reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Login", url=oauth_url)]]), 103 reply_markup=InlineKeyboardMarkup(
104 [[InlineKeyboardButton("Login", url=oauth_url)]]),
102 parse_mode=ParseMode.MARKDOWN) 105 parse_mode=ParseMode.MARKDOWN)
103 106
104 context.user_data[PROMPT_FEDI_LOGIN] = msg.message_id 107 context.user_data[PROMPT_FEDI_LOGIN] = msg.message_id
@@ -112,9 +115,18 @@ async def callback_location_sharing(update: Update, context: ContextTypes.DEFAUL
112 context.user_data["latitude"] = update.message.venue.location.latitude 115 context.user_data["latitude"] = update.message.venue.location.latitude
113 context.user_data["longitude"] = update.message.venue.location.longitude 116 context.user_data["longitude"] = update.message.venue.location.longitude
114 117
115 poi = get_poi_by_fsq_id(context.user_data.get("fsq_id")) 118 poi = query_poi_by_fsq_id(context.user_data.get("fsq_id"))
116 content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) 119 content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"])
117 status = get_mastodon_client(update.effective_user.id).status_post(content, visibility=TOOT_VISIBILITY_PRIVATE, media_ids=[]) 120
121 # TODO
122 # mastodon.py status_update() currently does not support content_type parameter
123 with db.connection_context():
124 u = User.get(User.telegram_user_id == update.effective_user.id)
125 content_type = "text/markdown" if u.home_instance_type == "pleroma" else "text/plain"
126 status = get_mastodon_client(update.effective_user.id).status_post(content,
127 visibility=TOOT_VISIBILITY_PRIVATE,
128 content_type=content_type,
129 media_ids=[])
118 130
119 context.user_data[KEY_TOOT_STATUS_ID] = status["id"] 131 context.user_data[KEY_TOOT_STATUS_ID] = status["id"]
120 context.user_data[KEY_TOOT_STATUS_CONTENT] = content 132 context.user_data[KEY_TOOT_STATUS_CONTENT] = content
@@ -158,7 +170,8 @@ async def callback_location_keyword_search(update: Update, context: ContextTypes
158 await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_LOCATION_KEYWORD)) 170 await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_LOCATION_KEYWORD))
159 key = update.effective_message.text 171 key = update.effective_message.text
160 172
161 keyboard = await _process_location_search(key, context.user_data.get("latitude"), context.user_data.get("longitude")) 173 keyboard = await _process_location_search(key, context.user_data.get("latitude"),
174 context.user_data.get("longitude"))
162 175
163 if len(keyboard) == 0: 176 if len(keyboard) == 0:
164 msg = await update.message.reply_text(PROMPT_WAIT_LOCATION_CONFIRMATION_NO_NEARBY_POI) 177 msg = await update.message.reply_text(PROMPT_WAIT_LOCATION_CONFIRMATION_NO_NEARBY_POI)
@@ -188,9 +201,17 @@ async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int
188 poi_name = poi["name"] 201 poi_name = poi["name"]
189 content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) 202 content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"])
190 else: 203 else:
191 content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), context.user_data.get("longitude")) 204 content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"),
205 context.user_data.get("longitude"))
192 206
193 status = get_mastodon_client(context.user_data["user_id"]).status_post(content, visibility=TOOT_VISIBILITY_PRIVATE, media_ids=[]) 207 with db.connection_context():
208 u = User.get(User.telegram_user_id == context.user_data["user_id"])
209 content_type = "text/markdown" if u.home_instance_type == "pleroma" else "text/plain"
210
211 status = get_mastodon_client(context.user_data["user_id"]).status_post(content,
212 visibility=TOOT_VISIBILITY_PRIVATE,
213 content_type=content_type,
214 media_ids=[])
194 215
195 context.user_data[KEY_TOOT_STATUS_ID] = status["id"] 216 context.user_data[KEY_TOOT_STATUS_ID] = status["id"]
196 context.user_data[KEY_TOOT_STATUS_CONTENT] = content 217 context.user_data[KEY_TOOT_STATUS_CONTENT] = content
@@ -238,11 +259,13 @@ async def _process_comment(context: ContextTypes.DEFAULT_TYPE) -> int:
238 259
239async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: 260async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
240 context.user_data["chat_id"] = update.effective_chat.id 261 context.user_data["chat_id"] = update.effective_chat.id
262 context.user_data["user_id"] = update.effective_user.id
241 await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) 263 await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT))
242 264
243 comment = update.effective_message.text 265 comment = update.effective_message.text
244 get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), 266 get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID),
245 status=f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT)) 267 status=f"{comment} " + context.user_data.get(
268 KEY_TOOT_STATUS_CONTENT))
246 context.user_data[KEY_TOOT_STATUS_CONTENT] = f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT) 269 context.user_data[KEY_TOOT_STATUS_CONTENT] = f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT)
247 270
248 return await _process_comment(context) 271 return await _process_comment(context)
Powered by cgit v1.2.3 (git 2.41.0)