本文整理汇总了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)
示例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
示例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.')
示例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
示例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)
示例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.')
示例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)
示例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)
示例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)
示例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.')
示例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.')
示例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.'