Exemple #1
0
    def compute():
        query_id, from_id, query_string = telepot.glance(msg,
                                                         flavor='inline_query')
        print('Inline Query:', query_id, from_id, query_string)

        responses = retrieve_something_from_subreddit(query_string)

        json_data = subprocess.run(
            ["curl", "-XPOST", "-d", gfycat_credentials, gfycat_auth],
            capture_output=True).stdout
        gfycat_bearer = json.loads(json_data)

        count = 0
        for response in responses:
            try:
                urlFormat = response.url[-4:]

                if response.url[:5] == 'https':
                    gfycat = response.url[8:14]
                else:
                    gfycat = response.url[7:13]

                if count == 0:
                    if response.url[-3:] == 'gif' or response.url[
                            -4:] == 'gifv':
                        articles = [
                            InlineQueryResultGif(id=response.id,
                                                 type='gif',
                                                 title=response.title,
                                                 gif_width=10,
                                                 gif_height=10,
                                                 gif_url=response.url,
                                                 thumb_url=response.url)
                        ]
                    elif gfycat == 'gfycat':
                        pos_id = [
                            pos for pos, char in enumerate(response.url)
                            if char == '/'
                        ]
                        gfycat_id = response.url[pos_id[-1] + 1:]

                        json_data = subprocess.run([
                            "curl", "-X", "GET",
                            "https://api.gfycat.com/v1/gfycats/" + gfycat_id,
                            "-H", "Authorization: Bearer " +
                            gfycat_bearer["access_token"]
                        ],
                                                   capture_output=True).stdout
                        gfycat_json = json.loads(json_data)

                        articles = [
                            InlineQueryResultGif(
                                id=response.id,
                                type='gif',
                                title=response.title,
                                gif_width=10,
                                gif_height=10,
                                gif_url=gfycat_json["gfyItem"]["max5mbGif"],
                                thumb_url=gfycat_json["gfyItem"]["max1mbGif"])
                        ]
                    elif response.url[-3:] == 'png' or response.url[
                            -3:] == 'jpg' or (response.url[8:17] == "i.redd.it"
                                              and response.url[-3:] != "gif"):
                        articles = [
                            InlineQueryResultPhoto(id=response.id,
                                                   type='photo',
                                                   photo_url=response.url,
                                                   thumb_url=response.url,
                                                   photo_width=10,
                                                   photo_height=10)
                        ]
                    #elif response.url[8:17] == "v.redd.it":
                    #    articles = [InlineQueryResultVideo(
                    #                    id=response.id,
                    #                    type='video',
                    #                    title=response.title,
                    #                    video_url=response.url,
                    #                    thumb_url=response.url,
                    #                    video_width=50,
                    #                    video_height=50,
                    #                    mime_type='video/mp4'
                    #                )]
                    else:
                        articles = [
                            InlineQueryResultArticle(
                                id=response.id,
                                title=response.title,
                                thumb_url=response.url,
                                input_message_content=InputTextMessageContent(
                                    message_text=response.url +
                                    ' powered by: ' +
                                    response.subreddit_name_prefixed))
                        ]
                    count += 1
                else:
                    if response.url[-3:] == 'gif' or response.url[
                            -4:] == 'gifv':
                        articles += [
                            InlineQueryResultGif(id=response.id,
                                                 type='gif',
                                                 title=response.title,
                                                 gif_width=10,
                                                 gif_height=10,
                                                 gif_url=response.url,
                                                 thumb_url=response.url)
                        ]
                    elif gfycat == 'gfycat':
                        pos_id = [
                            pos for pos, char in enumerate(response.url)
                            if char == '/'
                        ]
                        gfycat_id = response.url[pos_id[-1] + 1:]

                        json_data = subprocess.run([
                            "curl", "-X", "GET",
                            "https://api.gfycat.com/v1/gfycats/" + gfycat_id,
                            "-H", "Authorization: Bearer " +
                            gfycat_bearer["access_token"]
                        ],
                                                   capture_output=True).stdout
                        gfycat_json = json.loads(json_data)

                        articles += [
                            InlineQueryResultGif(
                                id=response.id,
                                type='gif',
                                title=response.title,
                                gif_width=10,
                                gif_height=10,
                                gif_url=gfycat_json["gfyItem"]["max5mbGif"],
                                thumb_url=gfycat_json["gfyItem"]["max1mbGif"])
                        ]
                    elif response.url[-3:] == 'png' or response.url[
                            -3:] == 'jpg' or (response.url[8:17] == "i.redd.it"
                                              and response.url[-3:] != "gif"):
                        articles += [
                            InlineQueryResultPhoto(id=response.id,
                                                   type='photo',
                                                   photo_url=response.url,
                                                   thumb_url=response.url,
                                                   photo_width=10,
                                                   photo_height=10)
                        ]
                    #elif response.url[8:17] == "v.redd.it":
                    #    articles += [InlineQueryResultVideo(
                    #                    id=response.id,
                    #                    type='video',
                    #                    title=response.title,
                    #                    video_url=response.url,
                    #                    thumb_url=response.url,
                    #                    video_width=50,
                    #                    video_height=50,
                    #                    mime_type='video/mp4'
                    #                )]
                    else:
                        articles += [
                            InlineQueryResultArticle(
                                id=response.id,
                                title=response.title,
                                thumb_url=response.url,
                                input_message_content=InputTextMessageContent(
                                    message_text=response.url +
                                    ' powered by: ' +
                                    response.subreddit_name_prefixed))
                        ]
            except Exception as e:
                print("Error at " + response.url)
                print(e)
        return articles
Exemple #2
0
def answer(msg):
    flavor = telepot.flavor(msg)

    if flavor == 'inline_query':
        query_id, from_id, query = telepot.glance2(msg, flavor=flavor)

        if from_id != USER_ID:
            print 'Unauthorized user:'******'InlineQuery')

        articles = [
            InlineQueryResultArticle(id='abc',
                                     title='HK',
                                     message_text='Hong Kong',
                                     url='https://www.google.com',
                                     hide_url=True), {
                                         'type': 'article',
                                         'id': 'def',
                                         'title': 'SZ',
                                         'message_text': 'Shenzhen',
                                         'url': 'https://www.yahoo.com'
                                     }
        ]

        photos = [
            InlineQueryResultPhoto(
                id='123',
                photo_url=
                'https://core.telegram.org/file/811140934/1/tbDSLHSaijc/fdcc7b6d5fb3354adf',
                thumb_url=
                'https://core.telegram.org/file/811140934/1/tbDSLHSaijc/fdcc7b6d5fb3354adf'
            ), {
                'type': 'photo',
                'id': '345',
                'photo_url':
                'https://core.telegram.org/file/811140184/1/5YJxx-rostA/ad3f74094485fb97bd',
                'thumb_url':
                'https://core.telegram.org/file/811140184/1/5YJxx-rostA/ad3f74094485fb97bd',
                'caption': 'Caption',
                'title': 'Title',
                'message_text': 'Message Text'
            }
        ]

        results = random.choice([articles, photos])

        bot.answerInlineQuery(query_id, results)

    elif flavor == 'chosen_inline_result':
        result_id, from_id, query = telepot.glance2(msg, flavor=flavor)

        if from_id != USER_ID:
            print 'Unauthorized user:'******'ChosenInlineResult')

        print 'Chosen inline query:'
        pprint.pprint(msg)

    else:
        raise telepot.BadFlavor(msg)
Exemple #3
0
    def compute():

        query_id, from_id, query_string = telepot.glance(msg,
                                                         flavor='inline_query')

        print('%s: Computing for: %s' %
              (threading.current_thread().name, query_string))

        articles = [
            InlineQueryResultArticle(
                id='abcde',
                title='Compartilhe',
                input_message_content=InputTextMessageContent(
                    message_text='Conheca o windows bot @Windows_BetaRoBot')),
            dict(
                type='article',
                id='fghij',
                title='Nosso Grupo',
                input_message_content=dict(
                    message_text=
                    'entre em meu grupo https://t.me/joinchat/D7lDSUQoqgaVljLOpIviHg'
                ))
        ]

        photo1_url = 'https://core.telegram.org/file/811140934/1/tbDSLHSaijc/fdcc7b6d5fb3354adf'

        photo2_url = 'https://www.telegram.org/img/t_logo.png'

        photos = [
            InlineQueryResultPhoto(id='12345',
                                   photo_url=photo1_url,
                                   thumb_url=photo1_url),
            dict(type='photo',
                 id='67890',
                 photo_url=photo2_url,
                 thumb_url=photo2_url)
        ]

        result_type = query_string[-1:].lower()

        if result_type == 'a':

            return articles

        elif result_type == 'p':

            return photos

        else:

            results = articles if random.randint(0, 1) else photos

            if result_type == 'b':

                return dict(results=results,
                            switch_pm_text='Back to Bot',
                            switch_pm_parameter='Optional start parameter')

            else:

                return dict(results=results)