-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatid.py
executable file
·38 lines (29 loc) · 1.01 KB
/
chatid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
import sys
if sys.version_info < (3, 0):
print("Python3 required.")
exit()
import urllib.request
import json
from time import sleep
def main(argv):
botId = argv[1]
url = "https://api.telegram.org/bot" + botId + "/getUpdates"
print("botId: " + botId)
try:
response = urllib.request.urlopen(url)
print("HTTP response code: " + str(response.getcode()))
except Exception as e:
print("Exception " + str(e.__class__.__name__) + " " + str(e))
exit()
jsonData = json.loads(response.read().decode())
if len(jsonData["result"]) == 0:
print("I can't get the chatId if you don't send a message to the bot first. Try to send a random message and run me again.")
exit()
chatId = jsonData["result"][0]["message"]["chat"]["id"]
print("ChatId: " + str(chatId))
if __name__ == "__main__":
if len(sys.argv) == 2:
main(sys.argv)
else:
print("Usage: >python3 " + sys.argv[0] + " <BotId> ")