Hi guys I have a problem between Node-Red and Python communicating through the broker Mqtt mosquitto. The problem is that I cannot convert the message.payload sent by the broker to python to pass it as simple user input.
Code Example:
#!/usr/bin/python3
import os
import sys
import json
import io
import aiml
def on_message(client, userdata, message):
print(str(message.payload.decode("utf-8")))
#MQTT
from paho.mqtt.client import *
client = Client(client_id = "IA")
client.connect("localhost")
client.loop_start()
client.subscribe("domanda")
client.on_message=on_message
if os.path.exists(BRAIN_FILE):
print("Carico intelligenza artificiale: " + BRAIN_FILE)
k.loadBrain(BRAIN_FILE)
else:
print("Carico file AIML")
k.bootstrap(learnFiles="std-startup.aiml", commands="load aiml b")
print("Salvo file intelligenza Artificiale: " + BRAIN_FILE)
k.saveBrain(BRAIN_FILE)
Inserimento domanda
while True:
input_text = input("Dimmi... ")
#Non mi passa richiesta Broker
**response = k.respond(input_text)**
#Editare per escludere risposta a terminale
print(response)
#Invia Risposta a Broker
client.publish(topic = "risposta", payload = (response))
If I print on the screen "print (str (message.payload.decode (" utf-8 ")))" the message is displayed correctly, if I try to send it as input "response = k.respond (str (message.payload.decode ("utf-8")) "I'm getting errors
in summary I should send a string value with subscribe Mosquitto and send it to python as if it were user input. Thank you all for any help