def on_created(self, event): super().on_created(event) # TODO: Filter on file size? with open(event.src_path) as f: src = Path(event.src_path) src_file_name = src.name if self.res_file_name is None: self.res_file_name = src_file_name res_path = self.res_dir.joinpath(self.res_file_name) handlers.handle(f.readline(), res_path)
def OnMessageStatus(Message, Status): if Status == 'RECEIVED' and Message.Body[0] == '#': if Message.Body.lower()[1:] == "reload" and commands.canreload(Message) == 1: Message.Chat.SendMessage('Reloading server commands.') reload(commands) reload(handlers) reload(useradd) elif Message.Body.lower()[1:] == "host": try: Message.Chat.SendMessage("/golive") except: nill = '' Message.Chat.SendMessage(Message.FromDisplayName + " started a group call.") elif Message.Body.lower()[1:4] == "add": params = Message.Body[4:] param = params.split() msg = params.find(param[0]) msg = params[msg + len(param[0]):] f = open('./useradd.py', 'a') f.write("\ndef " + param[0] + "(Message):\n Message.Chat.SendMessage('" + msg + "')") f.close() reload(useradd) else: error = 0 try: cmd = "commands." + Message.Body.lower()[1:] + "(Message)" exec cmd except AttributeError or SyntaxError: error = 1 try: cmd = "useradd." + Message.Body.lower()[1:] + "(Message)" exec cmd error = 0 except AttributeError or SyntaxError: error = 1 if error == 1: print Message.FromDisplayName + ':Command not found ' + Message.Body[1:] Message.Chat.SendMessage("Command not found.") else: handlers.handle(Message)
def handle(user, message): rm = tel.ReplyKeyboardRemove.create() try: answer, keyboard = handlers.handle(user, message.text) print(answer, keyboard) if keyboard: rm = tel.ReplyKeyboardMarkup.create(keyboard) if answer: if isinstance(answer, str): bot.send_message(user.id, answer, reply_markup=rm) else: bot.send_photo(user.id, answer, reply_markup=rm) else: bot.send_message(user.id, 'Я не могу понять что ты от меня хочешь', reply_markup=rm) except Exception as e: print(e)
def create_reply(id, body): requests = set(re.findall('/u/{} (.*)'.format(name), body)) if handlers.has(requests): print 'Preparing reply for {}'.format(id) response = '' for request in requests: result = handlers.handle(request) if result != None: final = '' for line in result.split('\n'): if line != '': if not line.startswith('['): line = '> ' + line final = final + line + '\n\n' final = final + '---\n' if len(signature) + len(final) + len(response) <= 10000: response = response + final if response == '': return None return hide_string + response + signature return None
def listener(messages): global config, logger, token, auth_chats for m in messages: chat_id = m.chat.id tb.send_chat_action(chat_id, "typing") res = text_res("") send_markup = False res_markup = types.ReplyKeyboardMarkup() if is_authorized_chat(m): if m.content_type == "text": msg = m.text if msg == "/begin": res = text_res("Hi there!") elif msg.startswith("/pair"): if chat_id in auth_chats: res = text_res("Already authenticated") else: tokens = msg.split(" ") if len(auth_chats) < config["telegram"]["authorization"]["max_chats"]: res = text_res("Please enter authentication token: /auth <token>") token[str(chat_id)] = {"time": None, "key": id_generator()} logger.info("Authentication token for: " + str(chat_id) + ": " + token[str(chat_id)]["key"]) else: logger.info("Cannot authenticate: " + str(chat_id) + ": " + m.chat.username) elif msg.startswith("/auth"): if chat_id in auth_chats: res = text_res("Already authenticated") else: tokens = msg.split(" ") if len(tokens) == 2 and str(chat_id) in token and tokens[1] == token[str(chat_id)]["key"]: status = add_auth_chat(chat_id) if status: logger.info("Authenticated chat for: {0} [{1}]".format(m.chat.username, str(chat_id))) res = text_res("Successfully authenticated.") send_markup = True else: res = text_res("Cannot authenticate.") else: res = text_res("Invalid token. Please try again.") else: res = handlers.handle(msg, config, logger) else: res = text_res("Content type not handled. Just yet!: " + m.content_type) else: if len(auth_chats) < config["telegram"]["authorization"]["max_chats"]: res = text_res("Unrecognized chat. Please authorize using /pair") send_markup = True res_markup.row("/pair") else: res = text_res("Cannot authenticate.") logger.info("Failed authentication attempt for: " + str(chat_id) + ": " + m.chat.username) if res is not None: if res["type"] == "text": if send_markup: tb.send_message(chat_id, res["message"], reply_markup=res_markup) else: tb.send_message(chat_id, res["message"]) elif res["type"] == "image": with open(res["path"], "rb") as f: tb.send_photo(chat_id, f) f.close() elif res["type"] == "document": logger.debug("Starting upload:") with open(res["path"], "rb") as f: tb.send_document(chat_id, f) f.close() logger.debug("Done uploading") else: pass else: pass
def listener(messages): global config, logger, token, auth_chats for m in messages: chat_id = m.chat.id tb.send_chat_action(chat_id, 'typing') res = text_res('') send_markup = False res_markup = types.ReplyKeyboardMarkup() if is_authorized_chat(m): if m.content_type == 'text': msg = m.text if msg == '/begin': res = text_res('Hi there!') elif msg.startswith('/pair'): if chat_id in auth_chats: res = text_res('Already authenticated') else: tokens = msg.split(' ') if len(auth_chats) < config['telegram']['authorization']['max_chats']: res = text_res('Please enter authentication token: /auth <token>') token[str(chat_id)] = {'time': None, 'key': id_generator()} logger.info('Authentication token for: ' + str(chat_id) + ': ' + token[str(chat_id)]['key']) else: logger.info('Cannot authenticate: ' + str(chat_id) + ': ' + m.chat.username) elif msg.startswith('/auth'): if chat_id in auth_chats: res = text_res('Already authenticated') else: tokens = msg.split(' ') if len(tokens) == 2 and str(chat_id) in token and tokens[1] == token[str(chat_id)]['key']: status = add_auth_chat(chat_id) if status: logger.info('Authenticated chat for: {0} [{1}]'.format(m.chat.username, str(chat_id))) res = text_res('Successfully authenticated.') send_markup = True else: res = text_res('Cannot authenticate.') else: res = text_res('Invalid token. Please try again.') else: res = handlers.handle(msg, config, logger) else: res = text_res('Content type not handled. Just yet!: ' + m.content_type) else: if len(auth_chats) < config['telegram']['authorization']['max_chats']: res = text_res('Unrecognized chat. Please authorize using /pair') send_markup = True res_markup.row('/pair') else: res = text_res('Cannot authenticate.') logger.info('Failed authentication attempt for: ' + str(chat_id) + ': ' + m.chat.username) if res is not None: if res['type'] == 'text': if send_markup: tb.send_message(chat_id, res['message'], reply_markup=res_markup) else: tb.send_message(chat_id, res['message']) elif res['type'] == 'image': with open(res['path'], 'rb') as f: tb.send_photo(chat_id, f) f.close() elif res['type'] == 'document': logger.debug('Starting upload:') with open(res['path'], 'rb') as f: tb.send_document(chat_id, f) f.close() logger.debug('Done uploading') else: pass else: pass
def handler(): event = json.loads(request.data) handlers.handle(event) return 'OK'