shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
151 stars 35 forks source link

Send messages from bot without user interaction #103

Closed mjtrini closed 2 years ago

mjtrini commented 2 years ago

Hello Stefano,

Thank you again for all your support with my previous queries. I know this is not an issue but can your library allow the sending of messages automatically without the user sending a command on the telegram chat? In my case, I am controlling my gate with an esp8266. I have a sensor to tell the esp8266 if the gate is fully closed. I wish the esp8266 to send a "gate closed" message without me having to manually request a status of the gate. Can the CTBot library send messages based on the state of a digital input on the esp8266? If yes, can you provide an example of how to implement this?

Much respect and appreciation, Michael.

shurillu commented 2 years ago

Hello again Michael, this is what I will do. I'll check the transition from the gate open to the gate close. You could implement in this way:

#define GATE_SENSOR_PIN 3 // change it according to the pin where the gate sensor is connected
#define GATE_OPEN  0 // the value read form the pin GATE_SENSOR_PIN when the gate is open
bool previousGateStatus, gateStatus;
...

void stetup() {
   ...
   previousGateStatus = digitalRead(GATE_SENSOR_PIN); // store the current gate status
}

void loop() {
   ...
   gateStatus = digitalRead(GATE_SENSOR_PIN); // read the actual gate status
   if (gateStatus != previousGateStatus) {
      // the status of the gate is changed, check if it has changed from open to close
      // this mean previousGateStatus value is GATE_OPEN 
      if (previousGateStatus == GATE_OPEN ) {
         myBot.sendMessage(TELEGRAM_ID, "Gate closed");
      }
   }
   previousGateStatus= gateStatus;
   ...
}

Briefly:

  1. create two global variables (gateStatus and previousGateStatus) to store the actual status of the gate and the previous status
  2. in the setup function, initialize the previousGateStatus (reading the status of the gate, for example)
  3. in the loop function:
  4. store the current status of the gate in the gateStatus variable
  5. if it is different from previousGateStatus (this mean the state of the gate is changed from open to close or vice versa), check if the previousGateStatus is equal to GATE_OPEN.
  6. if yes (previously was open, now is closed) send a message
  7. update the previousGateStatus to gateStatus

I wrote these lines in a hurry, could be there some errors :-D

Hope this helps!

Stefano

mjtrini commented 2 years ago

Hello Stefano!

Thanks again for your invaluable assistance. I will give this snippet a shot when I return home in about 7 hours. So basically, using myBot.sendMessage(TELEGRAM_ID, "Gate closed"); anywhere in void loop() always the esp8266 CTBot to send messages without having to receive a message first?

Also, TELEGRAM_ID takes the place of msg.sender.id? I used your examples to build my sketch and they used myBot.sendMessage(msg.sender.id, "text to be sent for example");

Will be in contact soon! Keep safe my friend and greetings from the Caribbean! Michael.

shurillu commented 2 years ago

Some things you may give special attention:

So basically, using myBot.sendMessage(TELEGRAM_ID, "Gate closed"); anywhere in void loop() always the esp8266 CTBot to send messages without having to receive a message first?

As long as you have a valid Telegram ID, you can send a message to that Telegram ID, regardless receiving messages. So replace TELEGRAM_ID with your own (or use a define..).

Every Telegram account (your too!) have a special, unque ID and your bot can send messages directly to that ID. If you want to know your ID, simply use the echoBot example and add this line

myBot.sendMessage(msg.sender.id, (String)"Your ID is: " + msg.sender.id.toString());

after the line

myBot.sendMessage(msg.sender.id, msg.text);

(you can replace this line at all). Now every time you send a message to the bot, it replies with the Telegram ID of the writer (in this case, your). Another way is to use the "Get My ID" bot getmyid_bot. Find it in your mobile phone or install the telegram program on your PC. Once acquired, you can use your ID inside the bot to send messages to you.

Another thing: take care that all bots are public: in other words, every one can browse and find your bot and and use it. If you are planning to use a bot for controlling your smart home, ALWAYS check the sender ID: if the sender ID is one on a white list, then execute the command, otherwise drop the message.

Cheers

Stefano

mjtrini commented 2 years ago

Hi Stefano,

The snippet worked! Thanks greatly! I was hoping that the esp8266 can send an autonomous status message without me having to send any arbitrary message first to invoke a response from the bot (I allowed some family members to access my "smart gate" and it would be fantastic to know if the gate's state has changed when they remotely accessed it). Any chance that CTbot can send autonomous messages without first getting messages from a human user?

Much thanks, Michael.

shurillu commented 2 years ago

Hello Michael, glad to see that you solve the problem ;-) About your last question, as long as I know there are no API calls/functions to know if an user is online or not (for the bot). So an autonomously mode to send the status of the bot (or the controlled device) when a user open the chat is not possible. You can always send to one user (or more user - you have to know their Telegram ID as the previous comment) events, such as gate open/close, temperature below a threshold etc... Take in mind that Telegram (and my library do!) implement keyboards, like inline and reply keyboard. They are useful tools that enabling you to send predefined messages without writing word but simply tapping on custom buttons. Check the examples ;-) Cheers

Stefano

mjtrini commented 2 years ago

Hi my friend. Yes I do use buttons based on your examples and it makes life all that more sweeter for iot projects like my gate control. Thanking you immensely again and wishing you a fantastic week!

Great appreciation, Michael.