User-input using the Telegram node

Here's a very simple flow to send a series of prompts, via the Telegram node, to a user's mobile phone where each input is captured. This example asks for a user's ID followed by First Name and Last Name. When all three responses have been received the information is sent to the next part of the flow - probably a chart, an email or a database. Additional prompts can be introduced by editing the 'process input' and 'Send appropriate request' function nodes. Note: This flow uses the 'node-red-contrib-telegrambot' node.

[{"id":"4c1c22d1750b13fa","type":"tab","label":"Telegram form ","disabled":false,"info":"","env":[]},{"id":"4f96e235566061f3","type":"inject","z":"4c1c22d1750b13fa","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"1","payloadType":"num","x":130,"y":80,"wires":[["1ab91987066a8b16"]]},{"id":"1ab91987066a8b16","type":"function","z":"4c1c22d1750b13fa","name":"Users array","func":"// Array for users\n// New users will be dynamically added to the Javascript object \n\nlet users = {\n    1:{stage:0, id:0, first_name:'unknown', last_name:'unknown'}\n}\n\nreturn null;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":80,"wires":[[]]},{"id":"737d730ac6c5a7ce","type":"debug","z":"4c1c22d1750b13fa","name":"debug all","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":280,"y":140,"wires":[]},{"id":"470690d8c93b893d","type":"catch","z":"4c1c22d1750b13fa","name":"","scope":null,"uncaught":false,"x":120,"y":140,"wires":[["737d730ac6c5a7ce"]]},{"id":"a0f9ba1ef57fed59","type":"change","z":"4c1c22d1750b13fa","name":"Save chatID","rules":[{"t":"set","p":"chatID","pt":"flow","to":"payload.chatId","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":260,"wires":[["546a965f0521ed9d"]]},{"id":"5f82472c7b303d52","type":"comment","z":"4c1c22d1750b13fa","name":"Telegram >>> Input to Bot","info":"","x":170,"y":220,"wires":[]},{"id":"66c6f8f3fce6eb3d","type":"comment","z":"4c1c22d1750b13fa","name":"Output from Bot >>> Telegram","info":"","x":860,"y":220,"wires":[]},{"id":"546a965f0521ed9d","type":"switch","z":"4c1c22d1750b13fa","name":"","property":"payload.content","propertyType":"msg","rules":[{"t":"cont","v":"/Start","vt":"str"},{"t":"cont","v":"/start","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":290,"y":320,"wires":[["ef3da6b036a025b8"],["ef3da6b036a025b8"],["c1ab75d37c520995"]]},{"id":"ef3da6b036a025b8","type":"function","z":"4c1c22d1750b13fa","name":"Welcome screen","func":"let chatID = flow.get(\"chatID\");\nlet users = flow.get(\"users\") ||  {1: {\"stage\": 1, \"id\": 0, \"first_name\": \"unknown\", \"last_name\": \"unknown\"}};\n\n\n// if chatId not in users object then add an entry for it\nif (users[chatID] === undefined) {\n    users[chatID] = {}\n}\n\nusers[chatID] = {\"stage\": 1,\n                 \"id\":0,\n                 \"first_name\": \"\", \n                 \"last_name\": \"\"\n};\n\nflow.set(\"users\", users);\n\n// Assemble the content for the Welcome screen\nlet my_message = \"** Simple user registration sequence **\\n\";\n\nmy_message = my_message + \"\\nYou can use /start to...\";\nmy_message = my_message + \"\\nStart or re-start your registration.\";\n\n// Format the message to send to the user via Telegram \nmsg.payload = {\n        chatId: chatID, \n        type:\"message\",\n        content:my_message\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":300,"wires":[["e6c00e8803aec245","10082bbd6a518d47"]]},{"id":"e6c00e8803aec245","type":"delay","z":"4c1c22d1750b13fa","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":760,"y":320,"wires":[["c8866469eb24fab8"]]},{"id":"c8866469eb24fab8","type":"function","z":"4c1c22d1750b13fa","name":"Send appropriate request","func":"let chatID = flow.get(\"chatID\");\nlet users  = flow.get(\"users\");\nlet stage  = users[chatID].stage;\n\nif (stage == 1) {\n    let message = 'Enter your ID';\n    msg.payload = {chatId : chatID, type : 'message', content : message};\n    msg.payload.options = {disable_web_page_preview : true, parse_mode : \"Markdown\"};\n    return msg;\n}\nelse if (stage == 2) {\n    let message = 'Enter your First Name';\n    msg.payload = {chatId : chatID, type : 'message', content : message};\n    msg.payload.options = {disable_web_page_preview : true, parse_mode : \"Markdown\"};\n    return msg;\n}\nelse if (stage == 3) {\n    let message = 'Enter your Last Name';\n    msg.payload = {chatId : chatID, type : 'message', content : message};\n    msg.payload.options = {disable_web_page_preview : true, parse_mode : \"Markdown\"};\n    return msg;\n}\nelse if (stage == 0) {\n    let message = 'Thank you - input is complete';\n    msg.payload = {chatId : chatID, type : 'message', content : message};\n    msg.payload.options = {disable_web_page_preview : true, parse_mode : \"Markdown\"};\n    return msg;\n}\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":360,"wires":[["10082bbd6a518d47"]]},{"id":"c1ab75d37c520995","type":"function","z":"4c1c22d1750b13fa","name":"process input","func":"let chatID = flow.get(\"chatID\");\nlet users  = flow.get(\"users\");\nlet stage  = users[chatID].stage;\n\nlet my_content = msg.payload.content;\n\nif (stage == 1) {\n    users[chatID].id = my_content;\n    users[chatID].stage = 2;\n    flow.set(\"users\", users);\n    node.send( [msg, null] );\n}\n\nelse if (stage == 2) {\n    users[chatID].first_name = my_content;\n    users[chatID].stage = 3;\n    flow.set(\"users\", users);\n    node.send( [msg, null] );\n}\n\nelse if (stage == 3) {\n    users[chatID].last_name = my_content;\n    users[chatID].stage = 0;\n    flow.set(\"users\", users);\n    \n    msg.payload = {}; // Clear the object\n    msg.payload.chatID     = chatID;\n    msg.payload.id         = users[chatID].id\n    msg.payload.first_name = users[chatID].first_name;\n    msg.payload.last_name  = users[chatID].last_name;\n    node.send( [msg, msg] );\n}\nreturn null;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":380,"wires":[["c8866469eb24fab8"],["2999f3a667878437","c9434e1ed49387f0"]]},{"id":"2999f3a667878437","type":"delay","z":"4c1c22d1750b13fa","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":520,"y":220,"wires":[["ef3da6b036a025b8"]]},{"id":"c9434e1ed49387f0","type":"debug","z":"4c1c22d1750b13fa","name":"Send_to_next_stage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":780,"y":480,"wires":[]},{"id":"930df49cfc7838f0","type":"comment","z":"4c1c22d1750b13fa","name":"Output to next stage.... dBase","info":"","x":800,"y":440,"wires":[]},{"id":"6fa62da3ac4dde87","type":"comment","z":"4c1c22d1750b13fa","name":"User registration wil start again after 6s delay ","info":"","x":630,"y":140,"wires":[]},{"id":"743575fba88c69ef","type":"comment","z":"4c1c22d1750b13fa","name":"Delete this node to remove that functionality","info":"","x":630,"y":180,"wires":[]},{"id":"2aaf7694e2f77f2a","type":"telegram receiver","z":"4c1c22d1750b13fa","name":"User Bot-IN","bot":"06b49c8ae48d3c80","saveDataDir":"","filterCommands":false,"x":130,"y":260,"wires":[["a0f9ba1ef57fed59"],[]]},{"id":"10082bbd6a518d47","type":"telegram sender","z":"4c1c22d1750b13fa","name":"User Bot-OUT","bot":"06b49c8ae48d3c80","haserroroutput":false,"outputs":1,"x":820,"y":260,"wires":[[]]},{"id":"06b49c8ae48d3c80","type":"telegram bot","botname":"enter_bot_name","usernames":"","chatids":"","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}]

Here's an example of a set of prompts and a user's responses.

Here's what the output to the next stage looks like.
register_BB

I hope someone finds a use for this flow.

4 Likes