-
-
Notifications
You must be signed in to change notification settings - Fork 408
/
matchers.py
167 lines (137 loc) · 6.14 KB
/
matchers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Decorator functions to use when creating skill modules."""
import logging
from opsdroid.helper import get_opsdroid
from opsdroid.web import Web
_LOGGER = logging.getLogger(__name__)
def match_regex(regex, case_sensitive=True):
"""Return regex match decorator."""
def matcher(func):
"""Add decorated function to skills list for regex matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"regex": {"expression": regex,
"case_sensitive": case_sensitive},
"skill": func,
"config":
opsdroid.loader.current_import_config})
return func
return matcher
def match_apiai_action(action):
"""Return Dialogflow action match decorator."""
def matcher(func):
"""Add decorated function to skills list for Dialogflow matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"dialogflow_action": action, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
_LOGGER.warning("Api.ai is now called Dialogflow, this matcher "
"will stop working in the future. "
"Use match_dialogflow_action instead.")
return matcher
def match_apiai_intent(intent):
"""Return Dialogflow intent match decorator."""
def matcher(func):
"""Add decorated function to skills list for Dialogflow matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"dialogflow_intent": intent, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
_LOGGER.warning("Api.ai is now called Dialogflow, this matcher "
"will stop working in the future. "
"Use match_dialogflow_intent instead.")
return matcher
def match_dialogflow_action(action):
"""Return Dialogflowi action match decorator."""
def matcher(func):
"""Add decorated function to skills list for Dialogflow matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"dialogflow_action": action, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
return matcher
def match_dialogflow_intent(intent):
"""Return Dialogflow intent match decorator."""
def matcher(func):
"""Add decorated function to skills list for Dialogflow matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"dialogflow_intent": intent, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
return matcher
def match_luisai_intent(intent):
"""Return luisai intent match decorator."""
def matcher(func):
"""Add decorated function to skills list for luisai matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"luisai_intent": intent, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
return matcher
def match_recastai(intent):
"""Return recastai intent match decorator."""
def matcher(func):
"""Add decorated function to skills list for recastai matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"recastai_intent": intent, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
return matcher
def match_witai(intent):
"""Return witai intent match decorator."""
def matcher(func):
"""Add decorated function to skills list for witai matching."""
opsdroid = get_opsdroid()
opsdroid.skills.append({"witai_intent": intent, "skill": func,
"config":
opsdroid.loader.current_import_config})
return func
return matcher
def match_crontab(crontab, timezone=None):
"""Return crontab match decorator."""
def matcher(func):
"""Add decorated function to skills list for crontab matching."""
opsdroid = get_opsdroid()
config = opsdroid.loader.current_import_config
opsdroid.skills.append({"crontab": crontab, "skill": func,
"config": config, "timezone": timezone})
return func
return matcher
def match_webhook(webhook):
"""Return webhook match decorator."""
def matcher(func):
"""Add decorated function to skills list for webhook matching."""
opsdroid = get_opsdroid()
config = opsdroid.loader.current_import_config
opsdroid.skills.append({"webhook": webhook, "skill": func,
"config": config})
async def wrapper(req, opsdroid=opsdroid, config=config):
"""Wrap up the aiohttp handler."""
_LOGGER.info("Running skill %s via webhook", webhook)
opsdroid.stats["webhooks_called"] = \
opsdroid.stats["webhooks_called"] + 1
await func(opsdroid, config, req)
return Web.build_response(200, {"called_skill": webhook})
opsdroid.web_server.web_app.router.add_post(
"/skill/{}/{}".format(config["name"], webhook), wrapper)
opsdroid.web_server.web_app.router.add_post(
"/skill/{}/{}/".format(config["name"], webhook), wrapper)
return func
return matcher
def match_always(func=None):
"""Return always match decorator."""
def matcher(func):
"""Add decorated function to skills list for always matching."""
opsdroid = get_opsdroid()
config = opsdroid.loader.current_import_config
opsdroid.skills.append({"always": True, "skill": func,
"config": config})
return func
# Allow for decorator with or without parenthesis as there are no args.
if callable(func):
return matcher(func)
return matcher