Featured image of post Telegram Bot From the Shell

Telegram Bot From the Shell

Creating your Bot

  • First you have to open a chat with @BotFather by klicking this link.
  • Now send him /newbot
    • Give him a display-name for your new bot
    • Give him a identifyer for your new bot (This should look like <name>bot)
  • The BotFather sends you a link to start a chat with your bot and the api-token to access the new bot. Please note down the api-token at a safe place!

Getting the Chat-ID

  • Open the link to your Bot
  • Send /start to your Bot
  • Open up https://api.telegram.org/bot<api_token>/getUpdates
  • Every message comes with an id-field. This id is the chat-id!

Now we have everything to get started with sending messages.

Easy Scripts to get started

Here are some easy scripts that I personaly use for cron-status-messages. You should replace <api_token> and <chat_id> with your own values.

telegram_message.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
logger -p info "Sending Telegram message"
api_token=<api_token>
user_chat_id=<chat_id>
message="$1"
curl -s \
        -X POST \
        https://api.telegram.org/bot$api_token/sendMessage \
        -d text="$message" \
        -d chat_id=$user_chat_id
logger -p info "Telegram message send: $message"

Send a text-message to your chat-id via ./telegram_message.sh <your_message>

telegram_file.sh

1
2
3
4
5
6
logger -p info "Sending Telegram message"
api_token=<api_token>
user_chat_id=<chat_id>
file=$1
curl -F document=@$file https://api.telegram.org/bot$api_token/sendDocument?chat_id=$user_chat_id
logger -p info "Telegram message send: $file"

Send a file to your chat-id via ./telegram_message.sh <path_to_file>