diff --git a/reddit.py b/reddit.py index a4acff8..52dd2a6 100644 --- a/reddit.py +++ b/reddit.py @@ -13,9 +13,9 @@ def get_post_type(url): def get_reddit_instance(): reddit_config = {} - with open("reddit_config") as tmp_file: + with open("config/reddit_config") as tmp_file: for line in tmp_file: - (key, val) = line.split(';') + (key, val) = line.split('=') reddit_config[key] = val.replace('\n', '') return praw.Reddit( @@ -50,7 +50,7 @@ def get_reddit_post(sub, is_image_post=False): if tries >= 5: return "Geile, gib Sub mit Bildern (über 5 Fehlversuche)" - return post.url + return post def wrong_dog(bot, update): @@ -82,3 +82,4 @@ def reddit_img(bot, update, args): return except AttributeError: update.message.reply_text(post) + print("ERROR") diff --git a/schneiderbot.py b/schneiderbot.py index 0da954f..6b15b1e 100755 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -11,6 +11,7 @@ from telegram.ext import Updater, CommandHandler from coding_love import send_coding_love_gif from reddit import wrong_dog, hamster, reddit, reddit_img +from schneiderbot_twitter import toggle_pepito_for_group, check_pepito_and_post_auto, check_pepito_and_post_manually from db import Db from mensa import mensa @@ -206,19 +207,25 @@ def init_commands(dp): dp.add_handler(CommandHandler("hamster", hamster)) dp.add_handler(CommandHandler("reddit", reddit, pass_args=True)) dp.add_handler(CommandHandler("reddit_img", reddit_img, pass_args=True)) + dp.add_handler(CommandHandler("pepito", check_pepito_and_post_manually)) + dp.add_handler(CommandHandler("tpepito", toggle_pepito_for_group)) def main(): """Start the bot.""" # Create the EventHandler and pass it your bot's token. - token = open("token").read() - # token = open("test_token").read() + token = open("config/token").read() + # token = open("config/test_token").read() updater = Updater(token.strip()) + jq = updater.job_queue + # Get the dispatcher to register handlers dp = updater.dispatcher init_commands(dp) + job_minute = jq.run_repeating(check_pepito_and_post_auto, interval=180, first=0) + # on different commands - answer in Telegram # log all errors diff --git a/schneiderbot_twitter.py b/schneiderbot_twitter.py new file mode 100644 index 0000000..fd235be --- /dev/null +++ b/schneiderbot_twitter.py @@ -0,0 +1,52 @@ +import twitter + +pepito_id = 333923305 +last_status = None +pepito_groups = [] + + +def get_twitter_instance(): + twitter_config = {} + with open("config/twitter_config") as tmp_file: + for line in tmp_file: + (key, val) = line.split('=') + twitter_config[key] = val.replace('\n', '') + + return twitter.Api(consumer_key=twitter_config['consumer_key'], + consumer_secret=twitter_config['consumer_secret'], + access_token_key=twitter_config['access_token_key'], + access_token_secret=twitter_config['access_token_secret']) + + +def toggle_pepito_for_group(bot, update): + global pepito_groups + chat_id = update.message.chat_id + + if chat_id in pepito_groups: + pepito_groups.remove(chat_id) + update.message.reply_text("Pepito is now disabled!") + else: + pepito_groups.append(update.message.chat_id) + update.message.reply_text("Pepito is now enabled!") + + +def check_pepito_and_post_auto(bot, update): + api = get_twitter_instance() + current_status = api.GetUserTimeline(pepito_id)[0] + global last_status + if last_status != current_status: + for group_id in pepito_groups: + bot.send_message(chat_id=group_id, text=current_status.AsDict()['text']) + last_status = current_status + + +def check_pepito_and_post_manually(bot, update): + api = get_twitter_instance() + current_status = api.GetUserTimeline(pepito_id)[0] + global last_status + if last_status != current_status: + for group_id in pepito_groups: + bot.send_message(chat_id=group_id, text=current_status.AsDict()['text']) + last_status = current_status + else: + update.message.reply_text(current_status.AsDict()['text'])