本文整理汇总了Python中telegram.ext.Filters类的典型用法代码示例。如果您正苦于以下问题:Python Filters类的具体用法?Python Filters怎么用?Python Filters使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Filters类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_filters_user_id
def test_filters_user_id(self):
self.assertFalse(Filters.user(user_id=1)(self.message))
self.message.from_user.id = 1
self.assertTrue(Filters.user(user_id=1)(self.message))
self.message.from_user.id = 2
self.assertTrue(Filters.user(user_id=[1, 2])(self.message))
self.assertFalse(Filters.user(user_id=1)(self.message))
示例2: test_filters_chat_id
def test_filters_chat_id(self):
self.assertFalse(Filters.chat(chat_id=-1)(self.message))
self.message.chat.id = -1
self.assertTrue(Filters.chat(chat_id=-1)(self.message))
self.message.chat.id = -2
self.assertTrue(Filters.chat(chat_id=[-1, -2])(self.message))
self.assertFalse(Filters.chat(chat_id=-1)(self.message))
示例3: test_filters_chat_username
def test_filters_chat_username(self):
self.assertFalse(Filters.chat(username='chat')(self.message))
self.message.chat.username = 'chat'
self.assertTrue(Filters.chat(username='@chat')(self.message))
self.assertTrue(Filters.chat(username='chat')(self.message))
self.assertTrue(Filters.chat(username=['chat1', 'chat', 'chat2'])(self.message))
self.assertFalse(Filters.chat(username=['@chat1', 'chat_2'])(self.message))
示例4: test_filters_chat_username
def test_filters_chat_username(self, message):
assert not Filters.chat(username='chat')(message)
message.chat.username = 'chat'
assert Filters.chat(username='@chat')(message)
assert Filters.chat(username='chat')(message)
assert Filters.chat(username=['chat1', 'chat', 'chat2'])(message)
assert not Filters.chat(username=['@chat1', 'chat_2'])(message)
示例5: test_inverted_filters
def test_inverted_filters(self, message):
message.text = '/test'
assert Filters.command(message)
assert not (~Filters.command)(message)
message.text = 'test'
assert not Filters.command(message)
assert (~Filters.command)(message)
示例6: test_filters_reply
def test_filters_reply(self, message):
another_message = Message(1, User(1, 'TestOther', False), datetime.datetime.now(),
Chat(0, 'private'))
message.text = 'test'
assert not Filters.reply(message)
message.reply_to_message = another_message
assert Filters.reply(message)
示例7: test_filters_user_id
def test_filters_user_id(self, message):
assert not Filters.user(user_id=1)(message)
message.from_user.id = 1
assert Filters.user(user_id=1)(message)
message.from_user.id = 2
assert Filters.user(user_id=[1, 2])(message)
assert not Filters.user(user_id=[3, 4])(message)
示例8: test_filters_chat_id
def test_filters_chat_id(self, message):
assert not Filters.chat(chat_id=-1)(message)
message.chat.id = -1
assert Filters.chat(chat_id=-1)(message)
message.chat.id = -2
assert Filters.chat(chat_id=[-1, -2])(message)
assert not Filters.chat(chat_id=[-3, -4])(message)
示例9: main
def main():
config = configparser.ConfigParser()
config.read('bot.ini')
updater = Updater(token=config['KEYS']['bot_api'], use_context=True)
dispatcher = updater.dispatcher
global SELF_CHAT_ID
SELF_CHAT_ID = f'@{updater.bot.get_me().username}'
rate_limit_tracker_handler = MessageHandler(~Filters.command, rate_limit_tracker)
start_handler = CommandHandler('start', start)
rules_handler = CommandHandler('rules', rules)
rules_handler_hashtag = MessageHandler(Filters.regex(r'.*#rules.*'), rules)
docs_handler = CommandHandler('docs', docs)
wiki_handler = CommandHandler('wiki', wiki)
sandwich_handler = MessageHandler(Filters.regex(r'(?i)[\s\S]*?((sudo )?make me a sandwich)[\s\S]*?'),
sandwich)
off_on_topic_handler = MessageHandler(Filters.regex(r'(?i)[\s\S]*?\b(?<!["\\])(off|on)[- _]?topic\b'),
off_on_topic)
# We need several matches so Filters.regex is basically useless
# therefore we catch everything and do regex ourselves
# This should probably be in another dispatcher group
# but I kept getting SystemErrors...
github_handler = MessageHandler(Filters.text, github)
forward_faq_handler = MessageHandler(Filters.regex(r'(?i).*#faq.*'), forward_faq)
dispatcher.add_handler(rate_limit_tracker_handler, group=-1)
# Note: Order matters!
taghints.register(dispatcher)
dispatcher.add_handler(forward_faq_handler)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(rules_handler)
dispatcher.add_handler(rules_handler_hashtag)
dispatcher.add_handler(docs_handler)
dispatcher.add_handler(wiki_handler)
dispatcher.add_handler(sandwich_handler)
dispatcher.add_handler(off_on_topic_handler)
dispatcher.add_handler(github_handler)
inlinequeries.register(dispatcher)
dispatcher.add_error_handler(error)
updater.start_polling()
logger.info('Listening...')
try:
github_issues.set_auth(config['KEYS']['github_client_id'], config['KEYS']['github_client_secret'])
except KeyError:
logging.info('No github auth set. Rate-limit is 60 requests/hour without auth.')
github_issues.init_issues(dispatcher.job_queue)
updater.idle()
示例10: test_entities_filter
def test_entities_filter(self, message, message_entity):
message.entities = [message_entity]
assert Filters.entity(message_entity.type)(message)
message.entities = []
assert not Filters.entity(MessageEntity.MENTION)(message)
second = message_entity.to_dict()
second['type'] = 'bold'
second = MessageEntity.de_json(second, None)
message.entities = [message_entity, second]
assert Filters.entity(message_entity.type)(message)
示例11: test_entities_filter
def test_entities_filter(self):
self.message.entities = [self.e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = []
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = [self.e(MessageEntity.BOLD)]
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = [self.e(MessageEntity.BOLD), self.e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
示例12: test_entities_filter
def test_entities_filter(self):
e = functools.partial(MessageEntity, offset=0, length=0)
self.message.entities = [e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = []
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = [e(MessageEntity.BOLD)]
self.assertFalse(Filters.entity(MessageEntity.MENTION)(self.message))
self.message.entities = [e(MessageEntity.BOLD), e(MessageEntity.MENTION)]
self.assertTrue(Filters.entity(MessageEntity.MENTION)(self.message))
示例13: test_and_or_filters
def test_and_or_filters(self):
self.message.text = 'test'
self.message.forward_date = True
self.assertTrue((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION))
)(self.message))
self.message.forward_date = False
self.assertFalse((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION)
))(self.message))
self.message.entities = [self.e(MessageEntity.MENTION)]
self.assertTrue((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION))
)(self.message))
self.assertEqual(
str((Filters.text & (Filters.forwarded | Filters.entity(MessageEntity.MENTION)))),
'<Filters.text and <Filters.forwarded or Filters.entity(mention)>>'
)
示例14: test_language_filter_multiple
def test_language_filter_multiple(self, message):
f = Filters.language(['en_US', 'da'])
message.from_user.language_code = 'en_US'
assert f(message)
message.from_user.language_code = 'en_GB'
assert not f(message)
message.from_user.language_code = 'da'
assert f(message)
示例15: test_language_filter_multiple
def test_language_filter_multiple(self):
f = Filters.language(['en_US', 'da'])
self.message.from_user.language_code = 'en_US'
self.assertTrue(f(self.message))
self.message.from_user.language_code = 'en_GB'
self.assertFalse(f(self.message))
self.message.from_user.language_code = 'da'
self.assertTrue(f(self.message))