Giter Site home page Giter Site logo

Comments (9)

BrandonSmith avatar BrandonSmith commented on September 24, 2024 1

Tested. Subflow access to chat context from global context works in 0.5.9-beta. Thanks.

from node-red-contrib-chatbot.

guidone avatar guidone commented on September 24, 2024

@BrandonSmith should be

var chat = context.flow.get('chat:' + chatId); chat.get(...)

from node-red-contrib-chatbot.

BrandonSmith avatar BrandonSmith commented on September 24, 2024

@guidone context is what I started out with, but has the same result.

var chatId = msg.originalMessage.chat.id
console.log(chatId)
var chat = context.flow.get('chat:' + chatId)
console.log(chat)
chat.get('email')

Results with:

G1429V2JH
undefined
2 Sep 15:05:53 - [error] [function:Email payload scrub] TypeError: Cannot read property 'get' of undefined

However, if the listen node and function node are executing from the top-level flow, everything works as expected:

G1429V2JH
{ get: [Function: get],
  set: [Function: set],
  dump: [Function: dump],
  all: [Function: all] }

from node-red-contrib-chatbot.

BrandonSmith avatar BrandonSmith commented on September 24, 2024

@guidone I think the issue is in chatbot-listen.js. Namely, the ChatContext is retrieved from the flow context or created if it doesn't exist. In the condition it doesn't exist, the locally created ChatContext never gets put back into the flow context.

The following patch seems to work:

      var chatContext = context.flow.get('chat:' + chatId);
      if (!chatContext) {
          chatContext = ChatContext(chatId);
          context.flow.set('chat:' + chatId, chatContext);
      }

from node-red-contrib-chatbot.

guidone avatar guidone commented on September 24, 2024

Good catch @BrandonSmith , I'll include your fix tomorrow

Thanks!

from node-red-contrib-chatbot.

guidone avatar guidone commented on September 24, 2024

@BrandonSmith I've pushed a fix in 0.5.8

But it's strange, your fix is right but the chat context is always created in a receiver node.
Is it possible that you used a receiver node in the top level flow and the listen node in a subflow?

Subflows don't share the same context with the top level flow, I can fix it by storing all the chat contexts in the global context, in this way they will be available everywhere.

Thanks for your feedback

from node-red-contrib-chatbot.

BrandonSmith avatar BrandonSmith commented on September 24, 2024

That is the exact configuration I am exploring... Receive and send in the top-level flow and reusable bits in subflows.

Again, I'm a node-red newb and am only just learning the difference between node, flow, and global contexts. Seems like it would be a common use case to want data living owning flow contexts to be injected into subflows. I understand the value of a "pristine" context being created so that subflows don't make any assumptions about context. What is the idiomatic node-red way here? Is there a simple way of lifting context values and pass as explicit inputs into a subflow?

from node-red-contrib-chatbot.

guidone avatar guidone commented on September 24, 2024

The node context is available just in the node, and is cleared every time the node is redeployed.
Flow context is available for all nodes of the flow but not subflow, it's cleared when the flow is redeployed.
Global is available for every flow.

I'm not sure about what is the idiomatic way but

  • it's ok to store chat data in the global context, generally in hosted environment like FRED, they run multiple instance of Node-Red for different customers, so any sensitivie information that might be stored in global context is not exposed
  • it's better to store in chat context the results of parsing, etc in order to not mix informations of concurrent users of the chat bot

I've a branch ready for that, it moves the chat context under the global context, so it's available via script in subflows

// get the chat context
var chatId = msg.originalMessage.chat.id
var chat = context.global.get('chat:' + chatId);

Unfortunately it's a breaking change, I'll push in a while with a beta release tag, I'll keep you posted.

from node-red-contrib-chatbot.

guidone avatar guidone commented on September 24, 2024

ok @BrandonSmith, would you like to try

npm install [email protected]

from node-red-contrib-chatbot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.