def cancelarJogo(chat_id, u_id): l = c.getLanguage(chat_id) if bds.checkAdm(chat_id, u_id): bds.delGame(chat_id) keyboard = c.makeKb(c.getKb(chat_id, 'main')[0], resize_keyboard = True) return [c.toDict(chat_id, l.cancelar_jogo_msg, replyMarkup = keyboard)] return [c.toDict(chat_id, l.cantdo_msg)]
def fecharJogo(chat_id, u_id, message_id, date): l = c.getLanguage(chat_id) rpl = [] if bds.checkAdm(chat_id, u_id): bds.setPreGame(chat_id, False) bds.setInGame(chat_id, True) lista = bds.getCats(chat_id) if ((len(lista) == 0) or (0 in lista)): categoria = randint(0, (len(l.palavras)-1)) else: cat = randint(0, len(lista)-1) categoria = lista[cat]-1 palavra = randint(1, (len(l.palavras[categoria])-1)) palavra = l.palavras[categoria][palavra].decode('utf-8') categoria = l.palavras[categoria][0] mascara = bds.setCP(chat_id, categoria, palavra) vidas = bds.setVidas(chat_id) bds.shufflePlayers(chat_id, date) u_names = bds.getPlayers(chat_id)[1] message_ids = bds.getPlayers(chat_id)[2] ordem = '' for i in range(len(u_names)): ordem = ordem+u_names[i]+'\n' kb = c.makeKb(c.getKb(chat_id, 'main')[0], resize_keyboard = True, selective = True) rpl.append(c.toDict(chat_id, l.close_game_msg, replyMarkup = c.makeKbh(True))) rpl.append(c.toDict(chat_id, ordem, replyTo = message_ids[0], replyMarkup = kb)) rpl.append(c.toDict(chat_id, l.categoria_msg+categoria)) rpl.append(c.toDict(chat_id, l.palavra_msg+mascara)) rpl.append(c.toDict(chat_id, l.vidas_msg+g.vidasEmoji(chat_id))) return rpl return [c.toDict(chat_id, l.cantdo_msg)]
def fecharJogo(chat_id, u_id, message_id, date): l = c.getLanguage(chat_id) rpl = [] if bds.checkAdm(chat_id, u_id): bds.setPreGame(chat_id, False) bds.setInGame(chat_id, True) lista = bds.getCats(chat_id) if ((len(lista) == 0) or (0 in lista)): categoria = randint(0, (len(l.palavras)-1)) else: cat = randint(0, len(lista)-1) categoria = lista[cat]-1 palavra = randint(1, (len(l.palavras[categoria])-1)) palavra = l.palavras[categoria][palavra].decode('utf-8') categoria = l.palavras[categoria][0] mascara = bds.setCP(chat_id, categoria, palavra) vidas = bds.setVidas(chat_id) bds.shufflePlayers(chat_id, date) u_names = bds.getPlayers(chat_id)[1] message_ids = bds.getPlayers(chat_id)[2] ordem = '' for i in range(len(u_names)): ordem = ordem+u_names[i]+'\n' kb = c.makeKb(c.getKb(chat_id, 'main')[0], resize_keyboard = True, selective = True) rpl.append(c.toDict(chat_id, l.close_game_msg, replyMarkup = c.makeKbh(True))) rpl.append(c.toDict(chat_id, ordem + '\n' + l.categoria_msg+categoria + '\n' + l.palavra_msg+mascara + '\n' + l.vidas_msg+g.vidasEmoji(chat_id), replyTo = message_ids[0], replyMarkup = kb)) """rpl.append(c.toDict(chat_id, l.categoria_msg+categoria)) rpl.append(c.toDict(chat_id, l.palavra_msg+mascara)) rpl.append(c.toDict(chat_id, l.vidas_msg+g.vidasEmoji(chat_id)))""" return rpl return [c.toDict(chat_id, l.cantdo_msg)]
def comandos(chat_id, message_id, u_id): l = getLanguage(chat_id) kb = getKb(chat_id, 'cmd') if not (bds.getInGame(chat_id) and bds.checkAdm(chat_id, u_id)): keyboard = makeKb(kb[0], resize_keyboard = True, selective = True) else: keyboard = makeKb(kb[1], resize_keyboard = True, selective = True) return [toDict(chat_id, l.comandos_msg, replyTo = message_id, replyMarkup = keyboard)]
def categorias(chat_id, u_id, message_id): l = c.getLanguage(chat_id) if bds.checkAdm(chat_id, u_id): bds.setCategorias(chat_id, True) return [ c.toDict(chat_id, l.cat_msg, replyTo=message_id, replyMarkup=c.makeFr(True, selective=True)) ]
def kb(chat_id, u_id, message_id): l = getLanguage(chat_id) kb = getKb(chat_id, 'main', u_id = u_id) keyboard = makeKb(kb[0], resize_keyboard = True, selective = True) if (bds.getInGame(chat_id)) or (not bds.getPreGame(chat_id)): i = 0 elif bds.getPreGame(chat_id): i = 0 if bds.checkAdm(chat_id, u_id): i = 1 keyboard = makeKb(kb[i], resize_keyboard = True, selective = True) return [toDict(chat_id, l.teclado_msg, replyTo = message_id, replyMarkup = keyboard)]
def comandos(chat_id, message_id, u_id): l = getLanguage(chat_id) kb = getKb(chat_id, 'cmd') if not (bds.getInGame(chat_id) and bds.checkAdm(chat_id, u_id)): keyboard = makeKb(kb[0], resize_keyboard=True, selective=True) else: keyboard = makeKb(kb[1], resize_keyboard=True, selective=True) return [ toDict(chat_id, l.comandos_msg, replyTo=message_id, replyMarkup=keyboard) ]
def voltar(chat_id, msg, message_id, u_id, esp=None): rpl = [] i = 0 if bds.getSettings(chat_id).waiting: bds.setWaiting(chat_id, False) if bds.getInGame(chat_id): kb = getKb(chat_id, 'cmd') if not bds.checkAdm(chat_id, u_id): i = 0 else: i = 1 elif bds.getPreGame(chat_id): kb = getKb(chat_id, 'cmd') i = 0 else: kb = getKb(chat_id, 'main') i = 0 else: if bds.getArriscarBlock(chat_id): bds.setArriscarBlock(chat_id, False) kb = getKb(chat_id, 'main', u_id=u_id) if len(kb) != 1: if not bds.checkAdm(chat_id, u_id): i = 0 else: i = 1 if not esp: keyboard = makeKb(kb[i], resize_keyboard=True, selective=True) rpl.append( toDict(chat_id, msg, replyTo=message_id, replyMarkup=keyboard)) else: keyboard = makeKb(kb[0], resize_keyboard=True) rpl.append(toDict(chat_id, msg, replyMarkup=keyboard)) if len(kb) != 1: keyboard = makeKb(kb[1], resize_keyboard=True, selective=True) rpl.append( toDict(chat_id, msg, replyTo=message_id, replyMarkup=keyboard)) return rpl
def voltar(chat_id, msg, message_id, u_id, esp = None): rpl = [] i = 0 if bds.getSettings(chat_id).waiting: bds.setWaiting(chat_id, False) if bds.getInGame(chat_id): kb = getKb(chat_id, 'cmd') if not bds.checkAdm(chat_id, u_id): i = 0 else: i = 1 elif bds.getPreGame(chat_id): kb = getKb(chat_id, 'cmd') i = 0 else: kb = getKb(chat_id, 'main') i = 0 else: if bds.getArriscarBlock(chat_id): bds.setArriscarBlock(chat_id, False) kb = getKb(chat_id, 'main', u_id = u_id) if len(kb) != 1: if not bds.checkAdm(chat_id, u_id): i = 0 else: i = 1 if not esp: keyboard = makeKb(kb[i], resize_keyboard = True, selective = True) rpl.append(toDict(chat_id, msg, replyTo = message_id, replyMarkup = keyboard)) else: keyboard = makeKb(kb[0], resize_keyboard = True) rpl.append(toDict(chat_id, msg, replyMarkup = keyboard)) if len(kb) != 1: keyboard = makeKb(kb[1], resize_keyboard = True, selective = True) rpl.append(toDict(chat_id, msg, replyTo = message_id, replyMarkup = keyboard)) return rpl
def kb(chat_id, u_id, message_id, waiting): l = getLanguage(chat_id) kb = getKb(chat_id, 'main', u_id=u_id) keyboard = makeKb(kb[0], resize_keyboard=True, selective=True) if bds.getEnabled(chat_id): if (bds.getInGame(chat_id)) or (not bds.getPreGame(chat_id)): i = 0 elif bds.getPreGame(chat_id): i = 0 if bds.checkAdm(chat_id, u_id): i = 1 else: i = 0 keyboard = makeKb(kb[i], resize_keyboard=True, selective=True) if waiting: kb = getKb(chat_id, 'config') keyboard = makeKb(kb[0], resize_keyboard=True, selective=True) return [ toDict(chat_id, l.teclado_msg, replyTo=message_id, replyMarkup=keyboard) ]
def post(self): urlfetch.set_default_fetch_deadline(60) body = json.loads(self.request.body) logging.info('request body:') logging.info(body) self.response.write(json.dumps(body)) #Função que verifica se o forca_bot foi excluido ou se ele existe no BD def verifyBot(left_chat_participant = None): if left_chat_participant: first_name = left_chat_participant['first_name'].encode('utf-8') if first_name == 'The Hangman': bds.delChat(chat_id) return return #Dados que recebemos do telegram update_id = body['update_id'] message = body['message'] message_id = str(message.get('message_id')).encode('utf-8') left_chat_participant = message.get('left_chat_participant') new_chat_participant = message.get('new_chat_participant') group_chat_created = message.get('group_chat_created') date = message.get('date') text = message.get('text').encode('utf-8') if message.get('text') else message.get('text') if text: if not text.startswith('/admin'): text = text.lower() fr = message.get('from') chat = message['chat'] chat_id = str(chat['id']) user_id = message['from'] u_id = str(user_id.get('id')).encode('utf-8') u_name = user_id.get('first_name').encode('utf-8') bds.checkChat(chat_id) #'Chama' a verificação. if left_chat_participant: verifyBot(left_chat_participant = left_chat_participant) if not text: logging.info('no text') return #Função que envia o dict para o Telegram def reply(dict = None): if dict: resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode(dict)).read() else: logging.error('no msg or img specified') resp = None logging.info('send response:') logging.info(resp) #Lê as configurações def getLanguage(chat_id): s = bds.getSettings(chat_id) #Classe settings if s: if s.language == 'ptBR': import ptBR as l return l elif s.language == 'enUS': import enUS as l return l elif s.language == 'hbIL': import hbIL as l return l else: bds.checkChat(chat_id) s = bds.getSettings(chat_id) #Classe settings if s.language == 'ptBR': import ptBR as l return l elif s.language == 'enUS': import enUS as l return l elif s.language == 'hbIL': import hbIL as l return l return #Aqui começa a lógica principal l = getLanguage(chat_id) s = bds.getSettings(chat_id) ab = bds.getArriscarBlock(chat_id) first = bds.getFirstWelcome(chat_id)[0] rpl = [c.toDict(chat_id, l.sorry_msg)] text = '/start' if text == l.ligar.lower() else text #Tratamento para o caso do /start text = l.ajuda.lower() if text.startswith('/help') else text text = l.desligar.lower() if text.startswith('/stop') else text if text.startswith('@PlayHangmanBot'): text = text[15:] if (u_id in creators) and (text.startswith('/admin')): #Funções especiais dos criadores do bot if text.startswith('/admindelchat'): chat = text[14:] if len(chat) > 0: if bds.delChat(chat): rpl = [c.toDict(chat_id, 'Chat '+chat+' deletado')] else: rpl = [c.toDict(chat_id, 'Chat '+chat+' não existe')] elif text.startswith('/adminshout'): text = text[12:] chats = bds.getChats() rpl = [] for i in range(len(chats)): time.sleep(0.1) rpl.append(c.toDict(chats[i], text)) elif text.startswith('/admingetdadoschat'): chat = text[19:] if len(chat) > 0: dados = bds.getDadosChat(chat) jogos_dia = bds.getJogosDia(chat, date) if dados: rpl = [c.toDict(chat_id, 'Chat '+str(chat)+'\nJogos: '+ str(dados.games)+'\nJogadores: '+str(len(dados.players))+'\nJogos por dia: '+str(jogos_dia)+'\nTop player: '+dados.topPlayer.u_name.encode('utf-8')+'\n\tScore: '+str(dados.topPlayer.u_score)+'\n\tId: '+str(dados.topPlayer.u_id))] else: rpl = [c.toDict(chat_id, 'Chat '+chat+' não existe')] elif text.startswith('/admingetdadosglobais'): urlfetch.set_default_fetch_deadline(300) resp = bds.getDadosGlobais(date) rpl = [c.toDict(chat_id, 'Chats: '+str(resp[0])+'\nJogadores: '+str(resp[1])+'\nJogos por dia: '+str(resp[2])+'\nJogos: '+str(resp[3]))] elif (not s.waiting) or first: #comandos que indiferem do estado atual de jogo if '/start' in text: rpl = c.start(chat_id, u_id, first) elif bds.getEnabled(chat_id): if ('/kb' in text) or (l.att_kb in text): rpl = c.kb(chat_id, u_id, message_id, s.waiting) elif l.desligar.lower() in text: rpl = c.stop(chat_id) elif l.ajuda.lower() in text: rpl = c.ajuda(chat_id) elif l.rank.lower() in text: rpl = c.rank(chat_id) elif l.config.lower() in text: rpl = c.config(chat_id, message_id) elif l.sobre.lower() in text: rpl = c.sobre(chat_id) elif l.voltar.lower() in text: rpl = c.voltar(chat_id, l.voltar_msg, message_id, u_id) elif l.comandos.lower() in text: rpl = c.comandos(chat_id, message_id, u_id) #comandos inGame elif bds.getInGame(chat_id): check = bds.checkUid(chat_id, u_id) if l.cancelar_jogo.lower() in text: rpl = g.cancelarJogo(chat_id, u_id) elif check == True: if bds.getArriscarBlock(chat_id): rpl = g.arriscarPalavra2(chat_id, u_id, u_name, message_id, text) elif l.arriscar.lower() in text: rpl = g.arriscarPalavra1(chat_id, u_id, message_id) elif (len(text) == 1) or (text.startswith('@PlayHangmanBot')): if text.startswith('@PlayHangmanBot'): text = text[10:] rpl = g.chutarLetra(chat_id, u_id, u_name, message_id, text) elif check == 'rnd': rpl = [c.toDict(chat_id, l.round_errado_msg)] elif check == 'out': rpl = [c.toDict(chat_id, l.fora_msg)] #comandos preGame elif bds.getPreGame(chat_id): if s.categorias and bds.checkAdm(chat_id, u_id): rpl = p.setCategorias(chat_id, text, message_id,u_id) elif l.entrar.lower() in text: rpl = p.entrar(chat_id, u_id, u_name, message_id) elif l.categorias_btn.lower() in text: rpl = p.categorias(chat_id, u_id, message_id) elif l.sair.lower() in text: rpl = p.sair(chat_id, u_id, u_name, message_id) elif l.fechar_jogo.lower() in text: rpl = p.fecharJogo(chat_id, u_id, message_id, date) elif l.cancelar_jogo.lower() in text: rpl = p.cancelarJogo(chat_id, u_id) #se preGame e inGame == False (vide flowchart) elif (not bds.getPreGame(chat_id)) and (not bds.getInGame(chat_id)): if l.novojogo.lower() in text: rpl = c.novojogo(chat_id, u_id, u_name, message_id) else: if l.ajuda.lower() in text: rpl = c.ajuda(chat_id) elif '/kb' in text: rpl = c.kb(chat_id, u_id, message_id, s.waiting) else: rpl = c.changeLanguage(chat_id, text, message_id, u_id) error = False for i in range(len(rpl)): time.sleep(1.5) try: reply(rpl[i]) except Exception, e: print e if (str(e) == "HTTP Error 429: Unknown") and (not error): error = True time.sleep(2) i = i - 1 else: try: reply(c.toDict(chat_id, l.error_msg)) except Exception, e: print e try: reply(c.toDict(chat_id, 'Fatal error, contact @cristoferoswald or @bcesarg6.')) except Exception, e: print e
if bds.getArriscarBlock(chat_id): rpl = g.arriscarPalavra2(chat_id, u_id, u_name, message_id, text) elif l.arriscar.lower() in text: rpl = g.arriscarPalavra1(chat_id, u_id, message_id) elif (len(text) == 1) or (text.startswith('@PlayHangmanBot')): if text.startswith('@PlayHangmanBot'): text = text[10:] rpl = g.chutarLetra(chat_id, u_id, u_name, message_id, text) elif check == 'rnd': rpl = [c.toDict(chat_id, l.round_errado_msg)] elif check == 'out': rpl = [c.toDict(chat_id, l.fora_msg)] #comandos preGame elif bds.getPreGame(chat_id): if s.categorias and bds.checkAdm(chat_id, u_id): rpl = p.setCategorias(chat_id, text, message_id,u_id) elif l.entrar.lower() in text: rpl = p.entrar(chat_id, u_id, u_name, message_id) elif l.categorias_btn.lower() in text: rpl = p.categorias(chat_id, u_id, message_id) elif l.sair.lower() in text: rpl = p.sair(chat_id, u_id, u_name, message_id) elif l.fechar_jogo.lower() in text: rpl = p.fecharJogo(chat_id, u_id, message_id, date) elif l.cancelar_jogo.lower() in text: rpl = p.cancelarJogo(chat_id, u_id) #se preGame e inGame == False (vide flowchart) elif (not bds.getPreGame(chat_id)) and (not bds.getInGame(chat_id)): if l.novojogo.lower() in text: rpl = c.novojogo(chat_id, u_id, u_name, message_id)
def categorias(chat_id, u_id, message_id): l = c.getLanguage(chat_id) if bds.checkAdm(chat_id, u_id): bds.setCategorias(chat_id,True) return [c.toDict(chat_id, l.cat_msg, replyTo = message_id, replyMarkup = c.makeFr(True, selective = True))]