Skip to content

Instantly share code, notes, and snippets.

@bembu
Last active October 17, 2016 15:18
Show Gist options
  • Save bembu/fc822d1fd1eafc03e62c0af8af8bec19 to your computer and use it in GitHub Desktop.
Save bembu/fc822d1fd1eafc03e62c0af8af8bec19 to your computer and use it in GitHub Desktop.
A short script to get Telegram group_id
"""
Skripti noutaa botin päivitykset ja parsii vastauksesta
group_id:t.
>> python get_group_id.py
@author Peter Kronstrom
"""
import json
import requests
BOT_TOKEN = 'my-api-token-here'
TG_URL = 'https://api.telegram.org/bot' + BOT_TOKEN
def get_group_id():
"""Skripti noutaa botin päivitykset ja parsii vastauksesta kaikkien ryhmien group_id:t"""
data = requests.post(TG_URL + '/getUpdates', params={'timeout': 10}).text
data = [msg.get('message') for msg in json.loads(data)['result']]
for msg in data:
chat = msg.get('chat')
if chat.get('type') == 'group':
print('<{}> {}'.format(chat.get('title').encode('utf-8'), chat.get('id')))
if __name__ == '__main__':
if BOT_TOKEN == 'my-api-token-here':
print('1. Luo botti BotFatherilla')
print('(2. Anna sille oikeudet lukea viestejä "/setprivacy -> Disable)"')
print('3. Muokkaa muuttujaa BOT_TOKEN tässä tiedostossa')
print('4. Kutsu uusi bottisi ryhmään')
print('5. Lähetä botillesi viesti ryhmässä "\moi@botti"')
print('6. Aja tämä skripti uudelleen nähdäksesi group_id:n')
else:
get_group_id()
@bembu
Copy link
Author

bembu commented Oct 10, 2016

Tested to be working on Python 3 and Python 2.7. Please note, that after Telegram bot creation, bots privacy setting must be disabled (run command /setprivacy with BotFather).

Installation:
sudo pip install requests

Run:
python get_group_id.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment