[Solved] Not able to send Linux command's output to Telegram

Hello All,

Greetings!!

I am newbie in Node-Red(BTW GREAT tech and would like to learn it more).
I created flow today whose json is as follows.

[{"id":"e8fec1f1.d46c7","type":"tab","label":"/help function in bot.","disabled":false,"info":""},{"id":"fcb32d51.06df8","type":"telegram receiver","z":"e8fec1f1.d46c7","d":true,"name":"/help","bot":"db43ddd0.9b459","saveDataDir":"","x":90,"y":100,"wires":[["d100118a.caee5"],[]]},{"id":"d100118a.caee5","type":"function","z":"e8fec1f1.d46c7","d":true,"name":"help funct","func":"//comments here testing again...\nvar helpMsg = \"/help - shows help\\n\"\nhelpMsg += \"/foo - opens a dialog\\n\\n\\n\"\nif (msg.payload.chatId === 760176267){\n    helpMsg = \"R. Singh, Welcome sir!!!! \"\n}\nelse{\nhelpMsg += \"You are welcome, \"+ msg.originalMessage.from.username;\n}\nhelpMsg += \"Your chat id is: \" + msg.payload.chatId;\nmsg.payload.content = helpMsg\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":40,"wires":[["d15c5bfa.0d8738"]]},{"id":"d15c5bfa.0d8738","type":"telegram sender","z":"e8fec1f1.d46c7","d":true,"name":"response from BOT","bot":"db43ddd0.9b459","x":530,"y":40,"wires":[["d38b5da7.daa43"]]},{"id":"d38b5da7.daa43","type":"debug","z":"e8fec1f1.d46c7","d":true,"name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":40,"wires":[]},{"id":"7a930af1.0d88d4","type":"telegram receiver","z":"e8fec1f1.d46c7","name":"/help","bot":"db43ddd0.9b459","saveDataDir":"","x":50,"y":240,"wires":[["66c5029a.4dc8cc"],[]]},{"id":"4b33c9c5.0c6038","type":"debug","z":"e8fec1f1.d46c7","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":400,"wires":[]},{"id":"612c98a1.b75a08","type":"telegram sender","z":"e8fec1f1.d46c7","name":"","bot":"db43ddd0.9b459","x":690,"y":320,"wires":[["4b33c9c5.0c6038"]]},{"id":"66c5029a.4dc8cc","type":"function","z":"e8fec1f1.d46c7","name":"help funct1","func":"var helpMsg = \"/help - shows help\\n\"\nhelpMsg += \"/foo - opens a dialog\\n\\n\\n\"\nif (msg.payload.chatId === 760176267){\n    helpMsg = \"R. Singh, Welcome sir!!!! \"\n}\nelse{\nhelpMsg += \"You are welcome, \"+ msg.originalMessage.from.username;\n}\nhelpMsg += \"Your chat id is: \" + msg.payload.chatId;\nmsg.payload.content = helpMsg\n\nreturn msg;","outputs":1,"noerr":0,"x":190,"y":320,"wires":[["a094494f.5b0ce8","612c98a1.b75a08","4f037622.b1a0c8"]]},{"id":"a094494f.5b0ce8","type":"debug","z":"e8fec1f1.d46c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":340,"y":400,"wires":[]},{"id":"4f037622.b1a0c8","type":"exec","z":"e8fec1f1.d46c7","command":"df / | awk 'FNR==2'","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"*nix  command","x":360,"y":220,"wires":[["dc04d410.b91be8"],[],[]]},{"id":"9b4d5856.171c58","type":"debug","z":"e8fec1f1.d46c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":700,"y":260,"wires":[]},{"id":"dc04d410.b91be8","type":"function","z":"e8fec1f1.d46c7","name":"send output","func":"//var msg1=msg.payload;\nmsg.payload.content = msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":280,"wires":[["612c98a1.b75a08","9b4d5856.171c58"]]},{"id":"db43ddd0.9b459","type":"telegram bot","z":"","botname":"singh_is_king13","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"1000","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]

Its recognizing my chat id by first function and providing me proper response on Telegram but NOT giving any output for df command in Telegram, though it gives me df command's output(exec toolkit) on DEBUG console. I have tried to search a lot but couldn't find solution, any help is greatly appreciated here.

Thanks,
R. Singh

Which telegram node are you using ? Seems there are multiple out there.

Hello realjax,

Thank you for your reply, if you mean type of buttons, then I am using telegram receiver and telegram sender 2 functions. Error is coming in telegram sender side.

My belief is exec module is loosing the Chat id from msg.payload. Any help will be greatly appreciated since I am working on this from last 4 to 5 hours and no luck yet, it prints the message correctly(unix command output) in DEBUG window but don't send it to Telegram.

Thanks,
R. Singh

The question which telegram nodes means what node did you install? Probably node-red-contrib-something.

Thank you for your reply Colin. Oh ok got it; I am using "node-red-contrib-telegrambot".
Kindly do let me know in case of any more queries here, will be grateful for any guidance here.

You need to save msg.payload.chatid to a flow variable because the exec node is creating a new msg.payload. Then before using the telegram out node you need to build
msg.payload,content
msg.payload.type
msg.payload.chatid

1 Like

Thank you for your reply.

I used following to create variables(custom ones) to save values before exec module:

flow.set('ChatID',msg.payload.chatId);

flow.set('content',msg.payload.content);

flow.set('type',msg.payload.type)


return msg;

Then after exec I created one function and used this to again assign them values:

msg.payload.chatId=global.get('ChatID');
msg.payload.content=global.get('content');
msg.payload.type=global.get('type');

But still I am getting same error, I know its a minor issue, trust me I am working on it now more than 7 hours or so :frowning:

Any help/guidance will be appreciated.

NOTE: Error again for reference is:

3/10/2020, 11:25:24 PMnode: 612c98a1.b75a08
msg : string[27]
"msg.payload.chatId is empty"

Thanks,
R.Singh

can you provide your current flow?

Hello zenofmud,

Following is the flow details in form of js (note: This is a bit different from 1st post of mine, since I was keep doing experiments to fix the issue):

[{"id":"e8fec1f1.d46c7","type":"tab","label":"/help function in bot.","disabled":false,"info":""},{"id":"fcb32d51.06df8","type":"telegram receiver","z":"e8fec1f1.d46c7","d":true,"name":"/help","bot":"db43ddd0.9b459","saveDataDir":"","x":70,"y":20,"wires":[["d100118a.caee5"],[]]},{"id":"d100118a.caee5","type":"function","z":"e8fec1f1.d46c7","d":true,"name":"help funct","func":"//comments here testing again...\nvar helpMsg = \"/help - shows help\\n\"\nhelpMsg += \"/foo - opens a dialog\\n\\n\\n\"\nif (msg.payload.chatId === 760176267){\n    helpMsg = \"R. Singh, Welcome sir!!!! \"\n}\nelse{\nhelpMsg += \"You are welcome, \"+ msg.originalMessage.from.username;\n}\nhelpMsg += \"Your chat id is: \" + msg.payload.chatId;\nmsg.payload.content = helpMsg\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":20,"wires":[["d15c5bfa.0d8738"]]},{"id":"d15c5bfa.0d8738","type":"telegram sender","z":"e8fec1f1.d46c7","d":true,"name":"response from BOT","bot":"db43ddd0.9b459","x":510,"y":20,"wires":[["d38b5da7.daa43"]]},{"id":"d38b5da7.daa43","type":"debug","z":"e8fec1f1.d46c7","d":true,"name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":710,"y":20,"wires":[]},{"id":"7a930af1.0d88d4","type":"telegram receiver","z":"e8fec1f1.d46c7","name":"/help","bot":"db43ddd0.9b459","saveDataDir":"","x":50,"y":240,"wires":[["9faef31b.5c1"],[]]},{"id":"4b33c9c5.0c6038","type":"debug","z":"e8fec1f1.d46c7","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":790,"y":120,"wires":[]},{"id":"612c98a1.b75a08","type":"telegram sender","z":"e8fec1f1.d46c7","name":"","bot":"db43ddd0.9b459","x":670,"y":180,"wires":[["4b33c9c5.0c6038"]]},{"id":"66c5029a.4dc8cc","type":"function","z":"e8fec1f1.d46c7","name":"help","func":"var helpMsg = \"/help - shows help\\n\"\ncontext.set(\"chatId\",msg.payload.chatId)\nhelpMsg += \"/foo - opens a dialog\\n\\n\\n\"\nif (msg.payload.chatId === 760176267){\n    helpMsg = \"R. Singh, Welcome sir!!!! \"\n}\nelse{\nhelpMsg += \"You are welcome, \"+ msg.originalMessage.from.username;\n}\nhelpMsg += \"Your chat id is: \" + msg.payload.chatId;\nmsg.payload.content = helpMsg\n\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":180,"wires":[["a094494f.5b0ce8","612c98a1.b75a08"]]},{"id":"a094494f.5b0ce8","type":"debug","z":"e8fec1f1.d46c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":100,"wires":[]},{"id":"fbb59e9d.943ff","type":"debug","z":"e8fec1f1.d46c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":460,"wires":[]},{"id":"9faef31b.5c1","type":"switch","z":"e8fec1f1.d46c7","name":"check","property":"payload.content","propertyType":"msg","rules":[{"t":"eq","v":"help","vt":"str"},{"t":"eq","v":"df","vt":"str"},{"t":"eq","v":"df","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":190,"y":260,"wires":[["66c5029a.4dc8cc"],["a4d5e323.71f43"],["9a404f67.cc6b3"]]},{"id":"a4d5e323.71f43","type":"function","z":"e8fec1f1.d46c7","name":"df","func":"flow.set('ChatID',msg.payload.chatId);\nflow.set('content',msg.payload.content);\nflow.set('type',msg.payload.type)\n\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":260,"wires":[["733a5a27.28f004"]]},{"id":"9a404f67.cc6b3","type":"exec","z":"e8fec1f1.d46c7","command":"df -h / | awk 'FNR==2'","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"df","x":510,"y":400,"wires":[["fbb59e9d.943ff","8286b21e.de059"],[],[]]},{"id":"733a5a27.28f004","type":"debug","z":"e8fec1f1.d46c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":410,"y":300,"wires":[]},{"id":"8286b21e.de059","type":"function","z":"e8fec1f1.d46c7","name":"","func":"msg.payload.chatId=global.get('ChatID');\nmsg.payload.content=global.get('content');\nmsg.payload.type=global.get('type');\n\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":280,"wires":[["612c98a1.b75a08","c5223391.62c21"]]},{"id":"c5223391.62c21","type":"debug","z":"e8fec1f1.d46c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":720,"y":380,"wires":[]},{"id":"db43ddd0.9b459","type":"telegram bot","z":"","botname":"singh_is_king13","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"1000","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]

Thanks,
R. Singh

Where are you storing your chat ID? (show the code)

Where do you get the chat ID? (show the code)

Hello zenofmud,

I pasted those variable initialization in my post#7, here are the details.

flow.set('ChatID',msg.payload.chatId);
flow.set('content',msg.payload.content);
flow.set('type',msg.payload.type);

msg.payload.chatId=global.get('ChatID');
msg.payload.content=global.get('content');
msg.payload.type=global.get('type');

First 3 lines have been put in function before executing exec module AND 2nd 3 statements have been put in another function before sending final data.

Kindly do let me know in case of any more information required.

Thanks,
R. Singh

now look at your code and tell me in words exactly where you store the chat id and where you get it from.

Hello zenofmud,

IMHO:

flow.set('ChatID',msg.payload.chatId);

is setting msg.payload.chatId to variable ChatID and likewise all others are doing the same IMHO. Isn't it the case?

Also code:

msg.payload.chatId=global.get('ChatID');

Is setting msg.paylaod.chatId from variable ChatID.

Thanks,
R. Singh

flow.set('ChatID',msg.payload.chatId);

this is setting a flow variable called 'ChatID' to what is in msg.payload.chatId

msg.payload.chatId=global.get('ChatID');

this is setting msg.payload.chatId to what is in the global variable 'ChatID'

can you see the problem?

Thank you for your response here zenofmud, so you mean I need to use flow.get method here?

if you store something in location A can you expect to retreive it from location 'B'?
You should read the documentation here: https://nodered.org/docs/user-guide/context and here: https://nodered.org/docs/user-guide/writing-functions#storing-data

2 Likes

Thank you zenofmud for your reply, I will check on it and post you in few hours(once I reach home) with my updated code.

Thanks,
R. Singh

Hey Ravinder,

Good so see you here.

In general, it's often a good idea to build a new message object with the new or derived params and return that new message object instead of modifying the input message object.

For example,

Early in your flow, you might need to store the chatId.

This might be something like:

flow.set('ChatID',msg.payload.chatId);

Later in the same flow, you might need to build a new message object in Telegram. Telegram requires three params to send a message. You must have at least these three properties in a Telegram outgoing message:

  • msg.payload.content
  • message.payload.type
  • mesage.payload.chatId

So later in your flow, you might do this:

var message = 'my brilliant message';    // message is required
var chat = flow.get('ChatID');           // chatid type is required
var type = 'message';                    // message type is required
newMsg = {payload:{chatId:chat,type:type,content:message}};
return newMsg;

This is the generalized construct I use.

Hope this helps.

2 Likes

Hello All,

Thanks for your guidance and help here, I was able to do it last late night IST, I was able to do it with flow.set and flow.get variable. Where I had get values of chatid, content and type in 1st function before exec and then retrived them back in function after exec successfully. Right now I can't post code since I not on my machine. Will post codes in few hours.

@unixneo Thanks unixneo for letting know another of doing it too.

Appreciate help of zenofmud, Colin, realjax and unixneo here.

EDIT: As promised previously in my reply here are my codes.

function 1st(before using exec):

//console.log("Chat id before:"+msg.payload.chatId);
var ChatID=flow.get('ChatID') // || 0;
//console.log("TYPE test value:"+msg.payload.type)
var TyPE=flow.get('TyPE')
var ConTent=flow.get('ConTent')
flow.set('ChatID',msg.payload.chatId);
flow.set('TyPE',msg.payload.type);
flow.set('ConTent',msg.payload.content);
console.log("Chatid value BEFORE:"+ChatID);
console.log("Type value BEFORE:"+TyPE);
console.log("Content value BEFORE:"+ConTent);
return msg;

2nd function(after exec to retrieve values):

var ChatID=flow.get('ChatID') || 0;
var TyPE=flow.get('TyPE') || 0;
var ConTent=flow.get('ConTent') || 0;
//console.log("AFTERRRRRR id value is:"+ ChatID)
var temp=msg.payload;
msg.payload = {};
msg.payload.chatId=ChatID;
msg.payload.type=TyPE;
msg.payload.content=temp;

console.log("Disk space AFTER chat id here:"+msg.payload.chatId);
console.log("Disk space AFTER type here:"+msg.payload.type);
console.log("Disk space AFTER content of message here:"+msg.payload.content);
return msg;

I am adding it so that it could help future users, cheers and happy learning :slight_smile:

Thanks,
R. Singh

2 Likes

Will post codes in few hours.

@zenofmud - re-opening this as the few hours have not yet passed...