How to create communication channel between 2 nodes?

Hello Node red community,

I have created a very simple nod-red Flow as follows:

Now question comes is: Whenever I am entering 1 option its going to JS function and accordingly it prints values. But my question is how to create a communication channel between JS function and telegram end node(right side node)? So that until chat is really ended(suppose any operation has multiple inputs 1 by 1 from user in that case we need a communication channel between these) OR I have all the inputs from user its NOT really ended.

Will be grateful to you if you could guide me on same.

Thanks,
Ravinder Singh

I don't use telegram.

But doesn't the chat coming in (left), contain a Chat ID or some other identifier for the current interaction taking place?

so when you send a response - you also include that ID - so the response (right) ends up in the right chat session?

I'm guessing this is what you are trying to achieve?

Much more experienced folk here than Myself :sweat_smile:

There are examples in the documentation of that node that do what you are asking.

1 Like

Thank you @TotallyInformation for your response here. Could you please do give me link of that documentations OR link to any existing sample project(I tried searching in forums Projects section also but didn't find as of now, still searching). Will be grateful to you, cheers.

node-red-contrib-telegrambot (node) - Node-RED (nodered.org)

Also check the node-red import menu an look at the examples section.

1 Like

Thank you @TotallyInformation for your reply here. Looks like I was not clear. What I was saying is it possible to create a continuous communication channel where bot knows this is a continuous chat going on from previous response from user. Let me show an example here.

User: Hi
Bot: Hello, do you want to do a virtual Linux session?

User: Yes
Bot: Then please enter your username:

User: Some random answer

now if you see when user wrongly etc entered Some random answer and if response is NOT proper from user then it should stay at that step once user sends a response rather than going to first option.

I have tried using basic Nodes eg: telegram receiver and telegram sender but if a message chain is broken then it starts from beginning. I have also tried telegram reply but its not working.

Any help and guidance will be apricated.

Thanks.
Ravinder Singh

May be this will help you

[{"id":"4df9d7e97e459ddb","type":"function","z":"452103ea51141731","name":"function 17","func":"const chat_route =[\n    {\"contains\":\"hi\", \"response\":\"I am, do you?\", \"next\":1},\n     {\"contains\":\"yes\", \"response\":\"please enter\", \"next\":2},\n     {\"contains\":\"ben\", \"response\":\"welcome\", \"next\":0}\n]\nlet index = context.get(\"next\") || 0;\nlet chat_response = chat_route[index] //|| chat_route[0];\nif(msg.payload.content.includes(chat_response.contains)){\n    msg.payload.content = chat_response.response;\n    index = chat_response.next;\n}else if(msg.payload.content === \"reset\"){\n    msg.payload.content = \"resetting\";\n    index = 0;\n}else{\n    msg.payload.content = \"responses are limited, you are not asking the correct question\";\n}\ncontext.set(\"next\", index)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":2760,"wires":[["f5fc85ba8acd22c7","9000c909847e6b0f"]]},{"id":"ca455474847ba3ce","type":"telegram receiver","z":"452103ea51141731","name":"","bot":"2199975104e81aee","saveDataDir":"","filterCommands":false,"x":210,"y":2700,"wires":[["9000c909847e6b0f","4df9d7e97e459ddb"],[]]},{"id":"f5fc85ba8acd22c7","type":"telegram sender","z":"452103ea51141731","name":"","bot":"2199975104e81aee","haserroroutput":false,"outputs":1,"x":570,"y":2760,"wires":[[]]},{"id":"9000c909847e6b0f","type":"debug","z":"452103ea51141731","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":2700,"wires":[]},{"id":"2199975104e81aee","type":"telegram bot","botname":"elcidtestbot","usernames":"","chatids":"-822824734","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

first answer must contain, hi
second, yes
third, ben
can be reset by sending reset

This is an example not a complete solution, hope it helps

2 Likes

Thanks a TON @E1cid your answer is spot on. I have few questions related to this one:

1st: Since we have only 1 JS function and only 1 telegram sender and receiver so how its happening in a single shot? I mean how conversation happening in one shot? Is telegram waiting for JS code to complete? OR JS running a loop(which I don't see written so may be internally?)?

2nd: Responses will not be same always for example Ben so can use regex(regular expressions) on its place also?

Will be grateful to you if you could guide me on same, Thank you.

Thanks,
Ravinder Singh

1 The chat path is set in in the var chat_route, You can add more fields to the route to expand on logic and how to handle response and checks etc

2 as said it is an example. You will need to add elements to chat_route to accommodate different response, as i am sure you will have more than ben in the chat.

What you are attempting is not a simple task and will need a good understanding of JS You do not have to use JS and a function node you could do similar with swtch nodes and templates and change nodes, but the logic will be similar.
Everyone here will answer questions,but you will need to be specific with your issues ongoing. Again this is not a simple task.

1 Like

Thank you @E1cid for your answer and explanation. I am a noob in JS and node-red and trying to learn it. To your question of using switch and other tools yes I am trying them but this bot is a series of questions bot, so now problem is: Lets say there are 50 details required from user to bot and many of them having yes/no answers at that time my code is getting into problem because at that time code doesn't know which yes OR no answer is referring to.

As my efforts this is the code I have tried in node-red:

    {
        "id": "f1ad7bfd5882cc1a",
        "type": "tab",
        "label": "bot-3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "585d4255e238512f",
        "type": "telegram receiver",
        "z": "f1ad7bfd5882cc1a",
        "d": true,
        "name": "",
        "bot": "c700dd99.33c84",
        "saveDataDir": "",
        "filterCommands": false,
        "x": 110,
        "y": 40,
        "wires": [
            [
                "8e4658e2b26d5bea"
            ],
            []
        ]
    },
    {
        "id": "7dcced297798c4b9",
        "type": "telegram sender",
        "z": "f1ad7bfd5882cc1a",
        "d": true,
        "name": "",
        "bot": "c700dd99.33c84",
        "haserroroutput": false,
        "outputs": 1,
        "x": 730,
        "y": 160,
        "wires": [
            [
                "3209237fd1f57fa2",
                "dbeb4a3eb521554a"
            ]
        ]
    },
    {
        "id": "8e4658e2b26d5bea",
        "type": "switch",
        "z": "f1ad7bfd5882cc1a",
        "name": "",
        "property": "payload.content",
        "propertyType": "msg",
        "rules": [
            {
                "t": "regex",
                "v": "^ *(hi|helloz|hey|howdy) *$",
                "vt": "str",
                "case": true
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 110,
        "y": 160,
        "wires": [
            [
                "9a7653c6bb6d150d"
            ]
        ]
    },
    {
        "id": "9a7653c6bb6d150d",
        "type": "function",
        "z": "f1ad7bfd5882cc1a",
        "name": "Welcome",
        "func": "var helpMsg = \"/help - shows help\\n\\n\"\ncontext.set(\"chatId\", msg.payload.chatId)\nhelpMsg += \"/foo - opens a dialog\\n\\n\\n\"\nif (msg.payload.chatId === 0001) {\n    helpMsg = \"R. Singh, Welcome sir!!!! \"\n}\nelse if (msg.payload.chatId === 11234) {\n    helpMsg = \"Ki karda hai ghussuu :) :) :) \"\n}\nelse {\n    helpMsg += \"You are welcome in Singh's bot, \" + msg.originalMessage.from.username;\n}\nhelpMsg += \"Your chat id is: \" + msg.payload.chatId;\nmsg.payload.content = helpMsg;\nmsg.payload.sentMessageId  = msg.payload.chatId;\nmsg.payload.flag = msg.payload.chatId;\nconsole.log(msg);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 160,
        "wires": [
            [
                "386a0fe52619624b",
                "7dcced297798c4b9",
                "133175beaeded35c"
            ]
        ]
    },
    {
        "id": "386a0fe52619624b",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 280,
        "y": 120,
        "wires": []
    },
    {
        "id": "bda0fc24c4a035fb",
        "type": "function",
        "z": "f1ad7bfd5882cc1a",
        "name": "new joinee??",
        "func": "/*function sleep(milliseconds) {\n    const date = Date.now();\n    let currentDate = null;\n    do {\n        currentDate = Date.now();\n    } while (currentDate - date < milliseconds);\n}\nsleep(1500);*/\nmsg.payload.type = 'message';\nmsg.payload.options = { reply_to_message_id: msg.payload.messageId };\nmsg.payload.content = \"Are you a new joinee?\";\nreturn [msg];",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 650,
        "y": 60,
        "wires": [
            [
                "7dcced297798c4b9",
                "6b03f5af1ec023a8"
            ]
        ]
    },
    {
        "id": "6b03f5af1ec023a8",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 640,
        "y": 20,
        "wires": []
    },
    {
        "id": "133175beaeded35c",
        "type": "delay",
        "z": "f1ad7bfd5882cc1a",
        "name": "",
        "pauseType": "delay",
        "timeout": "2",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 460,
        "y": 60,
        "wires": [
            [
                "bda0fc24c4a035fb"
            ]
        ]
    },
    {
        "id": "3209237fd1f57fa2",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 5",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 120,
        "wires": []
    },
    {
        "id": "25f6f3f143492fac",
        "type": "function",
        "z": "f1ad7bfd5882cc1a",
        "name": "confimed new joinee",
        "func": "msg.payload.content = \"Good to know, welcome to company\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 300,
        "wires": [
            [
                "fd4f421c85a6966e",
                "c9265e97ac2d800d"
            ]
        ]
    },
    {
        "id": "fd4f421c85a6966e",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 7",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 260,
        "wires": []
    },
    {
        "id": "bf0f82dd730d671d",
        "type": "function",
        "z": "f1ad7bfd5882cc1a",
        "name": "not a new joinee",
        "func": "msg.payload.content = \"NOTE: at this point only yes OR no answer is accepted.\\n SO please either enter yes OR no, Thank you!!\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 400,
        "wires": [
            [
                "2c73336f9070571d",
                "c9265e97ac2d800d"
            ]
        ]
    },
    {
        "id": "2c73336f9070571d",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 9",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 360,
        "wires": []
    },
    {
        "id": "dbeb4a3eb521554a",
        "type": "telegram reply",
        "z": "f1ad7bfd5882cc1a",
        "d": true,
        "name": "",
        "bot": "c700dd99.33c84",
        "x": 500,
        "y": 200,
        "wires": [
            [
                "45c8e6fa42f6707b",
                "aa1e77582d99edcf"
            ]
        ]
    },
    {
        "id": "b36440cbecbdc934",
        "type": "switch",
        "z": "f1ad7bfd5882cc1a",
        "name": "",
        "property": "payload.content",
        "propertyType": "msg",
        "rules": [
            {
                "t": "regex",
                "v": "^ *yes *$",
                "vt": "str",
                "case": true
            },
            {
                "t": "regex",
                "v": "^ *no *$",
                "vt": "str",
                "case": true
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 250,
        "y": 340,
        "wires": [
            [
                "25f6f3f143492fac",
                "bf0f82dd730d671d"
            ],
            []
        ]
    },
    {
        "id": "c9265e97ac2d800d",
        "type": "telegram sender",
        "z": "f1ad7bfd5882cc1a",
        "d": true,
        "name": "",
        "bot": "c700dd99.33c84",
        "haserroroutput": false,
        "outputs": 1,
        "x": 710,
        "y": 340,
        "wires": [
            [
                "ac4b7e52eb3c80cb"
            ]
        ]
    },
    {
        "id": "ac4b7e52eb3c80cb",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 12",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 720,
        "y": 280,
        "wires": []
    },
    {
        "id": "45c8e6fa42f6707b",
        "type": "function",
        "z": "f1ad7bfd5882cc1a",
        "name": "function 3",
        "func": "if (msg.payload.content === 'yes') {\n    console.log(\"I am in yes answer of user new joinee\");\n    return msg\n}\nelse {\n    return null;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 100,
        "y": 340,
        "wires": [
            [
                "b36440cbecbdc934",
                "5ebb90f9c18a7efd"
            ]
        ]
    },
    {
        "id": "5ebb90f9c18a7efd",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 13",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 100,
        "y": 300,
        "wires": []
    },
    {
        "id": "aa1e77582d99edcf",
        "type": "debug",
        "z": "f1ad7bfd5882cc1a",
        "name": "debug 14",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 200,
        "wires": []
    },
    {
        "id": "c700dd99.33c84",
        "type": "telegram bot",
        "botname": "Your_bot",
        "usernames": "",
        "chatids": ""
    }
]

As said you will need to create a chat route with objects that tell what type of message to expect and how to handle the check and response and next chat_route to use on a correct answer.

So for example
{"looking_for": "yes", "type": "equals", "response":"I am, do you?", "next":1}
looking_for would tell your flow that the chat_route element is now expecting a yes answer, the if conditional should use
type which in this case is equals. So now the logic knows to look for yes and check the values is equal to yes. If type was "include" and look_for was user, the logic would then check to see if your chat response ben is in the array in msg.look_for.users.
response would be the response to a correct answer, probably the next question. next tells the logic where to go for next chat_route.
(see edit example flow below)

You will need to set out your questions and answers and work a chat route you require and what checks and look_fors may be needed at each stage. You will also need to store your position in the chat route.

[edit] example of a flow no code, it will check different questions according to msg.chat_response.type

[{"id":"ca455474847ba3ce","type":"telegram receiver","z":"452103ea51141731","name":"","bot":"2199975104e81aee","saveDataDir":"","filterCommands":false,"x":150,"y":2700,"wires":[["08ae816037a35ee4"],[]]},{"id":"08ae816037a35ee4","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"chat_route","pt":"msg","to":"[{\"type\":\"contains\",\"look_for\":\"hi\",\"response\":\"I am, do you?\",\"next\":1},{\"type\":\"equals\",\"look_for\":\"yes\",\"response\":\"please enter\",\"next\":2},{\"type\":\"includes\",\"look_for\":\"users\",\"response\":\"welcome\",\"next\":0}]","tot":"json"},{"t":"set","p":"index","pt":"msg","to":"index","tot":"flow"},{"t":"set","p":"look_for.users","pt":"msg","to":"[\"ben\",\"tom\"]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":2840,"wires":[["e0ae55fbcc572e87"]]},{"id":"e0ae55fbcc572e87","type":"switch","z":"452103ea51141731","name":"","property":"index","propertyType":"msg","rules":[{"t":"istype","v":"undefined","vt":"undefined"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":2840,"wires":[["5d2087e81fb969dd"],["27e88685c06a2156"]]},{"id":"5d2087e81fb969dd","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"index","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2820,"wires":[["27e88685c06a2156"]]},{"id":"27e88685c06a2156","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"chat_response","pt":"msg","to":"chat_route[msg.index]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":2860,"wires":[["73244018130c8899"]]},{"id":"73244018130c8899","type":"switch","z":"452103ea51141731","name":"","property":"chat_response.type","propertyType":"msg","rules":[{"t":"eq","v":"contains","vt":"str"},{"t":"eq","v":"equals","vt":"str"},{"t":"eq","v":"includes","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":170,"y":2920,"wires":[["40a6011057d887c1"],["097e2fd202b1caa8"],["4fbea9d0c7f46442"]]},{"id":"40a6011057d887c1","type":"switch","z":"452103ea51141731","name":"","property":"payload.content","propertyType":"msg","rules":[{"t":"cont","v":"chat_response.look_for","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":370,"y":2940,"wires":[["912b6e2b8f7e9c78"],["9e97ca28c26d95d7"]]},{"id":"097e2fd202b1caa8","type":"switch","z":"452103ea51141731","name":"","property":"payload.content","propertyType":"msg","rules":[{"t":"eq","v":"chat_response.look_for","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":370,"y":3000,"wires":[["8b65270e871b1e11"],["9e97ca28c26d95d7"]]},{"id":"4fbea9d0c7f46442","type":"switch","z":"452103ea51141731","name":"","property":"look_for[msg.chat_response.look_for]","propertyType":"msg","rules":[{"t":"cont","v":"payload.content","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":370,"y":3080,"wires":[["84836bd369f4fcd9"],["9e97ca28c26d95d7"]]},{"id":"912b6e2b8f7e9c78","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"chat_response.response","tot":"msg"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":2940,"wires":[["f5fc85ba8acd22c7"]]},{"id":"9e97ca28c26d95d7","type":"switch","z":"452103ea51141731","name":"","property":"payload.content","propertyType":"msg","rules":[{"t":"eq","v":"resst","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":510,"y":3140,"wires":[["3696f1206045b1a9"],["82cdf0ecc03a63e6"]]},{"id":"8b65270e871b1e11","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"chat_response.response","tot":"msg"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":3000,"wires":[["f5fc85ba8acd22c7"]]},{"id":"84836bd369f4fcd9","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"$$.chat_response.response & \" \" & $$.payload.content","tot":"jsonata"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":3060,"wires":[["f5fc85ba8acd22c7"]]},{"id":"f5fc85ba8acd22c7","type":"telegram sender","z":"452103ea51141731","name":"","bot":"2199975104e81aee","haserroroutput":false,"outputs":1,"x":810,"y":2880,"wires":[[]]},{"id":"82cdf0ecc03a63e6","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"no programed response please answer question","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":3160,"wires":[["f5fc85ba8acd22c7"]]},{"id":"3696f1206045b1a9","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.conyent","pt":"msg","to":"resetting","tot":"str"},{"t":"set","p":"index","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":3120,"wires":[["f5fc85ba8acd22c7"]]},{"id":"2199975104e81aee","type":"telegram bot","botname":"elcidtestbot","usernames":"","chatids":"-822824734","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

1st must contain.
2nd must equal.
3rd must be in msg.look_for.users array.

2 Likes

Sure, thanks a TON for nice explanation. As mentioned before could you please explain me how only 1 JS function is able to do it all within single telegram node? If you see my code I had to use multiple. If you could explain that part I would be grateful.

Basically what is my understanding here is: one message from user to one message to telegram but within 1 JS function how its getting response and sending it at the same time with only 1 node of telegram.

Thank you,
Ravinder Singh

Every time the function is called it uses the stored context var to know where it is in the chat_route, if a correct answer is given the context var is given a new index so it know where to look in chat_route for next responses etc.

Here is my original function done with switch and change nodes, no code at all.
add debugs to all nodes, set to complete msg object, that way you can see the logic as it runs.

[{"id":"ca455474847ba3ce","type":"telegram receiver","z":"452103ea51141731","name":"","bot":"2199975104e81aee","saveDataDir":"","filterCommands":false,"x":150,"y":2700,"wires":[["08ae816037a35ee4"],[]]},{"id":"08ae816037a35ee4","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"chat_route","pt":"msg","to":"[{\"contains\":\"hi\",\"response\":\"I am, do you?\",\"next\":1},{\"contains\":\"yes\",\"response\":\"please enter\",\"next\":2},{\"contains\":\"ben\",\"response\":\"welcome\",\"next\":0}]","tot":"json"},{"t":"set","p":"index","pt":"msg","to":"index","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":2840,"wires":[["e0ae55fbcc572e87"]]},{"id":"e0ae55fbcc572e87","type":"switch","z":"452103ea51141731","name":"","property":"index","propertyType":"msg","rules":[{"t":"istype","v":"undefined","vt":"undefined"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":2840,"wires":[["5d2087e81fb969dd"],["27e88685c06a2156"]]},{"id":"5d2087e81fb969dd","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"index","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2820,"wires":[["27e88685c06a2156"]]},{"id":"27e88685c06a2156","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"chat_response","pt":"msg","to":"chat_route[msg.index]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":2860,"wires":[["40a6011057d887c1"]]},{"id":"40a6011057d887c1","type":"switch","z":"452103ea51141731","name":"","property":"payload.content","propertyType":"msg","rules":[{"t":"cont","v":"chat_response.contains","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":370,"y":2940,"wires":[["912b6e2b8f7e9c78"],["9e97ca28c26d95d7"]]},{"id":"912b6e2b8f7e9c78","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"chat_response.response","tot":"msg"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":2940,"wires":[["f5fc85ba8acd22c7"]]},{"id":"9e97ca28c26d95d7","type":"switch","z":"452103ea51141731","name":"","property":"payload.content","propertyType":"msg","rules":[{"t":"eq","v":"resst","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":370,"y":3000,"wires":[["3696f1206045b1a9"],["82cdf0ecc03a63e6"]]},{"id":"f5fc85ba8acd22c7","type":"telegram sender","z":"452103ea51141731","name":"","bot":"2199975104e81aee","haserroroutput":false,"outputs":1,"x":810,"y":2880,"wires":[[]]},{"id":"82cdf0ecc03a63e6","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"no programed response please answer question","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":3020,"wires":[["f5fc85ba8acd22c7"]]},{"id":"3696f1206045b1a9","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload.conyent","pt":"msg","to":"resetting","tot":"str"},{"t":"set","p":"index","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":2980,"wires":[["f5fc85ba8acd22c7"]]},{"id":"2199975104e81aee","type":"telegram bot","botname":"elcidtestbot","usernames":"","chatids":"-822824734","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]
1 Like

You are AWESOME. Since my requirement is big and your answer is very much sufficient to my initial question I have accepted it, its a GREAT guidance. Thanks a TON, THANK YOU.

Though I am still trying to understand how you did it in only node-red :slight_smile: though you explained its Logic from JS side but from implementation side still not understood it fully, will try to understand and get back here.

A side note question(s): How could I learn more on which object accepts OR sends which payload(or form of payload, I tried searching in documentation but didn't get much), thanks.

The sidebar help text for each node lists the main properties

Or there is the telegram bot api, which lists all methods and properties of methods https://core.telegram.org/bots/api#message

[edit]
And here is a flow that will handle the different answers according to chat_route.type

[{"id":"ca455474847ba3ce","type":"telegram receiver","z":"452103ea51141731","name":"","bot":"2199975104e81aee","saveDataDir":"","filterCommands":false,"x":210,"y":2700,"wires":[["08ae816037a35ee4"],[]]},{"id":"08ae816037a35ee4","type":"change","z":"452103ea51141731","name":"set chat and users and index","rules":[{"t":"set","p":"chat_route","pt":"msg","to":"[{\"type\":\"contains\",\"look_for\":\"hi\",\"response\":\"I am fine , do you want to chat?\",\"hint\":\"did you say hi\",\"next\":1},{\"type\":\"equals\",\"look_for\":\"yes\",\"response\":\"please enter name\",\"hint\":\"please\",\"next\":2},{\"type\":\"includes\",\"look_for\":\"users\",\"response\":\"welcome\",\"hint\":\"charlie said 'do not talk to strangers'\",\"next\":0}]","tot":"json"},{"t":"set","p":"index","pt":"msg","to":"index","tot":"flow"},{"t":"set","p":"look_for.users","pt":"msg","to":"[\"ben\",\"tom\"]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":2840,"wires":[["e0ae55fbcc572e87"]]},{"id":"e0ae55fbcc572e87","type":"switch","z":"452103ea51141731","name":"IS index","property":"index","propertyType":"msg","rules":[{"t":"istype","v":"undefined","vt":"undefined"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":380,"y":2840,"wires":[["5d2087e81fb969dd"],["27e88685c06a2156"]]},{"id":"5d2087e81fb969dd","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"index","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2820,"wires":[["27e88685c06a2156"]]},{"id":"27e88685c06a2156","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"chat_response","pt":"msg","to":"chat_route[msg.index]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":2860,"wires":[["73244018130c8899"]]},{"id":"73244018130c8899","type":"switch","z":"452103ea51141731","name":"type","property":"chat_response.type","propertyType":"msg","rules":[{"t":"eq","v":"contains","vt":"str"},{"t":"eq","v":"equals","vt":"str"},{"t":"eq","v":"includes","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":230,"y":2920,"wires":[["40a6011057d887c1"],["097e2fd202b1caa8"],["4fbea9d0c7f46442"]]},{"id":"40a6011057d887c1","type":"switch","z":"452103ea51141731","name":"IS correct","property":"payload.content","propertyType":"msg","rules":[{"t":"cont","v":"chat_response.look_for","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":440,"y":2940,"wires":[["912b6e2b8f7e9c78"],["9e97ca28c26d95d7"]]},{"id":"097e2fd202b1caa8","type":"switch","z":"452103ea51141731","name":"IS correct","property":"payload.content","propertyType":"msg","rules":[{"t":"eq","v":"chat_response.look_for","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":440,"y":3000,"wires":[["8b65270e871b1e11"],["9e97ca28c26d95d7"]]},{"id":"4fbea9d0c7f46442","type":"switch","z":"452103ea51141731","name":"IS correct","property":"look_for[msg.chat_response.look_for]","propertyType":"msg","rules":[{"t":"cont","v":"payload.content","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":440,"y":3060,"wires":[["84836bd369f4fcd9"],["9e97ca28c26d95d7"]]},{"id":"912b6e2b8f7e9c78","type":"change","z":"452103ea51141731","name":"SET next and response","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"chat_response.response","tot":"msg"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":2940,"wires":[["f5fc85ba8acd22c7"]]},{"id":"9e97ca28c26d95d7","type":"switch","z":"452103ea51141731","name":"reset or hint","property":"payload.content","propertyType":"msg","rules":[{"t":"eq","v":"resst","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":510,"y":3140,"wires":[["3696f1206045b1a9"],["82cdf0ecc03a63e6"]]},{"id":"8b65270e871b1e11","type":"change","z":"452103ea51141731","name":"SET next and response","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"chat_response.response","tot":"msg"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":3000,"wires":[["f5fc85ba8acd22c7"]]},{"id":"84836bd369f4fcd9","type":"change","z":"452103ea51141731","name":"SET next and response","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"$$.chat_response.response & \" \" & $$.payload.content","tot":"jsonata"},{"t":"set","p":"index","pt":"flow","to":"chat_response.next","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":3060,"wires":[["f5fc85ba8acd22c7"]]},{"id":"f5fc85ba8acd22c7","type":"telegram sender","z":"452103ea51141731","name":"","bot":"2199975104e81aee","haserroroutput":false,"outputs":1,"x":870,"y":2880,"wires":[[]]},{"id":"82cdf0ecc03a63e6","type":"change","z":"452103ea51141731","name":"SET hint","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"chat_response.hint","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":3160,"wires":[["f5fc85ba8acd22c7"]]},{"id":"3696f1206045b1a9","type":"change","z":"452103ea51141731","name":"RESET index","rules":[{"t":"set","p":"payload.conyent","pt":"msg","to":"resetting","tot":"str"},{"t":"set","p":"index","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":3120,"wires":[["f5fc85ba8acd22c7"]]},{"id":"2199975104e81aee","type":"telegram bot","botname":"elcidtestbot","usernames":"","chatids":"-822824734","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

Added hints for each response.

first input must contain hi.
second input must equal yes
third input must be in the msg.look_for.users array.

1 Like

With this kind of interaction, you might want to take a look at another node that uses Rivescript.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.