Skip to content

Commit

Permalink
Minor fixes (#18)
Browse files Browse the repository at this point in the history
* Add bold and \n to post title

* Add authorization client id for imgur images

Migration to python-telegram-bot v12
  • Loading branch information
fabiosangregorio committed Feb 25, 2020
1 parent 859b938 commit 291d5ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Binary file modified requirements.txt
Binary file not shown.
9 changes: 7 additions & 2 deletions telereddit/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ def _get_media(post_url, json={}):
post_url = oembed_url
media_type = 'youtube'
else:
file_size = int(requests.get(post_url, stream=True).headers['Content-length'])
file_size = int(
requests.get(
post_url,
headers={'Authorization': 'Client-ID 90bd24c00c6efe0'},
stream=True
).headers['Content-length'])

media = namedtuple('media', 'type url size')
return media(media_type, post_url, file_size)
Expand Down Expand Up @@ -167,7 +172,7 @@ def get_post(subreddit=None, post_url=None):
if media and media.type == 'youtube':
post_text = post_text + f"\n\n[Link to youtube video]({media.url})"

full_msg = f"{post_title}{post_text}\n\n{post_footer}"
full_msg = f"*{post_title}*\n{post_text}\n\n{post_footer}"

post = namedtuple('Post', 'subreddit title text msg footer permalink '
'content_url type media_url media_size')
Expand Down
22 changes: 12 additions & 10 deletions telereddit/telereddit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
import sentry_sdk

from telegram.ext import Updater, MessageHandler, CallbackQueryHandler, Filters
from telegram.ext import Updater, CallbackContext, MessageHandler, CallbackQueryHandler, Filters
from telegram import Update
import logging

from secret import TELEGRAM_TOKEN, SENTRY_TOKEN
Expand All @@ -10,7 +11,7 @@
import helpers


def on_chat_message(bot, update):
def on_chat_message(update: Update, context: CallbackContext):
'''Handles a single update message.'''
msg = update.message
if not msg.text:
Expand All @@ -19,12 +20,12 @@ def on_chat_message(bot, update):
if any(r in msg.text.lower() for r in ['reddit.com', 'redd.it', 'reddit.app.link']):
posts_url = helpers.get_urls_from_text(msg.text)
for url in posts_url:
reddit_linker.send_post_from_url(bot, msg.chat_id, url)
reddit_linker.send_post_from_url(context.bot, msg.chat_id, url)
elif 'r/' in msg.text.lower():
reddit_linker.send_random_posts(bot, msg.chat_id, msg.text)
reddit_linker.send_random_posts(context.bot, msg.chat_id, msg.text)


def on_callback_query(bot, update):
def on_callback_query(update: Update, context: CallbackContext):
'''Handles all the several types of callback queries.'''
query_id = update.callback_query.id
query_data = update.callback_query.data
Expand All @@ -34,19 +35,20 @@ def on_callback_query(bot, update):
text = (message.caption or message.text) + '\n'

if query_data == 'more':
reddit_linker.send_random_posts(bot, chat_id, text)
reddit_linker.send_random_posts(context.bot, chat_id, text)
elif query_data == 'edit':
reddit_linker.edit_result(bot, update)
reddit_linker.edit_result(context.bot, update)
elif query_data == 'delete':
bot.deleteMessage(chat_id, message_id)
context.bot.deleteMessage(chat_id, message_id)

bot.answerCallbackQuery(query_id)
context.bot.answerCallbackQuery(query_id)


if __name__ == "__main__":
sentry_sdk.init(SENTRY_TOKEN)

updater = Updater(token=TELEGRAM_TOKEN)
updater = Updater(token=TELEGRAM_TOKEN, use_context=True)
print("Listening...")
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
Expand Down

0 comments on commit 291d5ff

Please sign in to comment.