當前位置: 首頁>>代碼示例>>Python>>正文


Python telepot.namedtuple方法代碼示例

本文整理匯總了Python中telepot.namedtuple方法的典型用法代碼示例。如果您正苦於以下問題:Python telepot.namedtuple方法的具體用法?Python telepot.namedtuple怎麽用?Python telepot.namedtuple使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在telepot的用法示例。


在下文中一共展示了telepot.namedtuple方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: handle

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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) 
開發者ID:nickoala,項目名稱:telepot,代碼行數:25,代碼來源:emodi.py

示例2: equivalent

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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 
開發者ID:nickoala,項目名稱:telepot,代碼行數:18,代碼來源:test3_send.py

示例3: see_every_content_types

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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.') 
開發者ID:nickoala,項目名稱:telepot,代碼行數:22,代碼來源:test3_send.py

示例4: equivalent

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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 
開發者ID:nickoala,項目名稱:telepot,代碼行數:18,代碼來源:test27_inline.py

示例5: examine

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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) 
開發者ID:nickoala,項目名稱:telepot,代碼行數:20,代碼來源:test27_send.py

示例6: see_every_content_types

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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.') 
開發者ID:nickoala,項目名稱:telepot,代碼行數:22,代碼來源:test3a_send_updates.py

示例7: get_user_profile_photos

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
def get_user_profile_photos():
    print('Getting user profile photos ...')

    r = bot.getUserProfilePhotos(USER_ID)
    examine(r, telepot.namedtuple.UserProfilePhotos) 
開發者ID:nickoala,項目名稱:telepot,代碼行數:7,代碼來源:test3_send.py

示例8: get_user_profile_photos

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
def get_user_profile_photos():
    print 'Getting user profile photos ...'

    r = bot.getUserProfilePhotos(USER_ID)
    examine(r, telepot.namedtuple.UserProfilePhotos) 
開發者ID:nickoala,項目名稱:telepot,代碼行數:7,代碼來源:test27_send.py

示例9: get_user_profile_photos

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
def get_user_profile_photos():
    print('Getting user profile photos ...')

    r = await bot.getUserProfilePhotos(USER_ID)
    examine(r, telepot.namedtuple.UserProfilePhotos) 
開發者ID:nickoala,項目名稱:telepot,代碼行數:7,代碼來源:test3a_send_updates.py

示例10: on_new_chat_member

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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.') 
開發者ID:nickoala,項目名稱:telepot,代碼行數:45,代碼來源:test3a_admin.py

示例11: on_new_chat_member

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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.') 
開發者ID:nickoala,項目名稱:telepot,代碼行數:45,代碼來源:test3_admin.py

示例12: on_new_chat_member

# 需要導入模塊: import telepot [as 別名]
# 或者: from telepot import namedtuple [as 別名]
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.' 
開發者ID:nickoala,項目名稱:telepot,代碼行數:45,代碼來源:test27_admin.py


注:本文中的telepot.namedtuple方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。