Improve mention handling. Less code = better. Part 2.

This commit is contained in:
Klaus-Uwe Mitterer 2017-04-03 14:30:34 +02:00
parent 1736012d66
commit b39aef819e
1 changed files with 31 additions and 29 deletions

View File

@ -51,7 +51,7 @@ def callback(bot, update):
return
if status:
tweetMessage(status, None, bot, callback = update.callback_query)
tweetMessage(status, update.callback_query.message.chat_id, bot, callback = update.callback_query)
def captionHelper(bot, update):
if update.message.caption.startswith("@%s /"):
@ -321,30 +321,33 @@ def explicitTweet(bot, update, args, reply = None):
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
def getMentions(tweet, two, args = []):
sender = tweet.user.screen_name
try:
text = tweet.full_text
except:
text = tweet.text
mentions = []
for m in re.split('[^\w@]+', text):
try:
if m[0] == "@" and m[0].strip() != "" and not (m in args or m.strip() == "" or m == "@%s" % two.whoami().strip("@") or m == "@%s" % sender.strip("@")):
mentions += [m]
except:
pass
if not sender.strip("@") == two.whoami().strip("@"):
mentions = ["@%s" % sender.strip("@")] + mentions
return mentions
def reply(bot, update, args):
try:
reply = bottools.methods.getTweetID(args[0], update.message.chat_id)
two = bottools.methods.getTwo(update.message)
otweet = two.getTweet(reply)
sender = otweet.user.screen_name
if not ("@%s" % sender.strip("@")) in [("@%s" % a.strip("@")) for a in args]:
mentions = []
for m in re.split('[^\w@]+', otweet.full_text):
try:
if m[0] == "@" and m[0].strip() != "":
mentions += [m]
except:
pass
for m in mentions:
if m in args or m.strip() == "" or m =="@%s" % two.whoami().strip("@") or m == "@%s" % sender.strip("@"):
mentions.remove(m)
else:
mentions = []
mentions = getMentions(otweet, two, args)
pargs = mentions + args[1:]
except:
@ -463,15 +466,14 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No
except:
text = status.full_text
if not (force or callback):
try:
two = twitools.twoBotHelper(cid)
except tweepy.error.TweepError as e:
logging.exception("I really don't see how this could possibly happen.")
try:
two = twitools.twoBotHelper(cid)
except tweepy.error.TweepError as e:
logging.exception(e)
if not callback:
if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid):
return
if not callback:
if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid):
return
if not callback:
try:
@ -507,7 +509,7 @@ def tweetMessage(status, cid, bot, force = False, callback = None, notified = No
buttons += [telegram.InlineKeyboardButton("Retweet", callback_data = "/retweet %i" % i)]
buttons += [
telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i " % i),
telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i %s" % (i, ' '.join(getMentions(status, two)))),
telegram.InlineKeyboardButton("Quote", switch_inline_query_current_chat = "/quote %i " % i)
]