-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_system_extensions.py
More file actions
201 lines (166 loc) · 5.91 KB
/
response_system_extensions.py
File metadata and controls
201 lines (166 loc) · 5.91 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from __future__ import annotations
import typing
import aiogram
import aiogram.types
import aiogram.exceptions
import gls
import template
from template_for_aiogram import aiogram_syntax
from apps.botpiska.models import Employee
from response_system.core import globals_
from response_system.core.responses import TEditSuccessHandler, TBasicHandler, Response, TSendSuccessHandler, \
TFeedbackSuccessHandler, TNotifyEverySuccessHandler, TNotifyBasicSuccessHandler, handle, \
TNotifyCompletionHandler, action_edit, action_send, action_feedback, __debugging__
async def action_tmpl_notify(
path: str, context: dict,
receivers: typing.Sequence[int],
bot: aiogram.Bot,
include_chat_id: str, # Добавляет чат отправки в контекст
on_every_success: TNotifyEverySuccessHandler = None,
on_every_forbidden: TNotifyBasicSuccessHandler = None,
on_every_error: TNotifyBasicSuccessHandler = None,
on_completion: TNotifyCompletionHandler = None
):
if not include_chat_id:
message = template.render(path, context)
succeeded = 0
for chat in receivers:
if include_chat_id:
message = template.render(path, {
**{include_chat_id: chat},
**context
})
# noinspection PyBroadException,DuplicatedCode
try:
# noinspection PyUnboundLocalVariable
result = await message.send(chat, bot=bot)
await handle(on_every_success, result)
succeeded += 1
except Exception as error:
do_raise = __debugging__ and on_every_error is None
if isinstance(error, aiogram.exceptions.TelegramForbiddenError):
do_raise = __debugging__ and on_every_forbidden is None
await handle(on_every_forbidden, chat)
await handle(on_every_error, chat)
if do_raise:
raise
await handle(on_completion, succeeded, len(receivers))
# ---
def tmpl_edit(
path: str, context: dict,
original: aiogram.types.Message = None,
bot: aiogram.Bot = None,
on_success: TEditSuccessHandler = None,
on_forbidden: TBasicHandler = None,
on_error: TBasicHandler = None,
priority: int = 0
) -> Response:
response = Response()
response.add_action(
action_edit(
template.render(path, context, syntax=aiogram_syntax).extract(),
original or globals_.message_var.get(),
bot or aiogram.Bot.get_current(),
on_success=on_success,
on_forbidden=on_forbidden,
on_error=on_error
),
priority
)
return response
def tmpl_send(
path: str, context: dict,
chat: int = None,
bot: aiogram.Bot = None,
on_success: TSendSuccessHandler = None,
on_forbidden: TBasicHandler = None,
on_error: TBasicHandler = None,
priority: int = 2
) -> Response:
response = Response()
response.add_action(
action_send(
template.render(path, context, syntax=aiogram_syntax),
chat or globals_.message_var.get().chat.id,
bot or aiogram.Bot.get_current(),
on_success=on_success,
on_forbidden=on_forbidden,
on_error=on_error
),
priority
)
return response
def tmpl_feedback(
path: str, context: dict,
chat: int = None,
bot: aiogram.Bot = None,
on_success: TFeedbackSuccessHandler = None,
on_forbidden: TBasicHandler = None,
on_error: TBasicHandler = None,
on_delete: TBasicHandler = None,
priority: int = 3
) -> Response:
response = Response()
response.add_action(
action_feedback(
template.render(path, context, syntax=aiogram_syntax).extract(),
chat or globals_.message_var.get().chat.id,
bot or aiogram.Bot.get_current(),
on_success=on_success,
on_forbidden=on_forbidden,
on_error=on_error,
on_delete=on_delete
),
priority
)
return response
def tmpl_notify(
path: str, context: dict,
receivers: typing.Sequence[int],
bot: aiogram.Bot = None,
include_chat_id: str = None, # Добавляет чат отправки в контекст
on_every_success: TNotifyEverySuccessHandler = None,
on_every_forbidden: TNotifyBasicSuccessHandler = None,
on_every_error: TNotifyBasicSuccessHandler = None,
on_completion: TNotifyCompletionHandler = None,
priority: int = 4
) -> Response:
response = Response()
response.add_action(
action_tmpl_notify(
path, context,
receivers,
bot or aiogram.Bot.get_current(),
include_chat_id,
on_every_success=on_every_success,
on_every_forbidden=on_every_forbidden,
on_every_error=on_every_error,
on_completion=on_completion
),
priority
)
return response
def tmpl_notify_employee(
path: str, context: dict,
include_chat_id: str = None, # Добавляет чат отправки в контекст
on_every_success: TNotifyEverySuccessHandler = None,
on_every_forbidden: TNotifyBasicSuccessHandler = None,
on_every_error: TNotifyBasicSuccessHandler = None,
on_completion: TNotifyCompletionHandler = None,
priority: int = 4
) -> Response:
response = Response()
response.add_action(
action_tmpl_notify(
path, context,
Employee.get_all_chats(),
gls.operator_bot,
include_chat_id,
on_every_success=on_every_success,
on_every_forbidden=on_every_forbidden,
on_every_error=on_every_error,
on_completion=on_completion
),
priority
)
return response