def answer_inline_query(self, text='', **params): if 'inline_query_id' not in params: params['inline_query_id'] = self.inline_query_id if 'text' not in params: params['text'] = text if 'results' not in params: params['results'] = [] if 'results' in params and isinstance(params['results'], list): def is_result(x): return isinstance(x, InlineQueryResult) params['results'] = utils.json_dumps( list( map(lambda x: x.to_dict() if is_result(x) else x, params['results']))) req = HTTPRequest(self.get_method_url('answerInlineQuery'), method="POST", body=urlencode(params)) try: res = yield self.client.fetch(req) data = ujson.loads(res.body) return data.get('result', {}) except HTTPError as e: self.logger.error(e.response.body)
def suggest(self): query = self.get_str_argument('query') exchanges = Exchange.objects.filter(Q(title__startswith=query) & Q(active=True)).limit(10) suggestions = [] for exchange in exchanges: suggestions.append({ 'data': exchange.get_id(), 'value': exchange.title }) self.write(utils.json_dumps({'suggestions': suggestions}))
def send_response(self, status=ServerError.OK, message=None, data=None, field=None, field_problem=None): if not isinstance(status, ServerResponseStatus): status = ServerError.UNKNOWN if data is None: data = {} self.set_status(status.http_code) response = { 'status': status.alias, 'data': data } if message is not None: response['message'] = message if field is not None: response['field'] = field if field_problem is not None: response['field_problem'] = field_problem self.write(utils.json_dumps(response))
def _on_image(cls, chat_id, photo): if len(photo) == 3: photo = photo[2] elif len(photo) == 2: photo = photo[1] elif len(photo) == 1: photo = photo[0] else: return response = requests.get(cls.get_method_url('getFile'), { 'file_id': photo.get('file_id'), }) response = utils.json_loads(response.content) url = cls.get_file_url(response.get('result').get('file_path')) response = requests.post( 'https://api.projectoxford.ai/face/v1.0/detect?returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses', headers={ 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': '07974eb651b84489b9bd0ff0900531de' }, data=utils.json_dumps({'url': url})) response = utils.json_loads(response.content) if len(response) > 0: for face in response: attrs = face.get('faceAttributes') msg = u"Это {0}, возраст {1}".format( u'мальчик' if attrs.get('gender') == 'male' else u'девочка', int(attrs.get('age')) ) cls.send(chat_id=chat_id, text=msg) else: cls.send(chat_id=chat_id, text=u'Тут нет людей')
def to_json(self): return utils.json_dumps(self.to_dict())