d_skat

Untitled

May 9th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 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.  
  23. def get_last(data):
  24.     results = data['result']
  25.     count = len(results)
  26.     last = count - 1
  27.     last_update = results[last]
  28.     return last_update
  29.  
  30.  
  31. def get_last_id_text(updates):
  32.     last_update = get_last(updates)
  33.     chat_id = last_update['message']['chat']['id']
  34.     update_id = last_update['update_id']
  35.     try:
  36.         text = last_update['message']['text']
  37.     except:
  38.         text = ''
  39.     return chat_id, text, update_id
  40.  
  41.  
  42. def ask_location(chat_id):
  43.     text = 'Где ты?'
  44.     keyboard = [[{"text": "Отправить местоположение", "request_location": True}]]
  45.     reply_markup = {"keyboard": keyboard, "one_time_keyboard": True}
  46.     send_message(chat_id, text, json.dumps(reply_markup))
  47.  
  48.  
  49. def get_location(update_id):
  50.     updates = get_updates(update_id+1)
  51.     location = get_last(updates)['message']['location']
  52.     chat_id, text, update_id = get_last_id_text(updates)
  53.     lat = str(location['latitude'])
  54.     lon = str(location['longitude'])
  55.     return lat, lon, update_id
  56.  
  57. lat, lon, update_id = get_location(update_id)  # твои координаты
Advertisement
Add Comment
Please, Sign In to add comment