d_skat

Untitled

May 9th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. token = 'твой токен'
  2. url = 'https://api.telegram.org/bot{}/'.format(token)
  3.  
  4.  
  5. def get_updates(offset = None):
  6.     while True:
  7.         try:
  8.             URL = url + 'getUpdates'
  9.             if offset:
  10.                 URL += '?offset={}'.format(offset)
  11.  
  12.             res = requests.get(URL)
  13.             while (res.status_code !=200 or len(res.json()['result'])== 0):
  14.                 sleep(1)
  15.                 res = requests.get(URL)
  16.             print(res.url)
  17.             return res.json()
  18.        
  19.         except:
  20.             pass;
  21.  
  22. def get_last(data):
  23.     results = data['result']
  24.     count = len(results)
  25.     last = count -1
  26.     last_update = results[last]
  27.     return last_update
  28.  
  29.  
  30. def get_last_id_text(updates):
  31.     last_update = get_last(updates)
  32.     chat_id =last_update['message']['chat']['id']
  33.     update_id = last_update['update_id']
  34.     try:
  35.         text = last_update['message']['text']
  36.     except:
  37.         text = ''
  38.     return chat_id,text,update_id
  39.  
  40.  
  41. def get_location(update_id):
  42.     updates = get_updates(update_id+1)
  43.     location = get_last(updates)['message']['location']
  44.     chat_id,text,update_id = get_last_id_text(updates)
  45.     lat = str(location['latitude'])
  46.     lon = str(location['longitude'])
  47.     return lat,lon,update_id
  48.  
  49.  
  50. lat,lon,update_id = get_location(update_id)  # lat, lon - координаты
Advertisement
Add Comment
Please, Sign In to add comment