From a50b0c08b074be6b49e45fc27ed5058f77f8f915 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 23 Mar 2017 18:14:22 +0100 Subject: [PATCH] Add distinct broadcast and emergency methods --- bottools/methods.py | 15 +++++++++++++++ bottools/strings.py | 5 +++++ dbtools/__init__.py | 15 +++++++++++++-- setup.py | 2 +- telegrambot.py | 3 ++- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/bottools/methods.py b/bottools/methods.py index 9091a6a..2d27e43 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -249,6 +249,14 @@ def mentionstream(bot, update): except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, update.message) +# Admin interaction + +def togglebroadcasts(bot, update): + try: + update.message.reply_text(bottools.strings.toggleBroadcasts % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleBroadcasts(update.message.chat_id) else bottools.strings.toggleTweetOff)) + except tweepy.error.TweepError as e: + bottools.methods.twoExceptions(e, update.message) + # Administrator def isadmin(message): @@ -266,6 +274,13 @@ def restart(bot, update): bottools.methods.unknown(bot, update) def broadcast(bot, update, args): + if bottools.methods.isadmin(update.message): + for u in dbtools.dbHelper().broadcastUsers(): + bot.sendMessage(chat_id = u, text = ' '.join(args)) + else: + bottools.methods.unknown(bot, update) + +def emergency(bot, update, args): if bottools.methods.isadmin(update.message): for u in dbtools.dbHelper().allUsers(): bot.sendMessage(chat_id = u, text = ' '.join(args)) diff --git a/bottools/strings.py b/bottools/strings.py index 8f375cc..8de3074 100644 --- a/bottools/strings.py +++ b/bottools/strings.py @@ -44,11 +44,16 @@ Additionally, you will be able to use the following commands: If you have any further questions about these commands, I'll try to help you. Just use /help COMMAND for that. For instance, "/help timeline" will show you further details about the /timeline command. +Every once in a while, I may want to tell you about new features. If you don't want to receive these messages, you can opt out using /togglebroadcasts. Please note that you will still receive urgent notices if necessary. + In case you ever wish to leave, you can use /logout. Of course, you're always welcome to login again after that. Have fun!''' +toggleBroadcasts = '''Broadcast notifications are now %s.''' + + toggleTweet = '''Automatic tweeting is now %s.''' diff --git a/dbtools/__init__.py b/dbtools/__init__.py index bd14899..e87c2a0 100644 --- a/dbtools/__init__.py +++ b/dbtools/__init__.py @@ -131,12 +131,23 @@ class dbObject: except: return False + def getBStatus(self, cid): + try: + self.executeQuery("SELECT broadcast FROM tokens WHERE cid = %i;" % int(cid)) + return True if int(self.cur.fetchone()[0]) == 1 else False + except: + raise ValueError("No such user: %i" % int(cid)) + + def toggleBroadcasts(self, cid): + self.executeQuery("UPDATE tokens SET broadcast = NOT broadcast WHERE cid = %i;" % int(cid)) + self.commit() + + return self.getBStatus(cid) + def getTStatus(self, cid): try: self.executeQuery("SELECT tweet FROM tokens WHERE cid = %i;" % int(cid)) - return True if int(self.cur.fetchone()[0]) == 1 else False - except: raise ValueError("No such user: %i" % int(cid)) diff --git a/setup.py b/setup.py index 33d02c6..6f4590e 100755 --- a/setup.py +++ b/setup.py @@ -65,7 +65,7 @@ if not db.isInitialized(): db.executeQuery("CREATE TABLE names(`id` TEXT NOT NULL, `name` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));") db.executeQuery("CREATE TABLE retweets(id INT PRIMARY KEY, author VARCHAR(30), created_at VARCHAR(30), text TEXT);") db.executeQuery("CREATE TABLE lyrics(id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(140) NOT NULL, ref INT NOT NULL default '0', tweet_id INT, active BOOLEAN default '0');") - db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0);") + db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0, `broadcast` BOOLEAN DEFAULT 1);") db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));") db.commit() diff --git a/telegrambot.py b/telegrambot.py index 427321b..1e474e7 100755 --- a/telegrambot.py +++ b/telegrambot.py @@ -24,8 +24,9 @@ if __name__ == "__main__": updater.dispatcher.add_handler(telegram.ext.CommandHandler("retweet", bottools.methods.retweet, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", bottools.methods.start)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("timeline", bottools.methods.timeline, pass_args=True)) - updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", bottools.methods.toggleTweet)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("togglebroadcasts", bottools.methods.togglebroadcasts)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("togglementions", bottools.methods.mentionstream)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("toggletweet", bottools.methods.toggleTweet)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("tweet", bottools.methods.explicitTweet, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("unauth", bottools.methods.unauth)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("unfollow", bottools.methods.unfollow, pass_args=True))