def handle(msg):
    flavor = telepot.flavor(msg)

    # normal message
    if flavor == 'normal':
        content_type, chat_type, chat_id = telepot.glance(msg)
        print 'Normal Message:', content_type, chat_type, chat_id

        # Do your stuff according to `content_type` ...

    # inline query - need `/setinline`
    elif flavor == 'inline_query':
        query_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print 'Inline Query:', query_id, from_id, query_string

        # Compose your own answers
        articles = [{
            'type': 'article',
            'id': 'abc',
            'title': 'ABC',
            'message_text': 'Good morning'
        }]

        bot.answerInlineQuery(query_id, articles)

    # chosen inline result - need `/setinlinefeedback`
    elif flavor == 'chosen_inline_result':
        result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print 'Chosen Inline Result:', result_id, from_id, query_string

        # Remember the chosen answer to do better next time

    else:
        raise telepot.BadFlavor(msg)
Beispiel #2
0
def see_every_content_types(msg):
    global expected_content_type, content_type_iterator

    flavor = telepot.flavor(msg)

    if flavor == 'normal':
        content_type, chat_type, chat_id = telepot.glance2(msg)
        from_id = msg['from']['id']

        if chat_id != USER_ID and from_id != USER_ID:
            print 'Unauthorized user:'******'Message')
        try:
            if content_type == expected_content_type:
                expected_content_type = content_type_iterator.next()
                bot.sendMessage(chat_id,
                                'Please give me a %s.' % expected_content_type)
            else:
                bot.sendMessage(
                    chat_id, 'It is not a %s. Please give me a %s, please.' %
                    (expected_content_type, expected_content_type))
        except StopIteration:
            # reply to sender because I am kicked from group already
            bot.sendMessage(from_id, 'Thank you. I am done.')

    else:
        raise telepot.BadFlavor(msg)
Beispiel #3
0
    def handle(self, msg):

        if 'migrate_to_chat_id' in msg:
            yield from self.onSupergroupUpgradeCallback(self, msg)

        else:
            flavor = telepot.flavor(msg)

            if flavor == "chat":  # chat message
                content_type, chat_type, chat_id = telepot.glance(msg)
                if content_type == 'text':
                    if TelegramBot.is_command(msg):  # bot command
                        cmd, params = TelegramBot.parse_command(msg['text'])
                        user_id = TelegramBot.get_user_id(msg)
                        args = {'params': params, 'user_id': user_id, 'chat_type': chat_type}
                        if cmd in self.commands:
                            yield from self.commands[cmd](self, chat_id, args)
                        else:
                            if self.config['be_quiet']:
                                pass
                            else:
                                comebacks = [
                                    'Can\'t toast that: {cmd}',
                                ]
                                shuffle(comebacks)
                                yield from self.sendMessage(chat_id, comebacks[0].format(cmd=cmd))

                    else:  # plain text message
                        yield from self.onMessageCallback(self, chat_id, msg)

                elif content_type == 'location':
                    yield from self.onLocationShareCallback(self, chat_id, msg)

                elif content_type == 'new_chat_member':
                    yield from self.onUserJoinCallback(self, chat_id, msg)

                elif content_type == 'left_chat_member':
                    yield from self.onUserLeaveCallback(self, chat_id, msg)

                elif content_type == 'photo':
                    yield from self.onPhotoCallback(self, chat_id, msg)

                elif content_type == 'sticker':
                    if 'enable_sticker_sync' in tg_bot.ho_bot.config.get_by_path(['telesync']):
                        if tg_bot.ho_bot.config.get_by_path(['telesync'])['enable_sticker_sync']:
                            yield from self.onStickerCallback(self, chat_id, msg)

            elif flavor == "inline_query":  # inline query e.g. "@gif cute panda"
                query_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
                print("inline_query")

            elif flavor == "chosen_inline_result":
                result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
                print("chosen_inline_result")

            else:
                raise telepot.BadFlavor(msg)
Beispiel #4
0
    def handle(self, msg):
        flavor = telepot.flavor(msg)

        if flavor == "normal":  # normal message
            content_type, chat_type, chat_id = telepot.glance2(msg)
            if content_type == 'text':
                if TelegramBot.is_command(msg):  # bot command
                    cmd, params = TelegramBot.parse_command(msg['text'])
                    user_id = TelegramBot.get_user_id(msg)
                    args = {
                        'params': params,
                        'user_id': user_id,
                        'chat_type': chat_type
                    }
                    if cmd in self.commands:
                        yield from self.commands[cmd](self, chat_id, args)
                    else:
                        yield from self.sendMessage(
                            chat_id, "Unknown command: {cmd}".format(cmd=cmd))

                else:  # plain text message
                    yield from self.onMessageCallback(self, chat_id, msg)

            elif content_type == 'location':
                yield from self.onLocationShareCallback(self, chat_id, msg)

            elif content_type == 'new_chat_participant':
                yield from self.onUserJoinCallback(self, chat_id, msg)

            elif content_type == 'left_chat_participant':
                yield from self.onUserLeaveCallback(self, chat_id, msg)

            elif content_type == 'photo':
                yield from self.onPhotoCallback(self, chat_id, msg)

        elif flavor == "inline_query":  # inline query e.g. "@gif cute panda"
            query_id, from_id, query_string = telepot.glance2(msg,
                                                              flavor=flavor)
            print("inline_query")

        elif flavor == "chosen_inline_result":
            result_id, from_id, query_string = telepot.glance2(msg,
                                                               flavor=flavor)
            print("chosen_inline_result")

        else:
            raise telepot.BadFlavor(msg)
    def on_message(self, msg):
        print(u'on_message() is being called')
        flavor = telepot.flavor(msg)

        # normal message
        if chkNConv(flavor) == u'normal':
            content_type, chat_type, _chat_id = telepot.glance2(msg)
            print('Normal Message:', content_type, chat_type, _chat_id, '; message content: ', msg)

            if content_type == 'text':
                if self._convert_type == ConverType.nothing:
                    if chkNConv(msg['text']) == u'/start':
                        self.sender.sendMessage(text=bot_starting_script)
                    elif chkNConv(msg['text']) == u'/newgame' or chkNConv(msg['text']) == u'/newgame@' + chkNConv(bot_name):
                        self.create_game(msg=msg)
        else:
            raise telepot.BadFlavor(msg)
Beispiel #6
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])

        yield from bot.answerInlineQuery(query_id, results, cache_time=20, is_personal=True, next_offset='5')

    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)
Beispiel #7
0
def handle(msg):
    flavor = telepot.flavor(msg)

    # normal message
    if flavor == 'normal':
        content_type, chat_type, chat_id = telepot.glance(msg)
        print 'Normal Message:', content_type, chat_type, chat_id,
        content = msg['text']
        if containsKeyword(content):
            bot.sendMessage(chat_id, content)
            responseImg = random.choice(os.listdir("./Images/"))
            responsefile = open("./Images/" + responseImg, 'rb')
            bot.sendPhoto(chat_id, responsefile)
            # Do your stuff according to `content_type` ...

    # inline query - need `/setinline`
    elif flavor == 'inline_query':
        query_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print 'Inline Query:', query_id, from_id, query_string

        # Compose your own answers
        articles = [{
            'type': 'article',
            'id': 'abc',
            'title': 'ABC',
            'message_text': 'Good morning'
        }]

        bot.answerInlineQuery(query_id, articles)

    # chosen inline result - need `/setinlinefeedback`
    elif flavor == 'chosen_inline_result':
        result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print 'Chosen Inline Result:', result_id, from_id, query_string

        # Remember the chosen answer to do better next time

    else:
        raise telepot.BadFlavor(msg)
Beispiel #8
0
def handle(msg):
    flavor = telepot.flavor(msg)

    #print 'Got command: %s' % command

    if flavor == 'chat':

        welcomtekst = "The Thor events are : \nType words of the event for more info" + "\nThe latest update of the Thor data was at : " + time2 + "\nType the reset commando in order to obtain new data"
        content_type, chat_type, chat_id = telepot.glance(msg)
        print('Normal Message:', content_type, chat_type, chat_id)
        chat_id = msg['chat']['id']
        command = msg['text']
        from2 = msg['from']
        usernname2 = from2['username']
        if command == '/thor':
            bot.sendMessage(chat_id, welcomtekst)
            #bot.sendMessage(chat_id, "Type words of the event for more info")

            bot.sendMessage(chat_id, titletekst)
        if command == '/thor@Thor_promo_bot':
            bot.sendMessage(chat_id, welcomtekst)
            bot.sendMessage(chat_id, titletekst)
        if command == '/update':
                pushtext = usernname2 + " has updated the script"
                push = pb.push_note("Thor bot", pushtext )

        #     print("Update the script")
        #     bot.sendMessage(chat_id, "Updating the script , the next server reboot will use the new script")
        #     git.Git().clone("git://github.com/martijnvandijke/Thor-promo-bot")

        if command == '/photo':
             for o in range( 0, len(data3)):
                filename = str(o) + ".jpg"
                f = open(filename, 'rb')
                bot.sendPhoto(chat_id, f)

        if command == '/poster':
            for o in range( 0, len(data3)):
                filename = str(o) + ".jpg"
                f = open(filename, 'rb')
                bot.sendPhoto(chat_id, f)

        if command == '/photo@Thor_promo_bot':
             for o in range( 0, len(data3)):
                filename = str(o) + ".jpg"
                f = open(filename, 'rb')
                bot.sendPhoto(chat_id, f)

        if command == '/poster@Thor_promo_bot':
             for o in range( 0, len(data3)):
                filename = str(o) + ".jpg"
                f = open(filename, 'rb')
                bot.sendPhoto(chat_id, f)

        if command == '/about@Thor_promo_bot':
            abouttext = "This Thor bot is made by @Martyn_van_Dijke if you want you can contribute at the github https://github.com/martijnvandijke/Thor-promo-bot \nThe bot is hosted by Frank Boerman thanks for this :) "
            bot.sendMessage(chat_id, abouttext)

        if command == '/about':
            abouttext = "This Thor bot is made by @Martyn_van_Dijke if you want you can contribute at the github https://github.com/martijnvandijke/Thor-promo-bot \nThe bot is hosted by Frank Boerman thanks for this :) "
            bot.sendMessage(chat_id, abouttext)

        if command == '/reset':
            print("Obtaining new data")
            bot.sendMessage(chat_id, "Nieuwe data binnen halen")
            pushtext = usernname2 + " has requested a reload of the data"
            push = pb.push_note("Thor bot", pushtext )
            #photos()
            global titletekst,tekst,lengte,tijd,location,title,time2,dt
            titletekst,tekst,lengte,tijd,location,title,time2,dt = data()


        if command == '/reset@Thor_promo_bot':
            print("Obtaining new data")
            bot.sendMessage(chat_id, "Nieuwe data binnen halen")
            pushtext = usernname2 + " has requested a reload of the data"
            push = pb.push_note("Thor bot", pushtext )
            #photos()
            global titletekst,tekst,lengte,tijd,location,title,time2,dt
            titletekst,tekst,lengte,tijd,location,title,time2,dt = data()

        if datetime.now() > dt:
            print ("Ik ben al 12 uur met dezelfde data ik ga refreshhen")
            pushtext = " I am going to refresh the data -> since i have been 12 hours active"
            push = pb.push_note("Thor bot", pushtext )
            photos()
            global titletekst,tekst,lengte,tijd,location,title,time2,dt
            titletekst,tekst,lengte,tijd,location,title,time2,dt = data()

        for y in range(0, lengte):
            if (command in title[y]) or (command in title[y].lower()) or (command in title[y].upper()):
                print(y)
                print(tekst[y])
                timetext = "The time of the event is : \n" + tijd[y]
                locationtext = "\nThe location of the event is : \n" + location[y]
                overalltext = timetext + locationtext
                bot.sendMessage(chat_id, tekst[y])
                bot.sendMessage(chat_id, overalltext)


    elif flavor == 'inline_query':
        articles = []
        query_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print 'Inline Query:', query_id, from_id, query_string
        for i in range(0, len(title)):
            inlinetekst = tekst[i] + " \n \n"
            timetext = "\n The time of the event is : \n" + tijd[i]
            locationtext = "\nThe location of the event is : \n" + location[i]
            overalltext = inlinetekst + timetext + locationtext
            print overalltext
            articles = articles +  [{'type': 'article','id': str(i), 'title': str(title[i]), 'message_text': overalltext }]
            print (articles)

        bot.answerInlineQuery(query_id, articles)

    # chosen inline result - need `/setinlinefeedback`
    elif flavor == 'chosen_inline_result':
        result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print 'Chosen Inline Result:', result_id, from_id, query_string

        # Remember the chosen answer to do better next time

    else:
        raise telepot.BadFlavor(msg)
Beispiel #9
0
def handle(msg):
    flavor = telepot.flavor(msg)
    # pprint(msg)

    # chat message
    if flavor == 'chat':
        content_type, chat_type, chat_id = telepot.glance(msg)
        if chat_type == 'group' or chat_type == 'private':
            if content_type == 'text':
                text = msg['text']
                # text message parsing
                world_list = re.findall(regexStringPattern, text)
                prev_word = ""
                for word in world_list:
                    result = regexWordObj.match(word)
                    # Somebody wrote the word of interest
                    if result is not None:
                        maan_bot_response = compute_response(word)
                        if prev_word == "bella":
                            maan_bot_response = "bella " + maan_bot_response + " :))"
                        else:
                            if prev_word == "ciao":
                                maan_bot_response = "ciàm" + maan_bot_response + "!!"
                        bot.sendMessage(chat_id, maan_bot_response)
                    else:
                        # we check if the word instead of 'man' is 'pullman'
                        result_pullman = regexPullmanWordObj.match(word)
                        if result_pullman is not None:
                            maan_bot_response = "il pull" + compute_response(
                                word)
                            bot.sendMessage(chat_id, maan_bot_response)
                    prev_word = word
            elif content_type == 'left_chat_member':
                if msg['left_chat_member']['first_name'] != 'MaaanBot':
                    bot.sendMessage(chat_id, 'addio maaaaan :\'-(')
            elif content_type == 'new_chat_member':
                bot.sendMessage(chat_id, 'ciao maaaaan')
            else:
                print('content_type = ' + content_type)
        else:
            print('chat_type = ' + chat_type)

    # callback query - originated from a callback button
    elif flavor == 'callback_query':
        query_id, from_id, query_data = telepot.glance(msg, flavor=flavor)
        print('Callback query:', query_id, from_id, query_data)

    # inline query - need `/setinline`
    elif flavor == 'inline_query':
        query_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print('Inline Query:', query_id, from_id, query_string)

        # Compose your own answers
        articles = [{
            'type': 'article',
            'id': 'abc',
            'title': 'ABC',
            'message_text': 'maaaaaan!'
        }]

        bot.answerInlineQuery(query_id, articles)

    # chosen inline result - need `/setinlinefeedback`
    elif flavor == 'chosen_inline_result':
        result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
        print('Chosen Inline Result:', result_id, from_id, query_string)
        # Remember the chosen answer to do better next time

    else:
        raise telepot.BadFlavor(msg)