How To Limit Access To A Telegram Bot

后端 未结 5 1343
感情败类
感情败类 2020-12-16 12:07

When I send a message to my Telegram Bot, it responses with no problems.

I wanna limit the access such that me and only me can send message to it.

How can I

5条回答
  •  盖世英雄少女心
    2020-12-16 12:30

    Start a conversation with your bot, and send it a message. This will queue up an updates for the bot containing the message and the chat ID for your conversation.

    To view recent updates, you call the getUpdates method. This is done by making a HTTP GET request to the URL https://api.telegram.org/bot$TOKEN/getUpdates Where $TOKEN is the token provided by the BotFather. Something like:

    "chat":{
            "id":12345,
            "first_name":"Bob",
            "last_name":"Jones",
            "username":"bjones",
            "type":"private"},
          "date":1452933785,
          "text":"Hi there, bot!"}}]}
    

    Once you determined your chat id you can write a piece of code in your bot like:

    id_a = [111111,2222222,3333333,4444444,5555555]
    
        def handle(msg):
            chat_id = msg['chat']['id']
            command = msg['text']
            sender = msg['from']['id']
         if sender in id_a:
        [...]
         else:
               bot.sendMessage(chat_id, 'Forbidden access!')
               bot.sendMessage(chat_id, sender)
    

提交回复
热议问题