Last active
December 14, 2018 00:10
-
-
Save jefcolbi/0c954afb86b40d90b47ab2d231ca46cc to your computer and use it in GitHub Desktop.
Handler with support of args
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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