From 3537379e99de41fe21299e40e97c52607ff936c7 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Fri, 24 Mar 2017 22:24:55 +0100 Subject: [PATCH] Make outgoing tweet notifications optional --- bottools/methods.py | 11 ++++++++++- bottools/strings.py | 4 ++++ dbtools/__init__.py | 13 +++++++++++++ setup.py | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/bottools/methods.py b/bottools/methods.py index 4ff1122..31f7e28 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -82,6 +82,12 @@ def toggleTweet(bot, update): update.message.reply_text(bottools.strings.toggleTweet % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleTweet(update.message.chat_id) else bottools.strings.toggleTweetOff)) except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, update.message) + +def toggleConfirmations(bot, update): + try: + update.message.reply_text(bottools.strings.toggleConfirmations % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleConfirmations(update.message.chat_id) else bottools.strings.toggleTweetOff)) + except tweepy.error.TweepError as e: + bottools.methods.twoExceptions(e, update.message) def unknown(bot, update): update.message.reply_text(bottools.strings.unknownCommand) @@ -184,7 +190,8 @@ def explicitTweet(bot, update, args, reply = None): else: status = two.tweet(' '.join(args), reply) - bottools.methods.tweetMessage(status, update.message.chat_id, bot) + if dbtools.dbHelper().getCStatus(update.message.chat_id): + bottools.methods.tweetMessage(status, update.message.chat_id, bot) except tweepy.error.TweepError as e: bottools.methods.twoExceptions(e, update.message) @@ -484,7 +491,9 @@ commands = { "thread": thread, "timeline": timeline, "togglebroadcasts": togglebroadcasts, + "toggleconfirmations": toggleConfirmations, "togglementions": mentionstream, + "toggletweet": toggleTweet, "tweet": explicitTweet, "unauth": unauth, "unfollow": unfollow, diff --git a/bottools/strings.py b/bottools/strings.py index 3210586..03a56b6 100644 --- a/bottools/strings.py +++ b/bottools/strings.py @@ -36,6 +36,7 @@ Additionally, you will be able to use the following commands: * /quote TWEET - Quote a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. * /reply TWEET TEXT - Reply TEXT to a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. If you do not include any mentions, I will take care of this for you. * /retweet TWEET - Retweet a tweet. You will need to pass a tweet ID (TWEET) as returned by /timeline. +* /toggleconfirmations - Turn on/off notifications about tweets you posted yourself. * /togglementions - Turn on/off notifications when you are mentioned in a tweet. * /toggletweet - Turn automatic tweeting of messages on/off. Useful in groups. * /tweet TEXT - Explicitly tweet TEXT even if automatic tweeting is off (/toggletweet). @@ -55,6 +56,9 @@ Have fun!''' toggleBroadcasts = '''Broadcast notifications are now %s.''' +toggleConfirmations = '''Confirmations for outgoing tweets are now %s.''' + + toggleTweet = '''Automatic tweeting is now %s.''' diff --git a/dbtools/__init__.py b/dbtools/__init__.py index a6eb58b..d4eb71d 100644 --- a/dbtools/__init__.py +++ b/dbtools/__init__.py @@ -144,6 +144,19 @@ class dbObject: return self.getBStatus(cid) + def getCStatus(self, cid): + try: + self.executeQuery("SELECT confirmations 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 toggleConfirmations(self, cid): + self.executeQuery("UPDATE tokens SET confirmations = NOT confirmations WHERE cid = %i;" % int(cid)) + self.commit() + + return self.getCStatus(cid) + def getTStatus(self, cid): try: self.executeQuery("SELECT tweet FROM tokens WHERE cid = %i;" % int(cid)) diff --git a/setup.py b/setup.py index 6f4590e..f5913a1 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, `broadcast` BOOLEAN DEFAULT 1);") + 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, `confirmations` BOOLEAN DEFAULT 1);") db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));") db.commit()