Writing a telegram bot in python using the telebot library part 2

Hello! This is the second part of the article in which I will devote time to telegram requests and servers. If someone else has not seen the first part, then I recommend reading it . The third part will most likely describe how to work with databases. Well, if you again have questions, then write to me in telegram: dimagorovtsov



What is message?



Probably many who wrote the bot in my previous article asked what message is and why, for example, to send a message, we need to specify message.chat.id in the send_message function parameters? To find out, let's display message in the console:



@bot.message_handler(commands=['start']) def start(message): print(message)
      
      





Now when we enter the / start command, our bot sends a huge amount of information. All we got now is a json response. Json is a simple format for storing structured data. Everything is displayed in the format: 'key': value. Let's look at what I got:



 { 'content_type':'text', 'message_id':573, 'from_user':{ 'id':687595402, 'is_bot':False, 'first_name':'Dmitry', 'username':'dimagorovtsov', 'last_name':'Gorovtsov', 'language_code':'ru' }, 'date':1565206363, 'chat':{ 'type':'private', 'last_name':'Gorovtsov', 'first_name':'Dmitry', 'username':'dimagorovtsov', 'id':687595402, 'title':None, 'all_members_are_administrators':None, 'photo':None, 'description':None, 'invite_link':None, 'pinned_message':None, 'sticker_set_name':None, 'can_set_sticker_set':None }, 'forward_from_chat':None, 'forward_from':None, 'forward_date':None, 'reply_to_message':None, 'edit_date':None, 'media_group_id':None, 'author_signature':None, 'text':'/start', 'entities':[ <telebot.types.MessageEntity object at 0x03807F50> ], 'json':{ 'message_id':573, 'from':{ 'id':687595402, 'is_bot':False, 'first_name':'Dmitry', 'last_name':'Gorovtsov', 'username':'dimagorovtsov', 'language_code':'ru' }, 'chat':{ 'id':687595402, 'first_name':'Dmitry', 'last_name':'Gorovtsov', 'username':'dimagorovtsov', 'type':'private' }, 'date':1565206363, 'text':'/start', 'entities':[ { 'offset':0, 'length':6, 'type':'bot_command' } ] } }
      
      





For example, from all this information we want to get the id of the chat from which I sent the message. To do this, turn to the chat key.



Request:



 print(message.chat)
      
      





Answer:



 {'type': 'private', 'last_name': 'Gorovtsov', 'first_name': 'Dmitry', 'username': 'dimagorovtsov', 'id': 687595402, 'title': None, 'all_members_are_administrators': None, 'photo': None, 'description': None, 'invite_link': None, 'pinned_message': None, 'sticker_set_name': None, 'can_set_sticker_set': None}
      
      





Look, the chat key has a few more keys: first_name, last_name, username ... and each of them has its own values. Now let's turn to the id key:



 print(message.chat.id)
      
      





As you can see, in order to get the desired value, you just need to write the name of the keys through a dot. Now look at the response from the server:



 687595402
      
      





Everything is going as it should! We got the chat id, exactly as we wanted! Now get the name of the sender. Here, as you noticed, you need to use the from_user key.



Request:



 print(message.from_user)
      
      





Answer:



 {'id': 687595402, 'is_bot': False, 'first_name': 'Dmitry', 'username': 'dimagorovtsov', 'last_name': 'Gorovtsov', 'language_code': 'ru'}
      
      





Now we get the value of the first_name key:



 print(message.from_user.first_name)
      
      





Well that's all! In a couple of seconds, we were able to get the chat id and my name in the telegram. And again, for those who do not understand:







To get the value of the first_name key, we first need to turn to the chat key, and only then to first_name!



Now look, in order to send a message to any chat we need to specify several parameters in the send_message function. The first parameter is chat_id, the chat id itself. The second is text, the text of the message. And you guessed it, instead of writing message.chat.id, we can write our data! This is how you can send a message to yourself if you specify your id in the parameters:



 bot.send_message(< id>, ' ')
      
      





Well, when we write message.chat.id, we mean that the bot will send a message to the chat from which it was called.



Conclusion



Well, that’s all! I hope you understand how to receive data from the server, process it and use it where necessary. Thanks for attention.



All Articles