def post(self):
        urlfetch.set_default_fetch_deadline(60)

        logging.debug("Request: %s" % self.request.body)
        body = json.loads(self.request.body)
        update = telegram.Update.de_json(body)

        chat_id = update.message.chat.id
        message = update.message.text
        error_message = ""
        logging.debug("message text: %s" % message)
        req_message = False
        send_message = ""
        if message is not None:
            if "/start" in message or "/help" in message:
                self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_START)

            printList = list()
            no_of_res = 5  # default, max = all results on page 1
            name = None
            if "/BN:" in message or "/bn:" in message:
                req_message = True
                if len(message) >= 8:  # (4[an or bn] + 4[query])
                    name, no_of_res, format = self.checkInput(message[4:])
                    if name is not None:
                        book = getPDF(name, "title")
                        bookList = book.connect()
                        printList = book.formatList(bookList)
                    elif format == 0:
                        req_message = False
                        self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_INVALID)
                else:
                    req_message = False
                    self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_SMALL)

            if "/AN:" in message or "/an:" in message:
                req_message = True
                if len(message) >= 8:  # (4[an or bn] + 4[query])
                    name, no_of_res, format = self.checkInput(message[4:])
                    if name is not None:
                        book = getPDF(name, "author")
                        bookList = book.connect()
                        printList = book.formatList(bookList)
                    elif format == 0:
                        req_message = False
                        self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_INVALID)
                else:
                    req_message = False
                    self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_SMALL)

            if len(printList) > 0 and req_message == True:
                for i, eachBook in enumerate(printList, 1):
                    if i <= no_of_res:
                        self.sendMessageFn(chat_id, eachBook)

            elif len(printList) == 0 and req_message == True:
                self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_NORESULT)

        self.response.write(json.dumps(body))
Пример #2
0
def make_flashcards():
    text = request.form.get('text')
    keywords = [
        word for word in request.form.get('keywords').split(",")
        if len(word) > 1
    ]
    filename = getPDF(text, keywords)
    # Return them using send_from_directory
    return send_from_directory('', filename)
Пример #3
0
def run_command(chat_id,text):
    if "/getHabr" in text:
        proxy = urllib2.ProxyHandler({'https': 'https://*****:*****@proxy.ksu.ru:8080'})
        opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler,proxy)
        urllib2.install_opener(opener)
        if len(text.split(" ")) > 1:
            data = {"chat_id":str(chat_id),"text":"Create PDF"}
            opener.open(URL + TOKEN + '/sendMessage',data)

            link = getPDF.getPDF(text.split(" ")[-1])


            data = {"chat_id":str(chat_id),"text":"Upload file"}
            opener.open(URL + TOKEN + '/sendMessage',data)
            data = {"chat_id":str(chat_id),"document":open(link,"rb")}
            opener.open(URL + TOKEN + '/sendDocument',data)
            os.remove(link)
        else:
            data = {"chat_id":str(chat_id),"text":"Null link"}
            opener.open(URL + TOKEN + '/sendMessage',data)
Пример #4
0
def run_command(chat_id, text):
    if "/getHabr" in text:
        proxy = urllib2.ProxyHandler(
            {'https': 'https://*****:*****@proxy.ksu.ru:8080'})
        opener = urllib2.build_opener(
            MultipartPostHandler.MultipartPostHandler, proxy)
        urllib2.install_opener(opener)
        if len(text.split(" ")) > 1:
            data = {"chat_id": str(chat_id), "text": "Create PDF"}
            opener.open(URL + TOKEN + '/sendMessage', data)

            link = getPDF.getPDF(text.split(" ")[-1])

            data = {"chat_id": str(chat_id), "text": "Upload file"}
            opener.open(URL + TOKEN + '/sendMessage', data)
            data = {"chat_id": str(chat_id), "document": open(link, "rb")}
            opener.open(URL + TOKEN + '/sendDocument', data)
            os.remove(link)
        else:
            data = {"chat_id": str(chat_id), "text": "Null link"}
            opener.open(URL + TOKEN + '/sendMessage', data)
Пример #5
0
    def post(self):
        urlfetch.set_default_fetch_deadline(60)

        logging.debug('Request: %s' % self.request.body)
        body = json.loads(self.request.body)
        update = telegram.Update.de_json(body)

        chat_id = update.message.chat.id
        message = update.message.text
        error_message = ""
        logging.debug('message text: %s' % message)
        req_message = False
        send_message = ""
        if message is not None:
            if '/start' in message or '/help' in message:
                self.sendMessageFn(chat_id, getPDFBotWebhookPage.MESSAGE_START)

            printList = list()
            no_of_res = 5  # default, max = all results on page 1
            name = None
            if '/BN:' in message or '/bn:' in message:
                req_message = True
                if len(message) >= 8:  #(4[an or bn] + 4[query])
                    name, no_of_res, format = self.checkInput(message[4:])
                    if name is not None:
                        book = getPDF(name, "title")
                        bookList = book.connect()
                        printList = book.formatList(bookList)
                    elif format == 0:
                        req_message = False
                        self.sendMessageFn(
                            chat_id, getPDFBotWebhookPage.MESSAGE_INVALID)
                else:
                    req_message = False
                    self.sendMessageFn(chat_id,
                                       getPDFBotWebhookPage.MESSAGE_SMALL)

            if '/AN:' in message or '/an:' in message:
                req_message = True
                if len(message) >= 8:  #(4[an or bn] + 4[query])
                    name, no_of_res, format = self.checkInput(message[4:])
                    if name is not None:
                        book = getPDF(name, "author")
                        bookList = book.connect()
                        printList = book.formatList(bookList)
                    elif format == 0:
                        req_message = False
                        self.sendMessageFn(
                            chat_id, getPDFBotWebhookPage.MESSAGE_INVALID)
                else:
                    req_message = False
                    self.sendMessageFn(chat_id,
                                       getPDFBotWebhookPage.MESSAGE_SMALL)

            if len(printList) > 0 and req_message == True:
                for i, eachBook in enumerate(printList, 1):
                    if i <= no_of_res:
                        self.sendMessageFn(chat_id, eachBook)

            elif len(printList) == 0 and req_message == True:
                self.sendMessageFn(chat_id,
                                   getPDFBotWebhookPage.MESSAGE_NORESULT)

        self.response.write(json.dumps(body))