LoginAlert with Telegram BOT

TL;DR

First I would like to give the screenshot of my login alert here:

From this story, you can learn two things:

  1. Create a telegram BOT
  2. Implement APP with automator on mac In fact, I faced many challenges and choices in the process 😰 So, here is my story.

Create a telegram BOT

I didn’t try BOT of telegram directly from the beginning. What I only think is that I need an API, to connect between client side(my phone) and a webhook. When there is a request to the webhook, it would trigger the event on my client side.

First, I tried to use Zapier and Pushover. Zapier implements API with so much applications including pushover, I just need to choose the method to trigger it and the type of client applications, then it would do everything for me. And with pushover, it can be installed on multiple devices. I just need to pair the webhook with the token given by pushover, and all the work is done.

Although Zapier and Pushover are so convenient, both of them are so expensive for the poor student such like me… Specifically for Zapier, I need to afford for at least 20 dollars for each months! Of course, I can choose a free plan, but my pairs of connection would be refreshed for each month, this would be so bothering for me.

Then, another method come to my mind: my friend @kaibro has written a telegram BOT to alert himeself for Bug Bounty program. This should also be a good choice for me, what’s important, it’s free!!

  • Apply for BOT from @BotFather

    Find the account which is called @BotFather. After you input /start, there would be instructions for you to create your own BOT.
    After creating a BOT, we can get a webhook such like https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$user_id&text=msg.
  • Get $TOKEN
    Token would be in the conversation between you with @BotFather, make sure your token not be exposed to others.
  • Get chat_id
    At here, I want to let my BOT send message to me. To get my user id, I message with the account: @userinfobot.
  • Write down your msg
    Bot is ready, next I am going to write a script to call the webhook.

Implement APP with automator on mac

I also not tried writing the script in APP from the beginning. I spent so much time on writing loginhook but ended with finding it deprecated Click here to see the detail.

Then, I started trying another method: System Preferences > Users & Groups > Login Items. This method would run specific apps once logged in, and this is also what I want.
Here comes the automator, it can run shell script and wrap it as a application!

  • new file in automator
  • point to the path of script
  • Ctrl + s to save it as xxx.app in any folder you want
  • Then add the app to the Login Items

It works!
There are still some other choices such like Sleepwatcher and EventScripts. Unfortunately, my initial plist file for sleepwatcher is missed and I was so lazy to create it by myself. For EventScripts, I need to afford near to 4 dollars to buy it on app store, damn it…