Allow multiple accounts per chat
This commit is contained in:
parent
5c4f890bf5
commit
b2da5a1f85
|
@ -46,7 +46,7 @@ def callback(bot, update):
|
|||
except:
|
||||
status = feature(bot, update)
|
||||
except:
|
||||
update.callback_query.reply_text(bottools.strings.unknownTweet)
|
||||
update.callback_query.reply_text(bottools.strings.unknownCommand)
|
||||
return
|
||||
|
||||
if status:
|
||||
|
@ -132,38 +132,75 @@ def auth(bot, update):
|
|||
db = dbtools.dbHelper()
|
||||
cid = update.message.chat_id
|
||||
|
||||
if not (db.ato(cid) or db.ase(cid)):
|
||||
auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse())
|
||||
update.message.reply_text(bottools.strings.auth % auth.get_authorization_url())
|
||||
dbtools.dbHelper().storeToken(cid, auth.request_token)
|
||||
|
||||
else:
|
||||
update.message.reply_text(bottools.strings.authimp)
|
||||
|
||||
def verify(bot, update, args):
|
||||
db = dbtools.dbHelper()
|
||||
cid = update.message.chat_id
|
||||
|
||||
if db.ato(cid) and not db.ase(cid):
|
||||
try:
|
||||
auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse())
|
||||
auth.request_token = ast.literal_eval(db.ato(cid))
|
||||
auth.request_token = ast.literal_eval(db.getToken(cid))
|
||||
|
||||
try:
|
||||
auth.get_access_token(args[0])
|
||||
dbtools.dbHelper().storeUser(cid, auth.access_token, auth.access_token_secret)
|
||||
db.storeUser(cid, auth.access_token, auth.access_token_secret)
|
||||
update.message.reply_text(bottools.strings.verify)
|
||||
|
||||
except Exception as e:
|
||||
dbtools.dbHelper().deleteUser(update.message.chat_id)
|
||||
db.deleteToken(update.message.chat_id)
|
||||
update.message.reply_text(bottools.strings.verifyfail)
|
||||
|
||||
else:
|
||||
except:
|
||||
update.message.reply_text(bottools.strings.verifyimp)
|
||||
|
||||
def unauth(bot, update):
|
||||
dbtools.dbHelper().deleteUser(update.message.chat_id)
|
||||
update.message.reply_text(bottools.strings.unauth % setuptools.url())
|
||||
|
||||
def switch(bot, update, args):
|
||||
message = update.message or update.callback_query.message
|
||||
cid = message.chat_id
|
||||
found = None
|
||||
|
||||
for a in dbtools.dbHelper().accounts(cid):
|
||||
try:
|
||||
if twitools.twObject(ato=a[0], ase=a[1]).whoami().strip("@") == args[0].strip("@"):
|
||||
found = a
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
if found:
|
||||
dbtools.dbHelper().setActive(cid, a[0])
|
||||
message.reply_text(bottools.strings.switch % bottools.methods.getTwo(message).whoami().strip("@"))
|
||||
|
||||
else:
|
||||
message.reply_text(bottools.strings.noauth)
|
||||
|
||||
def accounts(bot, update):
|
||||
accounts = []
|
||||
|
||||
for a in dbtools.dbHelper().accounts(update.message.chat_id):
|
||||
try:
|
||||
accounts += [twitools.twObject(ato = a[0], ase = a[1]).whoami().strip("@")]
|
||||
except:
|
||||
pass
|
||||
|
||||
buttons = []
|
||||
|
||||
if len(accounts) != 0:
|
||||
for account in accounts:
|
||||
buttons += [telegram.InlineKeyboardButton("@%s" % account, callback_data = "/switch %s" % account)]
|
||||
|
||||
rmo = telegram.InlineKeyboardMarkup(bottools.methods.makeMenu(buttons, 1))
|
||||
update.message.reply_text(bottools.strings.accounts % getTwo(update.message).whoami(), reply_markup = rmo)
|
||||
|
||||
else:
|
||||
update.message.reply_text(bottools.strings.noauth)
|
||||
|
||||
# User methods
|
||||
|
||||
def follow(bot, update, args):
|
||||
|
@ -599,6 +636,7 @@ def emergency(bot, update, args):
|
|||
# Variables
|
||||
|
||||
commands = {
|
||||
"accounts": accounts,
|
||||
"auth": auth,
|
||||
"broadcast": broadcast,
|
||||
"emergency": emergency,
|
||||
|
@ -619,6 +657,7 @@ commands = {
|
|||
"sharelocation": shareLocation,
|
||||
"silence": silence,
|
||||
"start": start,
|
||||
"switch": switch,
|
||||
"thread": thread,
|
||||
"timeline": timeline,
|
||||
"togglebroadcasts": togglebroadcasts,
|
||||
|
@ -645,6 +684,7 @@ pargs = [
|
|||
retweet,
|
||||
search,
|
||||
selfTweets,
|
||||
switch,
|
||||
thread,
|
||||
timeline,
|
||||
toggleTweet,
|
||||
|
|
|
@ -146,3 +146,9 @@ shareLocationDecline = '''Nope.'''
|
|||
unsetLocation = '''Alright, I will no longer use your location data.'''
|
||||
|
||||
noLocation = '''Okay, I will not change your location data.'''
|
||||
|
||||
accounts = '''You are currently logged in as @%s.
|
||||
|
||||
You can use the following accounts:'''
|
||||
|
||||
switch = '''You are now logged in as @%s.'''
|
||||
|
|
|
@ -84,7 +84,6 @@ class dbObject:
|
|||
for i in db.getAll():
|
||||
yield int(i[0])
|
||||
|
||||
|
||||
def getLatestMessage(db):
|
||||
db.executeQuery("SELECT max(id) FROM messages")
|
||||
try:
|
||||
|
@ -109,79 +108,79 @@ class dbObject:
|
|||
return False
|
||||
|
||||
def deleteUser(self, cid):
|
||||
self.executeQuery("DELETE FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("DELETE FROM tokens WHERE cid = %i AND active;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
def storeUser(self, cid, ato, ase):
|
||||
self.executeQuery("DELETE FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("INSERT INTO tokens(cid, ato, ase) VALUES(%i, '%s', '%s');" % (int(cid), ato, ase))
|
||||
self.executeQuery("UPDATE tokens SET active = 0 WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("UPDATE tokens SET ato = '%s', ase = '%s', active = 1 WHERE cid = %i AND ase IS NULL;" % (ato, ase, int(cid)))
|
||||
self.commit()
|
||||
|
||||
def storeLocation(self, cid, lat, lon):
|
||||
self.executeQuery("UPDATE tokens SET lat = %s, lon = %s WHERE cid = %i;" % (lat, lon, int(cid)))
|
||||
self.executeQuery("UPDATE tokens SET lat = %s, lon = %s WHERE cid = %i AND active;" % (lat, lon, int(cid)))
|
||||
self.commit()
|
||||
|
||||
def getLocation(self, cid):
|
||||
self.executeQuery("SELECT lat, lon FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT lat, lon FROM tokens WHERE cid = %i AND active;" % int(cid))
|
||||
re = self.getNext()
|
||||
return re[0], re[1]
|
||||
|
||||
def ato(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT ato FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT ato FROM tokens WHERE cid = %i AND active;" % int(cid))
|
||||
return self.cur.fetchone()[0]
|
||||
except:
|
||||
return False
|
||||
|
||||
def ase(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT ase FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT ase FROM tokens WHERE cid = %i AND active;" % int(cid))
|
||||
return self.cur.fetchone()[0]
|
||||
except:
|
||||
return False
|
||||
|
||||
def getBStatus(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT broadcast FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT broadcast FROM tokens WHERE cid = %i AND active;" % 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.executeQuery("UPDATE tokens SET broadcast = NOT broadcast WHERE cid = %i AND active;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
return self.getBStatus(cid)
|
||||
|
||||
def getCStatus(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT confirmations FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT confirmations FROM tokens WHERE cid = %i AND active;" % 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.executeQuery("UPDATE tokens SET confirmations = NOT confirmations WHERE cid = %i AND active;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
return self.getCStatus(cid)
|
||||
|
||||
def getTStatus(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT tweet FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT tweet FROM tokens WHERE cid = %i AND active;" % int(cid))
|
||||
return True if int(self.cur.fetchone()[0]) == 1 else False
|
||||
except:
|
||||
raise ValueError("No such user: %i" % int(cid))
|
||||
|
||||
def toggleTweet(self, cid):
|
||||
self.executeQuery("UPDATE tokens SET tweet = NOT tweet WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("UPDATE tokens SET tweet = NOT tweet WHERE cid = %i AND active;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
return self.getTStatus(cid)
|
||||
|
||||
def getMStatus(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT mentions FROM tokens WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("SELECT mentions FROM tokens WHERE cid = %i AND active;" % int(cid))
|
||||
|
||||
return True if int(self.cur.fetchone()[0]) == 1 else False
|
||||
|
||||
|
@ -189,18 +188,18 @@ class dbObject:
|
|||
raise ValueError("No such user: %i" % int(cid))
|
||||
|
||||
def toggleMentions(self, cid):
|
||||
self.executeQuery("UPDATE tokens SET mentions = NOT mentions WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("UPDATE tokens SET mentions = NOT mentions WHERE cid = %i AND active;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
return self.getMStatus(cid)
|
||||
|
||||
def mentionsOn(self):
|
||||
self.executeQuery("SELECT cid FROM tokens WHERE mentions;")
|
||||
self.executeQuery("SELECT cid FROM tokens WHERE mentions AND active;")
|
||||
for u in self.getAll():
|
||||
yield u[0]
|
||||
|
||||
def broadcastUsers(self):
|
||||
self.executeQuery("SELECT cid FROM tokens WHERE broadcast;")
|
||||
self.executeQuery("SELECT cid FROM tokens WHERE broadcast AND active;")
|
||||
for u in self.getAll():
|
||||
yield u[0]
|
||||
|
||||
|
@ -209,14 +208,36 @@ class dbObject:
|
|||
for u in self.getAll():
|
||||
yield u[0]
|
||||
|
||||
def accounts(self, cid):
|
||||
self.executeQuery("SELECT ato, ase FROM tokens WHERE cid = %i AND ase IS NOT NULL;" % int(cid))
|
||||
for a in self.getAll():
|
||||
yield [a[0], a[1]]
|
||||
|
||||
def addFish(self, cid):
|
||||
self.executeQuery("UPDATE tokens SET fish = fish + 1 WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("UPDATE tokens SET fish = fish + 1 WHERE cid = %i AND active;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
def storeToken(self, cid, ato):
|
||||
self.executeQuery('DELETE FROM tokens WHERE cid = %i AND ase IS NULL;' % int(cid))
|
||||
self.executeQuery('INSERT INTO tokens(cid, ato) VALUES(%i, "%s");' % (int(cid), ato))
|
||||
self.commit()
|
||||
|
||||
def getToken(self, cid):
|
||||
try:
|
||||
self.executeQuery("SELECT ato FROM tokens WHERE cid = %i AND ase IS NULL;" % int(cid))
|
||||
return self.getNext()[0]
|
||||
except:
|
||||
raise ValueError("No such user: %i" % int(cid))
|
||||
|
||||
def deleteToken(self, cid):
|
||||
self.executeQuery("DELETE FROM tokens WHERE cid = %i AND ase IS NULL;" % int(cid))
|
||||
self.commit()
|
||||
|
||||
def setActive(self, cid, ato):
|
||||
self.executeQuery("UPDATE tokens SET active = 0 WHERE cid = %i;" % int(cid))
|
||||
self.executeQuery("UPDATE tokens SET active = 1 WHERE cid = %i AND ato = '%s';" % (int(cid), ato))
|
||||
self.commit()
|
||||
|
||||
def dbHelper():
|
||||
if setuptools.dbtype() == SQLITE:
|
||||
return dbObject(dbtype=SQLITE, path=setuptools.dbpath())
|
||||
|
|
2
setup.py
2
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, `confirmations` BOOLEAN DEFAULT 1, `lat` REAL DEFAULT NULL, `lon` REAL DEFAULT NULL);")
|
||||
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, `lat` REAL DEFAULT NULL, `lon` REAL DEFAULT NULL, `active` BOOLEAN DEFAULT 1);")
|
||||
db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));")
|
||||
db.commit()
|
||||
|
||||
|
|
Loading…
Reference in a new issue