def command_callback(update, context): command = update.effective_message.text endpoint = CONFIG.get_webhook(command.replace('/', '')) resp = service_client.get(endpoint) LOG.info(resp) context.bot.send_message(chat_id=update.effective_chat.id, text=str(resp.content))
async def handler(request): data = await request.json() is_valid = validate_data(data) if is_valid: custom_send(chat_id=data['chat_id'], text=data['text']) return web.Response(status=200) else: err = f'data {data} must contain chat_id and text keys' LOG.error(err) return web.Response(status=500, body=err)
def add_module_handlers(module_name): try: module = importlib.import_module(f'bot.modules.{module_name}').Module LOG.info( f'module imported - {module.name} (handlers: {len(module.handlers)})' ) for handler in module.handlers: dispatcher.add_handler(handler) except Exception as e: LOG.error(e)
def get(self, endpoint, **url_params) -> r.Response: """ GET request :param endpoint: endpoint for request :param url_params: params for GET not supported :return: request.Response object """ url = self.base_url + endpoint LOG.info(f'GET request on {url}') response = self.session.request(method='GET', url=url) LOG.info(f'finished with {response}') return response
def files_callback(update, context): file = context.bot.getFile(update.message.document.file_id) file_path = CONFIG.tmp_path / update.effective_message.document.file_name file.download(file_path) text = update.effective_message.caption file_ext = update.effective_message.document.mime_type file_type = CONFIG.get_file_type(file_ext) url = CONFIG.get_webhook(file_type) chat_id = update.effective_chat.id params = parse_args(text) data = {'chat_id': chat_id, 'params': params} with open(str(file_path), 'rb') as f: resp = service_client.post(url=url, body=data, file=f) LOG.info(resp) context.bot.send_message(chat_id=update.effective_chat.id, text=str(resp.content))
def post(self, endpoint, body=None, file=None) -> r.Response: """ POST request :param endpoint: endpoint for request :param body: request body :param file: file for sending :return: requests.Response object """ url = self.base_url + endpoint files_data = {} if file is not None: files_data['file'] = file if body is not None: files_data['data'] = json.dumps(body) LOG.info(f'POST request on {url}, with files data {files_data}') response = self.session.request(method='POST', url=url, files=files_data) LOG.info(f'finished with {response}') return response
def custom_send(chat_id, text): LOG.info('custom send command') bot.sendMessage(chat_id=chat_id, text=text)
def start_message(update, context): LOG.info('/start command') ans_msg = f'{CONFIG.start_description}\n your chat_id is: {update.effective_chat.id}' context.bot.send_message(chat_id=update.effective_chat.id, text=ans_msg)
def help_message(update, context): LOG.info('/help command') context.bot.send_message(chat_id=update.effective_chat.id, text=CONFIG.help_description)