Python telepot.namedtuple() Examples

The following are 17 code examples of telepot.namedtuple(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module telepot , or try the search function .
Example #1
Source File: test3_send.py    From telepot with MIT License 6 votes vote down vote up
def equivalent(data, nt):
    if type(data) is dict:
        keys = list(data.keys())

        # number of dictionary keys == number of non-None values in namedtuple?
        if len(keys) != len([f for f in nt._fields if getattr(nt, f) is not None]):
            return False

        # map `from` to `from_`
        fields = list([k+'_' if k in ['from'] else k for k in keys])

        return all(map(equivalent, [data[k] for k in keys], [getattr(nt, f) for f in fields]))
    elif type(data) is list:
        return all(map(equivalent, data, nt))
    else:
        return data==nt 
Example #2
Source File: test3_send.py    From telepot with MIT License 6 votes vote down vote up
def see_every_content_types(msg):
    global expected_content_type, content_type_iterator

    content_type, chat_type, chat_id = telepot.glance(msg)
    from_id = msg['from']['id']

    if chat_id != USER_ID and from_id != USER_ID:
        print('Unauthorized user:', chat_id, from_id)
        return

    examine(msg, telepot.namedtuple.Message)
    try:
        if content_type == expected_content_type:
            expected_content_type = next(content_type_iterator)
            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.') 
Example #3
Source File: test27_inline.py    From telepot with MIT License 6 votes vote down vote up
def equivalent(data, nt):
    if type(data) is dict:
        keys = data.keys()

        # number of dictionary keys == number of non-None values in namedtuple?
        if len(keys) != len([f for f in nt._fields if getattr(nt, f) is not None]):
            return False

        # map `from` to `from_`
        fields = list(map(lambda k: k+'_' if k in ['from'] else k, keys))

        return all(map(equivalent, [data[k] for k in keys], [getattr(nt, f) for f in fields]))
    elif type(data) is list:
        return all(map(equivalent, data, nt))
    else:
        return data==nt 
Example #4
Source File: test27_send.py    From telepot with MIT License 6 votes vote down vote up
def examine(result, type):
    try:
        print 'Examining %s ......' % type

        nt = type(**result)
        assert equivalent(result, nt), 'Not equivalent:::::::::::::::\n%s\n::::::::::::::::\n%s' % (result, nt)

        if type == telepot.namedtuple.Message:
            print 'Message glance: %s' % str(telepot.glance(result, long=True))

        pprint.pprint(result)
        pprint.pprint(nt)
        print
    except AssertionError:
        traceback.print_exc()
        answer = raw_input('Do you want to continue? [y] ')
        if answer != 'y':
            exit(1) 
Example #5
Source File: emodi.py    From telepot with MIT License 6 votes vote down vote up
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    m = telepot.namedtuple.Message(**msg)

    if chat_id < 0:
        # group message
        print 'Received a %s from %s, by %s' % (content_type, m.chat, m.from_)
    else:
        # private message
        print 'Received a %s from %s' % (content_type, m.chat)  # m.chat == m.from_

    if content_type == 'text':
        reply = ''

        # For long messages, only return the first 10 characters.
        if len(msg['text']) > 10:
            reply = u'First 10 characters:\n'

        # Length-checking and substring-extraction may work differently
        # depending on Python versions and platforms. See above.

        reply += msg['text'][:10].encode('unicode-escape').decode('ascii')
        bot.sendMessage(chat_id, reply) 
Example #6
Source File: test3_inline.py    From telepot with MIT License 6 votes vote down vote up
def equivalent(data, nt):
    if type(data) is dict:
        keys = data.keys()

        # number of dictionary keys == number of non-None values in namedtuple?
        if len(keys) != len([f for f in nt._fields if getattr(nt, f) is not None]):
            return False

        # map `from` to `from_`
        fields = list(map(lambda k: k+'_' if k in ['from'] else k, keys))

        return all(map(equivalent, [data[k] for k in keys], [getattr(nt, f) for f in fields]))
    elif type(data) is list:
        return all(map(equivalent, data, nt))
    else:
        return data==nt 
Example #7
Source File: test27_updates.py    From telepot with MIT License 6 votes vote down vote up
def equivalent(data, nt):
    if type(data) is dict:
        keys = data.keys()

        # number of dictionary keys == number of non-None values in namedtuple?
        if len(keys) != len([f for f in nt._fields if getattr(nt, f) is not None]):
            return False

        # map `from` to `from_`
        fields = list(map(lambda k: k+'_' if k in ['from'] else k, keys))

        return all(map(equivalent, [data[k] for k in keys], [getattr(nt, f) for f in fields]))
    elif type(data) is list:
        return all(map(equivalent, data, nt))
    else:
        return data==nt 
Example #8
Source File: test3_updates.py    From telepot with MIT License 6 votes vote down vote up
def equivalent(data, nt):
    if type(data) is dict:
        keys = list(data.keys())

        # number of dictionary keys == number of non-None values in namedtuple?
        if len(keys) != len([f for f in nt._fields if getattr(nt, f) is not None]):
            return False

        # map `from` to `from_`
        fields = list([k+'_' if k in ['from'] else k for k in keys])

        return all(map(equivalent, [data[k] for k in keys], [getattr(nt, f) for f in fields]))
    elif type(data) is list:
        return all(map(equivalent, data, nt))
    else:
        return data==nt 
Example #9
Source File: test3_updates.py    From telepot with MIT License 6 votes vote down vote up
def see_every_content_types(msg):
    global expected_content_type, content_type_iterator

    content_type, chat_type, chat_id = telepot.glance(msg)
    from_id = msg['from']['id']

    if chat_id != USER_ID and from_id != USER_ID:
        print('Unauthorized user:', chat_id, from_id)
        return

    examine(msg, telepot.namedtuple.Message)
    try:
        if content_type == expected_content_type:
            expected_content_type = next(content_type_iterator)
            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.') 
Example #10
Source File: test3a_send_updates.py    From telepot with MIT License 6 votes vote down vote up
def equivalent(data, nt):
    if type(data) is dict:
        keys = list(data.keys())

        # number of dictionary keys == number of non-None values in namedtuple?
        if len(keys) != len([f for f in nt._fields if getattr(nt, f) is not None]):
            return False

        # map `from` to `from_`
        fields = list([k+'_' if k in ['from'] else k for k in keys])

        return all(map(equivalent, [data[k] for k in keys], [getattr(nt, f) for f in fields]))
    elif type(data) is list:
        return all(map(equivalent, data, nt))
    else:
        return data==nt 
Example #11
Source File: test3a_send_updates.py    From telepot with MIT License 6 votes vote down vote up
def see_every_content_types(msg):
    global expected_content_type, content_type_iterator

    content_type, chat_type, chat_id = telepot.glance(msg)
    from_id = msg['from']['id']

    if chat_id != USER_ID and from_id != USER_ID:
        print('Unauthorized user:', chat_id)
        return

    examine(msg, telepot.namedtuple.Message)
    try:
        if content_type == expected_content_type:
            expected_content_type = next(content_type_iterator)
            await bot.sendMessage(chat_id, 'Please give me a %s.' % expected_content_type)
        else:
            await 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
        await bot.sendMessage(from_id, 'Thank you. I am done.') 
Example #12
Source File: test3_send.py    From telepot with MIT License 5 votes vote down vote up
def get_user_profile_photos():
    print('Getting user profile photos ...')

    r = bot.getUserProfilePhotos(USER_ID)
    examine(r, telepot.namedtuple.UserProfilePhotos) 
Example #13
Source File: test27_send.py    From telepot with MIT License 5 votes vote down vote up
def get_user_profile_photos():
    print 'Getting user profile photos ...'

    r = bot.getUserProfilePhotos(USER_ID)
    examine(r, telepot.namedtuple.UserProfilePhotos) 
Example #14
Source File: test3a_send_updates.py    From telepot with MIT License 5 votes vote down vote up
def get_user_profile_photos():
    print('Getting user profile photos ...')

    r = await bot.getUserProfilePhotos(USER_ID)
    examine(r, telepot.namedtuple.UserProfilePhotos) 
Example #15
Source File: test27_admin.py    From telepot with MIT License 4 votes vote down vote up
def on_new_chat_member(self, msg, new_chat_member):
        print 'New chat member:', new_chat_member
        content_type, chat_type, chat_id = telepot.glance(msg)

        r = self.getChat(chat_id)
        print r

        r = self.getChatAdministrators(chat_id)
        print r
        print telepot.namedtuple.ChatMemberArray(r)

        r = self.getChatMembersCount(chat_id)
        print r

        while 1:
            try:
                self.setChatTitle(chat_id, 'AdminBot Title')
                print 'Set title successfully.'
                break
            except NotEnoughRightsError:
                print 'No right to set title. Try again in 10 seconds ...'
                time.sleep(10)

        while 1:
            try:
                self.setChatPhoto(chat_id, open('gandhi.png', 'rb'))
                print 'Set photo successfully.'
                time.sleep(2)  # let tester see photo briefly
                break
            except NotEnoughRightsError:
                print 'No right to set photo. Try again in 10 seconds ...'
                time.sleep(10)

        while 1:
            try:
                self.deleteChatPhoto(chat_id)
                print 'Delete photo successfully.'
                break
            except NotEnoughRightsError:
                print 'No right to delete photo. Try again in 10 seconds ...'
                time.sleep(10)

        print 'I am done. You may remove me from the group.' 
Example #16
Source File: test3_admin.py    From telepot with MIT License 4 votes vote down vote up
def on_new_chat_member(self, msg, new_chat_member):
        print('New chat member:', new_chat_member)
        content_type, chat_type, chat_id = telepot.glance(msg)

        r = self.getChat(chat_id)
        print(r)

        r = self.getChatAdministrators(chat_id)
        print(r)
        print(telepot.namedtuple.ChatMemberArray(r))

        r = self.getChatMembersCount(chat_id)
        print(r)

        while 1:
            try:
                self.setChatTitle(chat_id, 'AdminBot Title')
                print('Set title successfully.')
                break
            except NotEnoughRightsError:
                print('No right to set title. Try again in 10 seconds ...')
                time.sleep(10)

        while 1:
            try:
                self.setChatPhoto(chat_id, open('gandhi.png', 'rb'))
                print('Set photo successfully.')
                time.sleep(2)  # let tester see photo briefly
                break
            except NotEnoughRightsError:
                print('No right to set photo. Try again in 10 seconds ...')
                time.sleep(10)

        while 1:
            try:
                self.deleteChatPhoto(chat_id)
                print('Delete photo successfully.')
                break
            except NotEnoughRightsError:
                print('No right to delete photo. Try again in 10 seconds ...')
                time.sleep(10)

        print('I am done. Remove me from the group.') 
Example #17
Source File: test3a_admin.py    From telepot with MIT License 4 votes vote down vote up
def on_new_chat_member(self, msg, new_chat_member):
        print('New chat member:', new_chat_member)
        content_type, chat_type, chat_id = telepot.glance(msg)

        r = await self.getChat(chat_id)
        print(r)

        r = await self.getChatAdministrators(chat_id)
        print(r)
        print(telepot.namedtuple.ChatMemberArray(r))

        r = await self.getChatMembersCount(chat_id)
        print(r)

        while 1:
            try:
                await self.setChatTitle(chat_id, 'AdminBot Title')
                print('Set title successfully.')
                break
            except NotEnoughRightsError:
                print('No right to set title. Try again in 10 seconds ...')
                await asyncio.sleep(10)

        while 1:
            try:
                await self.setChatPhoto(chat_id, open('gandhi.png', 'rb'))
                print('Set photo successfully.')
                await asyncio.sleep(2)  # let tester see photo briefly
                break
            except NotEnoughRightsError:
                print('No right to set photo. Try again in 10 seconds ...')
                await asyncio.sleep(10)

        while 1:
            try:
                await self.deleteChatPhoto(chat_id)
                print('Delete photo successfully.')
                break
            except NotEnoughRightsError:
                print('No right to delete photo. Try again in 10 seconds ...')
                await asyncio.sleep(10)

        print('I am done. Remove me from the group.')