aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-20 14:45:17 -0800
committerclarkzjw <[email protected]>2023-02-20 14:45:17 -0800
commitfda33866814e95830466787ae9cea2e006386e85 (patch)
tree4ba189c8e44fa32bd6c89960a9e28d171ffb89f3 /bot.py
parent88ad58c3d3412162ffdeb96f49b789e7976cad07 (diff)
downloadswarm2fediverse-fda33866814e95830466787ae9cea2e006386e85.tar.gz
attach top popular photo to mastodon media post
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/bot.py b/bot.py
index 7679d7f..467c37e 100644
--- a/bot.py
+++ b/bot.py
@@ -26,9 +26,10 @@ from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
26from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters 26from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters
27 27
28from config import BOT_TOKEN 28from config import BOT_TOKEN
29from foursquare.query_poi import query_poi 29from foursquare.poi import query_poi, get_poi_top_photo
30from dbstore.dbm_store import get_loc 30from dbstore.dbm_store import get_loc
31from toot import mastodon_client 31from toot import mastodon_client
32import urllib.request
32 33
33# Enable logging 34# Enable logging
34logging.basicConfig( 35logging.basicConfig(
@@ -66,12 +67,20 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
66 67
67 await query.answer() 68 await query.answer()
68 poi = get_loc(query.data) 69 poi = get_loc(query.data)
70 photo_url = get_poi_top_photo(query.data)
71 media_id = None
72 if photo_url is not None:
73 with urllib.request.urlopen(photo_url) as response:
74 data = response.read()
75 media = mastodon_client.media_post(data, mime_type="image/jpeg")
76 media_id = [media["id"]]
69 77
70 status = mastodon_client.status_post( 78 status = mastodon_client.status_post(
71 f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}", 79 f"I'm at {poi['name']} in {poi['locality']}, {poi['region']}, {poi['osm_url']}",
72 visibility="private") 80 visibility="private",
81 media_ids=media_id)
73 82
74 await query.edit_message_text(text=f"Selected place: {poi['name']}\nPosted to Mastodon: {status['url']}", 83 await query.edit_message_text(text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}",
75 parse_mode=telegram.constants.ParseMode.MARKDOWN) 84 parse_mode=telegram.constants.ParseMode.MARKDOWN)
76 85
77 86
Powered by cgit v1.2.3 (git 2.41.0)