Telegram, NodeRed, and an alternative to SMS messages

I used a Twilio test account to send SMS messages triggered by the front door bell, but of course that is about to expire, probably because I've flogged it to death with a piss-taking number of SMSs owing to connecting one of the security cameras at the rear of the house to it...

Anyway, now I need a sensible free alternative - who wants to pay £0.04 per SMS, or £0.08 per doorbell ring? If you knew the number of packages we have delivered, you'd know this is infeasible! Besides, all the services have different APIs, and I couldn't be arsed to sing up for another trial somewhere else. What to do?

Here's an idea - can I use Instant Messaging, like WhatsApp? WhatsApp announced that they aren't going to have an API, so that's out. I played around with iMessage, but that's pretty closed too. There are smartphone apps available that will receive a message and use the phone's text connection to send an SMS, but they also require using an API to contact the phone app. Boo...

Then I discover Telegram has an API, and is free, and available for smartphones (!), and there are NodeRed nodes available that use the API. In fact, it was exploring the NodeRed palette that pointed me to Telegram. So let's have a play.

NodeRed has two appropriate-seeming nodes:
  • Telegram Sender
  • Telegram Receiver
Both of these use the Bot interface referenced above. However, what does a Bot actually do?? Where is what it does actually done??? It took a bit of digging to find something at all helpful - I guess people can't be arsed to write documentation, but this item proved helpful. I ended up with the following flow:
Example Telegram Flow
First I had to add a Telegram Bot in Telegram itself. This involved

  • Loading Telegram onto my Mac and iPhone
  • Sending /start and /newbot to the BotMaster bot (!), then configuring my own bot
  • Getting the API Token from the resulting output
  • Configuring a Telegram Sender and Receiver in NodeRed
  • Configuring the API Token into the Telegram Receiver and Sender  nodes (actually just once - it's a shared resource)
Then nothing seemed to work... I couldn't get Sender to send, despite finding what seemed to be a reasonable description of the required msg.payload contents. You can see two Inject/Function node pairs that feed into Sender - no joy. 

So, following the simple Echo example from here [uh, I think was meant to be a link but I didn't configure it], I configured a Telegram Message node (Bot Reply Message) which just adds some fixed text to msg.payload, and forwards it to Telegram Sender. Then  I sent a message to my Bot from Telegram on my phone. Hurray!! It works... Or at least, my phone Telegram app showed a message had been sent to the Bot, and come back to the Receiver, and the new message had been triggered and send out as though it were from the Bot to my phone. Brill. Ish. 

Some digging around the message contents, revealed by the Debug nodes scattered around, revealed that I needed
  • A valid chatId, so that Telegram could pick the right chat; this was effectively created by my first message to the Bot from my phone
  • an originalMessage object that contained "transport:'telegram'"
This allowed me to send an unprompted message from my Telegram Sender to the Bot, and then have that be sent by the Bot to my iPhone, in fact, to all the Telegram application instances to which I was logged in. Fabbo! 

Update [2021-02-03]:
My wife changed her phone and didn't clone it, so the Telegram account info was lost, and her specific chat for receiving Bot messages. Now I had to get a new chat going, which was easy - message to @myBot - but getting the chatId a bit harder. I ended up configuring a Telegram Message Receiver on the bot's Auth Token, and displaying the message contents on the Node Red debug, then editing my "sendWifeMessage" node to use the new chatId. Phew.



Here's some code:

Telegram Message Chat

msg.payload.chatId = 999999999;
msg.payload.type = 'message';
msg.payload.content = 'Front Door Bell ' + Date().toString();
msg.originalMessage = {transport:"telegram"};
return msg;

As you can see, this has almost no fields, only chatId (the original value from my first manual attempt), a 'type' and 'content', along with the 'originalMessage.transport' field. Works pretty well. Now I have to see if I can 
  • Get my wife to install Telegram
  • Start a chat between all of us
  • Send stuff to that chat!
Oh yes, the Input Link node is used by the Friedland Output node on the bell ring detection flow. Simples...


Comments

Post a Comment

Popular posts from this blog

Replacing Coffee Machine Pump (Dualit Espressivo)

Multiple SHT30 sensors on a single I2C bus with Sonoff-Tasmota

NodeMCU + Tasmota code + SHT30