def run_command(offset, name, from_id, cmd): cmd = cmd.lower() cmd = cmd.split(' ') main = cmd[0] if main in commands: send_text(from_id, commands[cmd[0]]) elif main in dnd: send_text(from_id, throw_cubes(cmd)) elif main in [u'катя', u'kate']: i = randint(0, len(kate)-1) send_text(from_id, kate[i]) elif main == '/reboot': if from_id in ADMIN_IDs: log_event('Reboot by %s' % name) send_text(from_id, u'Ушел покурить') reboot() else: unauthorized(from_id, name, cmd) elif main == '/abort': if from_id in ADMIN_IDs: log_event('Aborted by %s' % name) send_text(from_id, u'Выключаюсь') abort() else: unauthorized(from_id, name, cmd) elif main == '/pause': speed = toggle_play() if speed: text = 'play' else: text = 'pause' send_text(from_id, text) else: i = randint(0, len(unknown)-1) send_text(from_id, unknown[i])
def check_updates(): """Проверка обновлений на сервере и инициация действий, в зависимости от команды""" global offset data = {'offset': offset + 1, 'limit': 10, 'timeout': 5} try: r = requests.post(FULL_URL + '/getUpdates', data=data) except: log_event('Error getting updates') return False if not r.status_code == 200: return False if not r.json()['ok']: return False for update in r.json()['result']: offset = update['update_id'] offset_file = open('offset', 'r') # Грязный хак. TODO пока не будет отслеживания old_offset = int(offset_file.read()) offset_file.close() if old_offset >= offset: continue offset_file = open('offset', 'w') offset_file.write(str(offset)) offset_file.close() if 'message' not in update or 'text' not in update['message']: log_event('Unknown update: %s' % update) continue from_id = update['message']['chat']['id'] if 'username' in update['message']['chat']: username = update['message']['chat']['username'] elif 'first_name' in update['message']['chat']: username = update['message']['chat']['first_name'] elif 'last_name' in update['message']['chat']: username = update['message']['chat']['last_name'] message = update['message']['text'] parameters = (offset, username, from_id, message) log_event('Message (id%s) from %s (id%s): "%s"' % parameters) run_command(*parameters)