def requestImage(data): try: output = '' if (data != ''): data = utils.base64Decode(data) input = BytesIO(data) output = BytesIO() image.prepareImage(input, output) topic = config.env[config.MQTT_TOPIC_RESPONSE] message = json.dumps({ 'type': 'image', 'value': utils.base64Encode(output.getvalue()) }) mqttPublish(topic, message) output.seek(0) input = output output = BytesIO() image.rotateImage(input, output) output = utils.base64Encode(output.getvalue()) topic = config.env[config.MQTT_TOPIC_CONTROL] message = json.dumps({'type': 'image', 'value': output}) mqttPublish(topic, message) except Exception as exception: logger.exception('requestImage: failed to process request: %s', utils.getExceptionMessage(exception))
def contentPhoto(update, context): checkUser(context.bot, update.message.from_user) updateChatId(update.message.chat.id) image = update.message.photo[-1] logger.info('image: from: {0}/{1}, image: {2}'.format( update.message.from_user.name, update.message.from_user.id, image.file_id)) try: imageFile = context.bot.get_file(image.file_id) imagePath = config.outputDir + 'image.jpg' imageFile.download(imagePath) output = BytesIO() with open(imagePath, "rb") as image: shutil.copyfileobj(image, output) output.seek(0) topic = config.env[config.MQTT_TOPIC_REQUEST] message = json.dumps({ 'type': 'image', 'value': utils.base64Encode(output.getvalue()) }) mqttPublish(topic, message) update.message.reply_text('ok') except Exception as exception: logger.exception('photo: failed to process photo: %s', utils.getExceptionMessage(exception)) update.message.reply_text('Something went wrong...')
def mqttOnMessage(client, userdata, message): # print('mqttOnMessage:' + message.topic + ": " + message.payload.decode("utf-8")[0:20]) try: data = json.loads(message.payload.decode("utf-8"), encoding='utf-8') requestTypeSwitcher[data['type']](data['value']) except Exception as exception: logger.exception('mqttOnMessage: failed to process request: %s', utils.getExceptionMessage(exception))
def postImage(image): if (chatId != None): try: dispatcher.bot.send_photo( chat_id=chatId, photo=BytesIO(image)) #open('tests/test.png', 'rb')) except Exception as exception: logger.exception('photo: failed to send photo: %s', utils.getExceptionMessage(exception))
def requestClock(data): try: output = data topic = config.env[config.MQTT_TOPIC_CONTROL] message = json.dumps({'type': 'clock', 'value': output}) mqttPublish(topic, message) except Exception as exception: logger.exception('requestClock: failed to process request: %s', utils.getExceptionMessage(exception))
def mqttOnMessage(client, userdata, message): # print('mqttOnMessage:' + message.topic + ": " + message.payload.decode("utf-8")[0:20]) try: data = json.loads(message.payload.decode("utf-8"), encoding='utf-8') if (data['type'] == 'image'): postImage(utils.base64Decode(data['value'])) except Exception as exception: print('mqttOnMessage: failed to process request: ' + utils.getExceptionMessage(exception))
def commandText(update, context): checkUser(context.bot, update.message.from_user) updateChatId(update.message.chat.id) try: topic = config.env[config.MQTT_TOPIC_REQUEST] message = json.dumps({'type': 'text', 'value': ''}) mqttPublish(topic, message) update.message.reply_text('ok') except Exception as exception: logger.exception('text: failed to process command: %s', utils.getExceptionMessage(exception)) update.message.reply_text('Something went wrong...')
def contentText(update, context): checkUser(context.bot, update.message.from_user) updateChatId(update.message.chat.id) logger.info('text: from: {0}/{1}, message: {2}'.format( update.message.from_user.name, update.message.from_user.id, update.message.text)) try: topic = config.env[config.MQTT_TOPIC_REQUEST] message = json.dumps({'type': 'text', 'value': update.message.text}) mqttPublish(topic, message) update.message.reply_text('ok') except Exception as exception: logger.exception('photo: failed to process text: %s', utils.getExceptionMessage(exception)) update.message.reply_text('Something went wrong...')