Example #1
0
    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))

        hangman = t.Hangman(body["message"])  # Constrói o objeto principal
        # print(hangman)
        data = bd.configure(hangman)  # Magia negra ou gambiarra? Fica ai a duvida

        shout = bd.getShout(data)
        if shout:
            bd.checkChatBd(shout[0])
            try:
                reply(c.toDict(shout[0], shout[1].encode("utf-8")))
            except Exception, e:
                logging.info((str(e) + " = " + shout[0].encode("utf-8")))
                if str(e) == "HTTP Error 403: Forbidden":
                    bds.delChat(shout[0])
                    bds.lessPos()
                    reply(c.toDict("-27626712", ("Chat " + shout[0].encode("utf-8") + " excluído")))
                else:
                    time.sleep(0.5)
                    try:
                        reply(c.toDict(shout[0], shout[1].encode("utf-8")))
                    except Exception, e:
                        print(e)
Example #2
0
    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))

        hangman = t.Hangman(body['message'])  #Constrói o objeto principal
        #print(hangman)
        data = bd.configure(
            hangman)  #Magia negra ou gambiarra? Fica ai a duvida

        shout = bd.getShout(data)
        if shout:
            bd.checkChatBd(shout[0])
            try:
                reply(c.toDict(shout[0], shout[1].encode('utf-8')))
            except Exception, e:
                logging.info((str(e) + ' = ' + shout[0].encode('utf-8')))
                if (str(e) == "HTTP Error 403: Forbidden"):
                    bds.delChat(shout[0])
                    bds.lessPos()
                    reply(
                        c.toDict('-27626712',
                                 ('Chat ' + shout[0].encode('utf-8') +
                                  ' excluído')))
                else:
                    time.sleep(0.5)
                    try:
                        reply(c.toDict(shout[0], shout[1].encode('utf-8')))
                    except Exception, e:
                        print(e)
Example #3
0
    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.get('message') if body.get('message') else body.get('edited_message')
        if not message:
            return
        message_id = str(message.get('message_id')).encode('utf-8')
        left_chat_participant = message.get('left_chat_member')
        new_chat_participant = message.get('new_chat_member')
        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 text.startswith('@ccuem_bot'):
            #    text = text[11:]
            if text.startswith('@PlayHangmanBot'):
                text = text[15:]
            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)
        shout = bds.getShout()
        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 shout:
            bds.checkChatBd(shout[0])
            try:
                reply(c.toDict(shout[0], shout[1].encode('utf-8')))
            except Exception, e:
                logging.info((str(e) + ' = ' + shout[0].encode('utf-8')))
                if (str(e) == "HTTP Error 403: Forbidden"):
                    bds.delChat(shout[0])
                    bds.lessPos()
                    reply(c.toDict('-27626712', ('Chat ' + shout[0].encode('utf-8') + ' excluído')))
                else:
                    time.sleep(0.5)
                    try:
                        reply(c.toDict(shout[0], shout[1].encode('utf-8')))
                    except Exception, e:
                        print(e)