It’s cool to use a Telegram bot to log things from your application!

A traditional log is boring! So it’s time to move

1/ Create telegram account

Starting with telegram require a Telegram account, of course, so let’s go ahead by downloading the app on your phone and create an account with your phone number

2/ Using FatherBot to create a new bot

  • Type /newbot and complete few other steps guided by FatherBot
  • Copy returning token
A token is a string that was highlighted in the picture
  • Create your own channel
Create new channel
  • Add your bot to the channel and make it as administrator
Add created bot into the channel

This is how API URL looks like https://api.telegram.org/bot<token>/METHOD_NAME I simply assign this URL to base_url to use in our later examples

Replace token with your token, METHOD_NAME with getUpdates

Make an API call

import requests
url = f"{base_url}/getUpdates"
res = requests.get(url=url)
print(res.json())
{'ok': True, 'result': [{'update_id': 903715091, 'my_chat_member': {'chat': {'id': -1001575722406, 'title': 'BCA', 'type': 'channel'}, 'from': {'id': 2053306211, 'is_bot': False, 'first_name': 'Bot', 'last_name': 'Telegram'}, 'date': 1634271044, 'old_chat_member': {'user': {'id': 2018068828, 'is_bot': True, 'first_name': 'fbcrawl', 'username': 'fbcrawl_bot'}, 'status': 'left'}, 'new_chat_member': {'user': {'id': 2018068828, 'is_bot': True, 'first_name': 'fbcrawl', 'username': 'fbcrawl_bot'}, 'status': 'administrator', 'can_be_edited': False, 'can_manage_chat': True, 'can_change_info': True, 'can_post_messages': True, 'can_edit_messages': True, 'can_delete_messages': True, 'can_invite_users': True, 'can_restrict_members': True, 'can_promote_members': False, 'can_manage_voice_chats': True, 'is_anonymous': False}}}, {'update_id': 903715092, 'my_chat_member': {'chat': {'id': -1001754970898, 'title': 'SASAMVIET', 'type': 'channel'}, 'from': {'id': 2053306211, 'is_bot': False, 'first_name': 'Bot', 'last_name': 'Telegram'}, 'date': 1634271082, 'old_chat_member': {'user': {'id': 2018068828, 'is_bot': True, 'first_name': 'fbcrawl', 'username': 'fbcrawl_bot'}, 'status': 'left'}, 'new_chat_member': {'user': {'id': 2018068828, 'is_bot': True, 'first_name': 'fbcrawl', 'username': 'fbcrawl_bot'}, 'status': 'administrator', 'can_be_edited': False, 'can_manage_chat': True, 'can_change_info': True, 'can_post_messages': True, 'can_edit_messages': True, 'can_delete_messages': True, 'can_invite_users': True, 'can_restrict_members': True, 'can_promote_members': False, 'can_manage_voice_chats': True, 'is_anonymous': False}}}, {'update_id': 903715093, 'message': {'message_id': 1, 'from': {'id': 2053306211, 'is_bot': False, 'first_name': 'Bot', 'last_name': 'Telegram', 'language_code': 'en'}, 'chat': {'id': 2053306211, 'first_name': 'Bot', 'last_name': 'Telegram', 'type': 'private'}, 'date': 1634271265, 'text': '/start', 'entities': [{'offset': 0, 'length': 6, 'type': 'bot_command'}]}}]}

Note that we have 2 ids respective with 2 channel

-1001575722406 and -1001754970898

  • Time to send very first message to our channel

Make an API call

url = f"{base_url}/sendMessage"
parameters = {
    "chat_id": "-1001754970898",
    "text": "By logging this way we can keep track of whats happen inside our system"
}
res = requests.post(url=url, data=parameters)

Done! Let’s check it out

Successfully sending a message with telegram bot