Sending messages with Telegram - APIs or CLI?

前端 未结 6 1688
-上瘾入骨i
-上瘾入骨i 2020-12-12 17:31

I would like to be able to send a message to a group chat in Telegram. I want to run a python script (which makes some operations that already works) and then, if some param

6条回答
  •  无人及你
    2020-12-12 18:05

    Telegram recently released their new Bot API which makes sending/receiving messages trivial. I suggest you also take a look at that and see if it fits your needs, it beats wrapping the client library or integrating with their MTProto API.

    import urllib
    import urllib2
    
    # Generate a bot ID here: https://core.telegram.org/bots#botfather
    bot_id = "{YOUR_BOT_ID}"
    
    # Request latest messages
    result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/getUpdates").read()
    print result
    
    # Send a message to a chat room (chat room ID retrieved from getUpdates)
    result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/sendMessage", urllib.urlencode({ "chat_id": 0, "text": 'my message' })).read()
    print result
    

    Unfortunately I haven't seen any Python libraries you can interact directly with, but here is a NodeJS equivalent I worked on for reference.

提交回复
热议问题