def checkMQTTSubscription(client, userdata, message): topic = str(message.topic) message = str(message.payload.decode("utf-8")) #if topic.startswith(topic_sub): # pass ## TODO: check topics MQTTData[topic] = [utils.getStrDateTime(), message] utils.myLog('MQTT: ' + utils.getStrDateTime() + ' ' + topic + ' - ' + message)
def sendMsg2Admin(message): utils.myLog(message) if config.ADMIN_USER != None: TelegramBase.send_message(utils.getStrDateTime() + " " + message, config.ADMIN_USER) else: utils.myLog('No admin user id')
def checkMQTTSubscription(client, userdata, message): global bUpdateCalderaStatus, bEsperandoRespuestaCaldera, bInitConsola topic = str(message.topic) message = str(message.payload.decode('utf-8')) MQTTData[topic] = [utils.getStrDateTime(), message] logmsg = 'MQTT < ' + topic + ' - ' + message utils.myLog(logmsg) if topic == config.topicCalderaStatus: bUpdateCalderaStatus = True elif topic == config.topic_subCalderaAction: bEsperandoRespuestaCaldera = True elif topic == config.topic_subInitConsola: bInitConsola = True utils.myLog('got initconsole') elif topic in dumbTopics: pass else: utils.myLog('Unknown msg: ' + logmsg)
def updateBot(bot): """Answer the message the user sent.""" global update_id global chat_id global time_between_picture global welcomeMsg global nightMode global bReboot #utils.myLog('Updating telegramBot') # Request updates after the last update_id for update in bot.get_updates(offset=update_id, timeout=10): update_id = update.update_id + 1 if update.message: # your bot can receive updates without messages # Proccess the incoming message comando = update.message.text # message text command_time = update.message.date # command date user = update.message.from_user #User full objetct chat_id = int(update.message.from_user.id) user_real_name = user.first_name #USER_REAL_NAME if chat_id not in config.ALLOWED_USERS: message = 'User: {} not allowed. Chat_id {} command: {}. Will be reported'.format( str(user_real_name), str(chat_id), comando) sendMsg2Admin(message) break TelegramBase.chat_ids[user_real_name] = [command_time, chat_id] utils.myLog('Command: ' + comando + ' from user ' + str(user_real_name) + ' in chat id:' + str(chat_id) + ' at ' + str(command_time)) if comando == cmdStart: update.message.reply_text(welcomeMsg, reply_markup=user_keyboard_markup) elif comando == cmdHi: update.message.reply_text('Hello {}'.format( update.message.from_user.first_name), reply_markup=user_keyboard_markup) elif comando == cmdInfo: answer = 'Info: ' + utils.getStrDateTime( ) + '\n==========================\n\n' + 'Tiempo entre imágenes: ' + getTimeLapseStr( ) + '\n ' + str(len(os.listdir( config.ImagesDirectory))) + ' imágenes' update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdHelp: bot.send_message(chat_id=chat_id, text=commandList, reply_markup=user_keyboard_markup) elif comando == cmdUsers: sUsers = TelegramBase.getUsersInfo() TelegramBase.send_message(sUsers, chat_id) elif comando == cmdDayMode: nightMode == False update.message.reply_text('Day mode', reply_markup=user_keyboard_markup) elif comando == cmdNightMode: nightMode == True update.message.reply_text('Night mode', reply_markup=user_keyboard_markup) elif comando == cmdPhoto: answer = getImage() utils.myLog(answer) TelegramBase.send_picture(answer, chat_id) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdLastPhoto: imagenes = os.listdir(config.ImagesDirectory) answer = config.ImagesDirectory + sorted(imagenes)[-1] TelegramBase.send_picture(answer, chat_id) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdListPhotos: imagenes = sorted(os.listdir(config.ImagesDirectory)) answer = str( len(imagenes)) + ' Imágenes\n----------------------\n' utils.myDebug(answer) contadorImagenes = 1 for imagen in imagenes: answer += str(contadorImagenes) + ' ' + imagen + '\n' contadorImagenes += 1 utils.myDebug(answer) if len(imagenes) > 70: answer = answer[0:2041] + ' \n...' update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando.startswith('/N'): numero = int(comando[2:]) imagenes = sorted(os.listdir(config.ImagesDirectory)) answer = config.ImagesDirectory + imagenes[numero] utils.myLog(answer) TelegramBase.send_picture(answer, chat_id) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando.startswith('/T'): time_between_picture = int(comando[2:]) answer = getTimeLapseStr() utils.myLog(answer) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando.startswith('/image'): answer = config.ImagesDirectory + comando[1:] TelegramBase.send_picture(answer, chat_id) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdTemp: answer = raspi.getTemp() utils.myLog(answer) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdDF: answer = raspi.getDiskUsed() utils.myLog(answer) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdIP: answer = raspi.getIP() utils.myLog(answer) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == cmdReboot: answer = 'Reboot in 10 seconds!!!' utils.myLog(answer) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) bReboot = True else: update.message.reply_text('echobot: ' + update.message.text, reply_markup=user_keyboard_markup)
# MQTT utils import paho.mqtt.client as mqtt # Import the MQTT library import config import utils MQTTData = {'initTime': [utils.getStrDateTime(), utils.getStrDateTime()]} # Our "on message" event def checkMQTTSubscription(client, userdata, message): topic = str(message.topic) message = str(message.payload.decode("utf-8")) #if topic.startswith(topic_sub): # pass ## TODO: check topics MQTTData[topic] = [utils.getStrDateTime(), message] utils.myLog('MQTT: ' + utils.getStrDateTime() + ' ' + topic + ' - ' + message) def initMQTT(): global ourClient ourClient = mqtt.Client( "CBT_bot_mqtt") # Create a MQTT client object with this id ourClient.connect(config.MQTT_SERVER, 1883) # Connect to the test MQTT broker utils.myLog('Conectado a MQTT broker ' + config.MQTT_SERVER) ourClient.subscribe(config.BaseTopic_sub + '/#') # Subscribe to the topic ourClient.on_message = checkMQTTSubscription # Attach the messageFunction to subscription ourClient.loop_start() # Start the MQTT client
def updateBot(bot): """Answer the message the user sent.""" global update_id global chat_id global camera #utils.myLog('Updating telegramBot') # Request updates after the last update_id for update in bot.get_updates(offset=update_id, timeout=10): update_id = update.update_id + 1 if update.message: # your bot can receive updates without messages # Proccess the incoming message comando = update.message.text # message text command_time = update.message.date # command date user = update.message.from_user #User full objetct chat_id = int(update.message.from_user.id) user_real_name = user.first_name #USER_REAL_NAME TelegramBase.chat_ids[user_real_name] = [command_time, chat_id] utils.myLog('Command: ' + comando + ' from user ' + str(user_real_name) + ' in chat id:' + str(chat_id) + ' at ' + str(command_time)) if comando == '/start': update.message.reply_text("Bienvenido al Bot de riego " + v, reply_markup=user_keyboard_markup) elif comando == 'hi': update.message.reply_text('Hello {}'.format( update.message.from_user.first_name), reply_markup=user_keyboard_markup) elif comando == '/info': answer = 'Datos @ ' + utils.getStrDateTime( ) + '\n==========================\n\n' + riego.getFullData() update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == '/help': bot.send_message(chat_id=chat_id, text=commandList, reply_markup=user_keyboard_markup) elif comando == '/users': sUsers = TelegramBase.getUsersInfo() TelegramBase.send_message(sUsers, chat_id) elif comando.startswith('/riego'): if comando == '/riegoOff': resultado = riego.riegoOff() elif comando == '/riegoOn': resultado = riego.riegoOn() elif comando == '/riegoPalet': resultado = riego.riegoOnSector(config.SectorPalet) elif comando == '/riegoMacetones': resultado = riego.riegoOnSector(config.SectorMacetones) elif comando == '/riegoMacetas': resultado = riego.riegoOnSector(config.SectorMacetas) else: utils.myLog('Comando: ' + comando + ' no reconocido') answer = 'Datos @ ' + utils.getStrDateTime( ) + '\n==========================\n\n' + riego.getFullData() update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) elif comando == '/imagen': answer = 'No implementada ' + comando if camera != None: imageFile = camara.getImage(camera) answer = imageFile utils.myLog(answer) TelegramBase.send_picture(imageFile, chat_id) update.message.reply_text( answer, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=user_keyboard_markup) else: update.message.reply_text('echobot: ' + update.message.text, reply_markup=user_keyboard_markup)