Skip to content

Instantly share code, notes, and snippets.

@jefcolbi
Last active December 14, 2018 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefcolbi/0c954afb86b40d90b47ab2d231ca46cc to your computer and use it in GitHub Desktop.
Save jefcolbi/0c954afb86b40d90b47ab2d231ca46cc to your computer and use it in GitHub Desktop.
Handler with support of args
import re
import telegram
rgx_tags = re.compile("\W?#(\w+)")
rgx_args = re.compile("\W?/(\w+)")
rgx_users = re.compile("\W?@(\w+)")
class InlineCommandHandler(telegram.ext.Handler):
def check_update(self, update):
try:
text = update.message.text if update.message != None else update.callback_query.data
if text.startswith('/'):
splitten = text.split(' ', 1)
if len(splitten) > 1:
rest = splitten[1]
update.message.rest = rest
tags = rgx_tags.findall(rest)
args = rgx_args.findall(rest)
users = rgx_users.findall(rest)
update.message.tags = tags
update.message.args = args
update.message.users = users
splitten = splitten[0].split("@")
command = splitten[0].replace('/', '')
update.message.type = "command"
update.message.command = command
return True
else:
return False
except:
import traceback
traceback.print_exc()
return False
def handle_update(self, update, dispatcher):
self.callback(dispatcher.bot, update)
class CommandHandler(telegram.ext.Handler):
def check_update(self, update:telegram.Update):
if update.effective_chat.type != "private":
return False
try:
text = update.message.text if update.message != None else update.callback_query.data
if text.startswith('/'):
splitten = text.split(' ', 1)
if len(splitten) > 1:
rest = splitten[1]
update.message.rest = rest
tags = rgx_tags.findall(rest)
args = rgx_args.findall(rest)
users = rgx_users.findall(rest)
update.message.tags = tags
update.message.args = args
update.message.users = users
splitten = splitten[0].split("@")
command = splitten[0].replace('/', '')
update.message.type = "command"
update.message.command = command
return True
else:
return False
except:
import traceback
traceback.print_exc()
return False
def handle_update(self, update, dispatcher):
self.callback(dispatcher.bot, update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment