Sending Telegram messages via bots!



  • Telegram is a messaging platform similar to Whatsapp, but you can create bots and use an API to send messages from devices to yourself.

    I have been experimenting with using Telegram bots to send notifications and alerts instead of emails.

    I thought I'd try it out on the Onion and see what happens! You can also send commands through the bot too although I havnt ventured down that path ie. send a "/lighton" command and the omega switches a specific GPIO pin to 1 to turn a light on.

    You should be able to follow the sequence below to get started:

    1. Browse to https://telegram.org and create an account OR download the app on your iDevice or similar and create an account that way. If you have created an account on your device you can login to a web session on the telegram website to send messages via your browser also.

    2. You must get your unique CHATID for your user. Send /getid to @IDBot, it needs to be this specific command to get the answer. This will give you your CHATID which we will use later.

    3. Next create a bot! Send /helpto @BotFather, next to create a bot send /newbot. The BotFather will then ask a couple of questions, first a Name that is just a label and then the username for the bot which must end in Bot. He will send you the API token which again you must keep, although you can ask him to generate another by using the /token message. You should notice there are a load of commands you can ask of the BotFather, I mainly use the /setuserpic which you can use to change the bots avatar!

    4. Next is to install curl which is used to create the HTTP requests, in this case a POST.

    opkg update
    opkg upgrade
    opkg install curl
    
    1. Once curl is installed we can start sending messages! Copy pasta the following to setup the script file:
    cd /root
    touch telegram.sh
    chmod 775 telegram.sh
    
    1. Open the file using your favourite text editor (I enjoy VI as it is on version of linux!) Copy the below text to create a script file that is used to send a message:
    #!/bin/sh
    #
    #  This script sends whatever is piped to it as a message to the specified Telegram bot
    #
    message=$( cat )
    apiToken=<PLACE API TOKEN HERE>
    # example:
    # apiToken=123456789:AbCdEfgijk1LmPQRSTu234v5Wx-yZA67BCD
    userChatId=<PLACE CHATID HERE>
    # example:
    # userChatId=123456789
    
    sendTelegram() {
            curl -s \
            -X POST \
            https://api.telegram.org/bot$apiToken/sendMessage \
            -d text="$message" \
            -d chat_id=$userChatId
    }
    
    if  [[ -z "$message" ]]; then
            echo "Please pipe a message to me!"
    else
            sendTelegram
    fi
    
    1. Once the script is setup and you have modified it to have your CHATID and bot API token then use it!
    echo "Test: Onion Omega FTW!!" | ./telegram.sh
    

    You should get back a return of a valid message from the console and on whatever platform you have Telgram installed on!

    One problem I have found is the pipe validation. Within bash it echos a response if nothing is piped. The same bit of code doesnt seem to work for busybox. If anyone has got a better way of doing it let me know!

    Hopefully this will get some people thinking of some better ways of notifying people of events!

    Hopefully @Boken-Lin will get it added to the Wiki if it is worthy! šŸ‘



  • Hi @Sawyer-McBride, this is awesome! Thanks for sharing! I will add it to the Wiki šŸ™‚



  • This never got added to the wiki, Im sure someone would find it useful šŸ˜„



  • As this is in Markdown you could cut and paste it to your github and post the link. So for the onioneers is it much more easy to transfer to their github.



  • I have a bot with 70K subscribers on Telegram! Why am I not able to broadcast a message to all of them on time?! I mean if I go ahead and broadcast a simple message, God knows when it will be delivered to all! Can anybody guide me on this issue? what should I do about it? Do I need a more powerful server or its not related to my server's configurations directly?! Thanks


  • Banned

    This post is deleted!

  • Banned

    This post is deleted!

  • Banned

    This post is deleted!


Looks like your connection to Community was lost, please wait while we try to reconnect.