-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotson.py
More file actions
1284 lines (1077 loc) · 45.6 KB
/
Copy pathbotson.py
File metadata and controls
1284 lines (1077 loc) · 45.6 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json as _json
import random
import time
from dataclasses import dataclass
from datetime import datetime, timedelta
from os import getenv
from pathlib import Path
from typing import Any, Awaitable, Callable, Iterable, Optional, Sequence, Union, Literal
import httpx
from aiogram import Bot, Dispatcher, F
from aiogram.filters import Command
from aiogram.types import CallbackQuery, InlineKeyboardMarkup, Message
from aiogram.types.input_file import FSInputFile
ChatId = Union[int, str]
EditTarget = Literal["previous", "message_id"]
DeleteTarget = Literal["context", "message_id"]
FileKind = Literal["photo", "video", "audio", "document", "voice", "animation", "sticker", "any"]
HandlerLike = Callable[[Any], Any]
Handler = Callable[[Any], Awaitable[None]]
Predicate = Callable[[Message], bool]
@dataclass(frozen=True)
class DownloadedFile:
file_id: str
file_unique_id: str
file_path: str
local_path: str
kind: str
AfterDownload = Callable[[DownloadedFile], None]
@dataclass(frozen=True)
class HttpResult:
status: int
headers: dict[str, str]
text: str
json: Any
url: str
method: str
@dataclass(frozen=True)
class CronSpec:
kind: Literal["daily", "weekly", "monthly", "yearly"]
hour: int
minute: int
weekday: Optional[int] = None
day: Optional[int] = None
month: Optional[int] = None
@dataclass
class CronJob:
spec: CronSpec
steps: list[Callable[["CronContext"], Awaitable[None]]]
class CronContext:
def __init__(self, app: "BotApp", bot: Bot) -> None:
self.app = app
self.bot = bot
def _has_text(message: Message) -> bool:
return bool(getattr(message, "text", None))
def _text(message: Message) -> str:
return getattr(message, "text", "") or ""
def _is_command(message: Message, name: str) -> bool:
t = _text(message).strip()
if not t.startswith("/"):
return False
cmd = t.split()[0][1:].split("@")[0]
return cmd == name.lstrip("/")
def _kind_pred(kind: str) -> Predicate:
k = (kind or "").strip().lower()
if k == "text":
return lambda m: bool(m.text)
if k == "photo":
return lambda m: bool(m.photo)
if k == "video":
return lambda m: bool(m.video)
if k == "audio":
return lambda m: bool(m.audio)
if k == "document":
return lambda m: bool(m.document)
if k == "voice":
return lambda m: bool(m.voice)
if k == "animation":
return lambda m: bool(m.animation)
if k == "sticker":
return lambda m: bool(m.sticker)
if k == "location":
return lambda m: bool(m.location)
if k == "contact":
return lambda m: bool(m.contact)
raise ValueError(f"Unknown kind: {kind}")
async def _delay(seconds: int) -> None:
s = int(seconds)
if s > 0:
await asyncio.sleep(s)
def _pick_text(items: Sequence[str]) -> str:
seq = [x for x in items if isinstance(x, str) and x.strip()]
if not seq:
raise ValueError("random_send requires a non-empty list of non-empty strings")
return random.choice(seq)
def _cb_message(cb: CallbackQuery) -> Optional[Message]:
return getattr(cb, "message", None)
def _ctx_chat_id(ctx: Any) -> int:
chat = getattr(ctx, "chat", None)
if chat is not None and getattr(chat, "id", None) is not None:
return int(chat.id)
msg = getattr(ctx, "message", None)
if msg is not None and getattr(msg, "chat", None) is not None:
return int(msg.chat.id)
raise RuntimeError("Context has no chat id")
def _ctx_user_id(ctx: Any) -> int:
fu = getattr(ctx, "from_user", None)
if fu is not None and getattr(fu, "id", None) is not None:
return int(fu.id)
msg = getattr(ctx, "message", None)
if msg is not None and getattr(msg, "from_user", None) is not None:
return int(msg.from_user.id)
raise RuntimeError("Context has no user id")
def _is_awaitable(x: Any) -> bool:
return hasattr(x, "__await__")
async def _run_user_callable(fn: Callable[[Any], Any], ctx: Any) -> None:
out = fn(ctx)
if _is_awaitable(out):
await out
def _wrap(fn: HandlerLike) -> Handler:
async def _h(ctx: Any) -> None:
await _run_user_callable(fn, ctx)
return _h
def _json_pick(obj: Any, path: str, default: Any = "") -> Any:
if obj is None:
return default
p = (path or "").strip()
if not p:
return default
if p.startswith("$."):
p = p[2:]
if p.startswith("$"):
p = p[1:]
if p.startswith("."):
p = p[1:]
if not p:
return default
cur = obj
for part in p.split("."):
if cur is None:
return default
if isinstance(cur, dict):
cur = cur.get(part, default)
elif isinstance(cur, list):
try:
i = int(part)
except Exception:
return default
if i < 0 or i >= len(cur):
return default
cur = cur[i]
else:
return default
return cur
class StateStore:
def __init__(self) -> None:
self._data: dict[tuple[int, int], dict[str, Any]] = {}
def _key(self, ctx: Any) -> tuple[int, int]:
return (_ctx_chat_id(ctx), _ctx_user_id(ctx))
def get(self, ctx: Any, key: str, default: Any = None) -> Any:
return self._data.get(self._key(ctx), {}).get(key, default)
def all(self, ctx: Any) -> dict[str, Any]:
return dict(self._data.get(self._key(ctx), {}))
def set(self, ctx: Any, key: str, value: Any) -> None:
k = self._key(ctx)
bucket = self._data.get(k)
if bucket is None:
bucket = {}
self._data[k] = bucket
bucket[key] = value
def drop(self, ctx: Any, key: str) -> None:
bucket = self._data.get(self._key(ctx))
if not bucket:
return
bucket.pop(key, None)
def clear(self, ctx: Any) -> None:
self._data.pop(self._key(ctx), None)
def inc(self, ctx: Any, key: str, step: int = 1) -> int:
k = self._key(ctx)
bucket = self._data.get(k)
if bucket is None:
bucket = {}
self._data[k] = bucket
cur = bucket.get(key, 0)
try:
cur_i = int(cur)
except Exception:
cur_i = 0
cur_i += int(step)
bucket[key] = cur_i
return cur_i
class StateAPI:
def __init__(self, store: StateStore) -> None:
self._s = store
def get(self, ctx: Any, key: str, default: Any = None) -> Any:
return self._s.get(ctx, key, default)
def all(self, ctx: Any) -> dict[str, Any]:
return self._s.all(ctx)
def set(self, key: str, value: Any) -> Handler:
async def _a(ctx: Any) -> None:
self._s.set(ctx, key, value)
return _a
def drop(self, key: str) -> Handler:
async def _a(ctx: Any) -> None:
self._s.drop(ctx, key)
return _a
def clear(self) -> Handler:
async def _a(ctx: Any) -> None:
self._s.clear(ctx)
return _a
def inc(self, key: str, step: int = 1) -> Handler:
async def _a(ctx: Any) -> None:
self._s.inc(ctx, key, step=step)
return _a
class HttpStore:
def __init__(self) -> None:
self._data: dict[tuple[int, int], HttpResult] = {}
def _key(self, ctx: Any) -> tuple[int, int]:
return (_ctx_chat_id(ctx), _ctx_user_id(ctx))
def set(self, ctx: Any, result: HttpResult) -> None:
self._data[self._key(ctx)] = result
def get(self, ctx: Any) -> Optional[HttpResult]:
return self._data.get(self._key(ctx))
def drop(self, ctx: Any) -> None:
self._data.pop(self._key(ctx), None)
class HttpAPI:
def __init__(self, store: HttpStore) -> None:
self._s = store
def last(self, ctx: Any) -> Optional[HttpResult]:
return self._s.get(ctx)
def clear(self, ctx: Any) -> None:
self._s.drop(ctx)
class HttpChain:
def __init__(
self,
app: "BotApp",
trigger: str,
params: dict,
method: str,
url: Union[str, Callable[[Any], str]],
*,
params_q: Optional[Union[dict[str, Any], Callable[[Any], dict[str, Any]]]] = None,
headers: Optional[Union[dict[str, str], Callable[[Any], dict[str, str]]]] = None,
json_body: Optional[Union[dict[str, Any], list[Any], str, Callable[[Any], Any]]] = None,
data: Optional[Union[dict[str, Any], str, bytes, Callable[[Any], Any]]] = None,
timeout: float = 10.0,
store_last: bool = True,
) -> None:
self._app = app
self._trigger = trigger
self._params = params
self._method = str(method).upper()
self._url = url
self._params_q = params_q
self._headers = headers
self._json_body = json_body
self._data = data
self._timeout = float(timeout)
self._store_last = bool(store_last)
self._steps: list[Callable[[Any, HttpResult], Awaitable[None]]] = []
def then(self, fn: Callable[[Any, HttpResult], Any]) -> "HttpChain":
async def _s(ctx: Any, result: HttpResult) -> None:
out = fn(ctx, result)
if _is_awaitable(out):
await out
self._steps.append(_s)
return self
def then_send_text(self, text: Union[str, Callable[[Any, HttpResult], str]]) -> "HttpChain":
async def _s(ctx: Any, result: HttpResult) -> None:
t = text(ctx, result) if callable(text) else str(text)
if isinstance(ctx, Message):
await ctx.answer(t)
self._steps.append(_s)
return self
def then_send_json(self, path: str, default: str = "") -> "HttpChain":
async def _s(ctx: Any, result: HttpResult) -> None:
v = _json_pick(result.json, path, default)
if isinstance(v, (dict, list)):
t = _json.dumps(v, ensure_ascii=False)
else:
t = str(v)
if isinstance(ctx, Message):
await ctx.answer(t)
self._steps.append(_s)
return self
def then_state_set_json(self, key: str, path: str, default: Any = None) -> "HttpChain":
async def _s(ctx: Any, result: HttpResult) -> None:
v = _json_pick(result.json, path, default)
self._app._state_store.set(ctx, key, v)
self._steps.append(_s)
return self
def done(self) -> "BotApp":
method = self._method
url = self._url
params_q = self._params_q
headers = self._headers
json_body = self._json_body
data = self._data
timeout = self._timeout
steps = list(self._steps)
store_last = self._store_last
async def _handler(ctx: Any) -> None:
u = url(ctx) if callable(url) else str(url)
pq = None
if params_q is not None:
pq = params_q(ctx) if callable(params_q) else dict(params_q)
hd = None
if headers is not None:
hd = headers(ctx) if callable(headers) else dict(headers)
jb = None
if json_body is not None:
jb = json_body(ctx) if callable(json_body) else json_body
dt = None
if data is not None:
dt = data(ctx) if callable(data) else data
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
resp = await client.request(
method=method,
url=u,
params=pq,
headers=hd,
json=jb,
data=dt,
)
txt = resp.text
try:
js = resp.json()
except Exception:
js = None
result = HttpResult(
status=int(resp.status_code),
headers={k: v for k, v in resp.headers.items()},
text=txt,
json=js,
url=str(resp.url),
method=method,
)
if store_last:
self._app._http_store.set(ctx, result)
for s in steps:
await s(ctx, result)
self._app._register_trigger(self._trigger, _handler, **self._params)
return self._app
class Node:
def __init__(self, app: "BotApp", trigger: str, **params) -> None:
self._app = app
self._trigger = trigger
self._params = params
def handle(self, handler: HandlerLike) -> "BotApp":
self._app._register_trigger(self._trigger, _wrap(handler), **self._params)
return self._app
def delay(self, seconds: int) -> "BotApp":
s = max(0, int(seconds))
async def _h(ctx: Any) -> None:
await _delay(s)
self._app._register_trigger(self._trigger, _h, **self._params)
return self._app
def send_message(self, text: str, **opts) -> "BotApp":
return self.handle(self._app.action_send_message(text, **opts))
def random_send(self, texts: Sequence[str], **opts) -> "BotApp":
return self.handle(self._app.action_random_send_message(texts, **opts))
def send_media(self, kind: str, media: str, caption: str = "", **opts) -> "BotApp":
return self.handle(self._app.action_send_media(kind, media, caption, **opts))
def send_photo(self, photo: str, caption: str = "", **opts) -> "BotApp":
return self.send_media("photo", photo, caption, **opts)
def send_video(self, video: str, caption: str = "", **opts) -> "BotApp":
return self.send_media("video", video, caption, **opts)
def send_audio(self, audio: str, caption: str = "", *, performer: str = "", title: str = "", **opts) -> "BotApp":
extra = dict(opts)
if performer:
extra["performer"] = performer
if title:
extra["title"] = title
return self.send_media("audio", audio, caption, **extra)
def send_file(self, file: str, caption: str = "", **opts) -> "BotApp":
return self.send_media("document", file, caption, **opts)
def send_animation(self, animation: str, caption: str = "", **opts) -> "BotApp":
return self.send_media("animation", animation, caption, **opts)
def send_location(self, latitude: float, longitude: float, **opts) -> "BotApp":
return self.handle(self._app.action_send_location(latitude=latitude, longitude=longitude, **opts))
def send_contact(self, phone_number: str, first_name: str, *, last_name: str = "", vcard: str = "", **opts) -> "BotApp":
return self.handle(self._app.action_send_contact(phone_number=phone_number, first_name=first_name, last_name=last_name, vcard=vcard, **opts))
def send_poll(self, question: str, options: Sequence[str], **opts) -> "BotApp":
return self.handle(self._app.action_send_poll(question=question, options=options, **opts))
def send_sticker(self, sticker: str, **opts) -> "BotApp":
return self.handle(self._app.action_send_sticker(sticker=sticker, **opts))
def send_game(self, *, game_type: str = "dice", **opts) -> "BotApp":
return self.handle(self._app.action_send_game(game_type=game_type, **opts))
def edit_text(self, new_text: str, **opts) -> "BotApp":
return self.handle(self._app.action_edit_text(new_text=new_text, **opts))
def edit_caption(self, new_caption: str, **opts) -> "BotApp":
return self.handle(self._app.action_edit_caption(new_caption=new_caption, **opts))
def forward_message(self, *, recipients: Iterable[ChatId], silent: bool = False, protect: bool = False) -> "BotApp":
return self.handle(self._app.action_forward_message(recipients=recipients, silent=silent, protect=protect))
def delete_message(self, **opts) -> "BotApp":
return self.handle(self._app.action_delete_message(**opts))
def show_activity(self, **opts) -> "BotApp":
return self.handle(self._app.action_show_activity(**opts))
def download_file(self, **opts) -> "BotApp":
return self.handle(self._app.action_download_file(**opts))
def state_set(self, key: str, value: Any) -> "BotApp":
return self.handle(self._app.state.set(key, value))
def state_drop(self, key: str) -> "BotApp":
return self.handle(self._app.state.drop(key))
def state_clear(self) -> "BotApp":
return self.handle(self._app.state.clear())
def state_inc(self, key: str, step: int = 1) -> "BotApp":
return self.handle(self._app.state.inc(key, step=step))
def http(
self,
*,
method: str,
url: Union[str, Callable[[Any], str]],
params: Optional[Union[dict[str, Any], Callable[[Any], dict[str, Any]]]] = None,
headers: Optional[Union[dict[str, str], Callable[[Any], dict[str, str]]]] = None,
json: Optional[Union[dict[str, Any], list[Any], str, Callable[[Any], Any]]] = None,
data: Optional[Union[dict[str, Any], str, bytes, Callable[[Any], Any]]] = None,
timeout: float = 10.0,
store_last: bool = True,
) -> HttpChain:
return HttpChain(
self._app,
self._trigger,
dict(self._params),
method,
url,
params_q=params,
headers=headers,
json_body=json,
data=data,
timeout=timeout,
store_last=store_last,
)
class CallbackNode:
def __init__(self, app: "BotApp", **params) -> None:
self._app = app
self._params = params
def handle(self, handler: HandlerLike) -> "BotApp":
self._app._register_callback(_wrap(handler), **self._params)
return self._app
def answer(self, text: str = "", *, alert: bool = False, cache_time: int = 0) -> "BotApp":
return self.handle(self._app.action_callback_answer(text=text, alert=alert, cache_time=cache_time))
def send_message(self, text: str, *, silent: bool = False, protect: bool = False) -> "BotApp":
return self.handle(self._app.action_callback_send_message(text=text, silent=silent, protect=protect))
def random_send(self, texts: Sequence[str], *, silent: bool = False, protect: bool = False) -> "BotApp":
return self.handle(self._app.action_callback_random_send(texts=texts, silent=silent, protect=protect))
def edit_text(self, new_text: str, *, parse_mode: Optional[str] = "HTML", reply_markup: Optional[InlineKeyboardMarkup] = None) -> "BotApp":
return self.handle(self._app.action_callback_edit_text(new_text=new_text, parse_mode=parse_mode, reply_markup=reply_markup))
def edit_caption(self, new_caption: str, *, parse_mode: Optional[str] = "HTML", reply_markup: Optional[InlineKeyboardMarkup] = None) -> "BotApp":
return self.handle(self._app.action_callback_edit_caption(new_caption=new_caption, parse_mode=parse_mode, reply_markup=reply_markup))
def delete_message(self) -> "BotApp":
return self.handle(self._app.action_callback_delete_message())
def show_activity(self, *, activity: str = "typing", seconds: int = 5) -> "BotApp":
return self.handle(self._app.action_callback_show_activity(activity=activity, seconds=seconds))
def state_set(self, key: str, value: Any) -> "BotApp":
return self.handle(self._app.state.set(key, value))
def state_drop(self, key: str) -> "BotApp":
return self.handle(self._app.state.drop(key))
def state_clear(self) -> "BotApp":
return self.handle(self._app.state.clear())
def state_inc(self, key: str, step: int = 1) -> "BotApp":
return self.handle(self._app.state.inc(key, step=step))
class CronNode:
def __init__(self, app: "BotApp", spec: CronSpec) -> None:
self._app = app
self._spec = spec
self._steps: list[Callable[[CronContext], Awaitable[None]]] = []
def handle(self, handler: HandlerLike) -> "BotApp":
self._steps.append(_wrap(handler))
self._app._cron_jobs.append(CronJob(self._spec, list(self._steps)))
return self._app
def send_message(
self,
*,
chat_id: ChatId,
text: str,
silent: bool = False,
protect: bool = False,
parse_mode: Optional[str] = None,
) -> "BotApp":
async def _a(ctx: CronContext) -> None:
await ctx.bot.send_message(
chat_id=chat_id,
text=text,
disable_notification=bool(silent),
protect_content=bool(protect),
parse_mode=parse_mode,
)
self._steps.append(_a)
self._app._cron_jobs.append(CronJob(self._spec, list(self._steps)))
return self._app
def http(
self,
*,
method: str,
url: str,
params: Optional[dict[str, Any]] = None,
headers: Optional[dict[str, str]] = None,
json: Optional[Any] = None,
data: Optional[Any] = None,
timeout: float = 10.0,
) -> "CronNode":
method_u = str(method).upper()
async def _a(_: CronContext) -> None:
async with httpx.AsyncClient(timeout=float(timeout), follow_redirects=True) as client:
await client.request(method=method_u, url=str(url), params=params, headers=headers, json=json, data=data)
self._steps.append(_a)
return self
class CronBuilder:
def __init__(self, app: "BotApp") -> None:
self._app = app
def daily(self, *, hour: int, minute: int = 0) -> CronNode:
return CronNode(self._app, CronSpec(kind="daily", hour=int(hour), minute=int(minute)))
def weekly(self, *, hour: int, minute: int = 0, weekday: int = 0) -> CronNode:
return CronNode(self._app, CronSpec(kind="weekly", hour=int(hour), minute=int(minute), weekday=int(weekday)))
def monthly(self, *, day: int = 1, hour: int = 9, minute: int = 0) -> CronNode:
return CronNode(self._app, CronSpec(kind="monthly", hour=int(hour), minute=int(minute), day=int(day)))
def yearly(self, *, month: int = 1, day: int = 1, hour: int = 9, minute: int = 0) -> CronNode:
return CronNode(self._app, CronSpec(kind="yearly", hour=int(hour), minute=int(minute), month=int(month), day=int(day)))
class BotApp:
_KIND_FILTERS = {
"text": F.text,
"location": F.location,
"contact": F.contact,
"document": F.document,
"photo": F.photo,
"video": F.video,
"audio": F.audio,
"sticker": F.sticker,
"voice": F.voice,
"animation": F.animation,
}
_MEDIA_METHODS = {
"photo": ("send_photo", "photo"),
"video": ("send_video", "video"),
"audio": ("send_audio", "audio"),
"document": ("send_document", "document"),
"animation": ("send_animation", "animation"),
}
_GAME_EMOJI = {
"dice": "🎲",
"cube": "🎲",
"darts": "🎯",
"basketball": "🏀",
"football": "⚽",
"soccer": "⚽",
"bowling": "🎳",
"slot": "🎰",
"slot_machine": "🎰",
}
_CHAT_ACTION = {
"typing": "typing",
"upload_photo": "upload_photo",
"upload_video": "upload_video",
"upload_audio": "upload_audio",
"upload_document": "upload_document",
"find_location": "find_location",
"record_video": "record_video",
"record_voice": "record_voice",
"record_audio": "record_audio",
}
def __init__(self, token: Optional[str] = None) -> None:
self.token = token or getenv("BOT_TOKEN")
if not self.token:
raise RuntimeError("BOT_TOKEN is not set")
self.dp = Dispatcher()
self._member_cache: dict[tuple[str, int], tuple[bool, float]] = {}
self._state_store = StateStore()
self.state = StateAPI(self._state_store)
self._http_store = HttpStore()
self.http = HttpAPI(self._http_store)
self._cron_jobs: list[CronJob] = []
def _targets(self, message: Message, recipients: Optional[Iterable[ChatId]]) -> list[ChatId]:
return list(recipients) if recipients else [message.chat.id]
def _file(self, src: str, from_path: bool):
return FSInputFile(src) if from_path else src
async def _send_to_many(self, message: Message, recipients: Optional[Iterable[ChatId]], send, **kwargs) -> None:
for chat_id in self._targets(message, recipients):
await send(chat_id=chat_id, **kwargs)
def _build_text_filter(self, filter: str, value: Optional[str]):
op = (filter or "any").strip().lower()
if op == "any":
return None
if op == "equal":
if value is None:
raise ValueError("value is required for filter='equal'")
return F.text == value
if op == "contains":
if value is None:
raise ValueError("value is required for filter='contains'")
return F.text.contains(value)
if op in ("not_contains", "not-contains"):
if value is None:
raise ValueError("value is required for filter='not_contains'")
return ~F.text.contains(value)
if op in ("starts", "starts_with", "starts-with"):
if value is None:
raise ValueError("value is required for filter='starts'")
return F.text.startswith(value)
if op == "regex":
if value is None:
raise ValueError("value is required for filter='regex'")
return F.text.regexp(value)
if op == "command":
if value is None:
raise ValueError("value is required for filter='command'")
return Command(value.lstrip("/"))
raise ValueError(f"Unknown text filter: {filter}")
def _build_callback_filter(self, filter: str, value):
op = (filter or "any").strip().lower()
if op == "any":
return None
if op in ("equal", "equals", "static"):
if value is None:
raise ValueError("value is required for callback filter='equal'")
return F.data == str(value)
if op in ("contains", "in"):
if value is None:
raise ValueError("value is required for callback filter='contains'")
return F.data.contains(str(value))
if op in ("starts", "starts_with", "startswith"):
if value is None:
raise ValueError("value is required for callback filter='starts'")
return F.data.startswith(str(value))
if op == "regex":
if value is None:
raise ValueError("value is required for callback filter='regex'")
return F.data.regexp(str(value))
if op in ("collection", "one_of", "in_list"):
if value is None:
raise ValueError("value is required for callback filter='collection'")
items = list(value) if isinstance(value, (list, tuple, set)) else [value]
items = [str(x) for x in items]
return F.data.in_(items)
raise ValueError(f"Unknown callback filter: {filter}")
def _register_trigger(self, trigger: str, handler: Handler, **params) -> None:
t = (trigger or "").strip().lower()
if t == "command":
name = params.get("name")
if not name:
raise ValueError("command trigger requires name")
self.dp.message.register(handler, Command(str(name)))
return
if t == "any":
self.dp.message.register(handler)
return
if t == "kind":
kind = (params.get("kind") or "").strip().lower()
flt = self._KIND_FILTERS.get(kind)
if flt is None:
raise ValueError(f"Unknown kind: {params.get('kind')}")
self.dp.message.register(handler, flt)
return
if t == "text":
flt = self._build_text_filter(params.get("filter", "any"), params.get("value"))
if flt is None:
self.dp.message.register(handler, F.text)
else:
self.dp.message.register(handler, F.text, flt)
return
if t == "reply":
ignore_commands = bool(params.get("ignore_commands", True))
flt_text = self._build_text_filter(params.get("filter", "any"), params.get("value"))
base = F.text
if ignore_commands:
base = base & ~F.text.startswith("/")
if flt_text is None:
self.dp.message.register(handler, base)
else:
self.dp.message.register(handler, base, flt_text)
return
raise ValueError(f"Unknown trigger: {trigger}")
def _register_callback(self, handler: Handler, **params) -> None:
flt = self._build_callback_filter(params.get("filter", "any"), params.get("value"))
if flt is None:
self.dp.callback_query.register(handler) # type: ignore[arg-type]
else:
self.dp.callback_query.register(handler, flt) # type: ignore[arg-type]
def action_send_message(self, text: str, *, recipients=None, silent=False, protect=False) -> Handler:
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
await self._send_to_many(
ctx,
recipients,
ctx.bot.send_message,
text=text,
disable_notification=bool(silent),
protect_content=bool(protect),
)
return _a
def action_random_send_message(self, texts: Sequence[str], *, recipients=None, silent=False, protect=False) -> Handler:
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
picked = _pick_text(texts)
await self._send_to_many(
ctx,
recipients,
ctx.bot.send_message,
text=picked,
disable_notification=bool(silent),
protect_content=bool(protect),
)
return _a
def action_send_media(self, kind: str, media: str, caption: str = "", *, recipients=None, silent=False, protect=False, from_path=False, **extra) -> Handler:
k = (kind or "").strip().lower()
spec = self._MEDIA_METHODS.get(k)
if spec is None:
raise ValueError(f"Unknown media kind: {kind}")
method_name, arg_name = spec
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
send = getattr(ctx.bot, method_name)
payload = {
arg_name: FSInputFile(media) if from_path else media,
"disable_notification": bool(silent),
"protect_content": bool(protect),
**extra,
}
if caption:
payload["caption"] = caption
await self._send_to_many(ctx, recipients, send, **payload)
return _a
def action_send_location(self, *, latitude: float, longitude: float, recipients=None, silent=False, protect=False) -> Handler:
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
await self._send_to_many(
ctx,
recipients,
ctx.bot.send_location,
latitude=latitude,
longitude=longitude,
disable_notification=bool(silent),
protect_content=bool(protect),
)
return _a
def action_send_contact(self, *, phone_number: str, first_name: str, last_name: str = "", vcard: str = "", recipients=None, silent=False, protect=False) -> Handler:
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
payload = {
"phone_number": phone_number,
"first_name": first_name,
"disable_notification": bool(silent),
"protect_content": bool(protect),
}
if last_name:
payload["last_name"] = last_name
if vcard:
payload["vcard"] = vcard
await self._send_to_many(ctx, recipients, ctx.bot.send_contact, **payload)
return _a
def action_send_poll(
self,
*,
question: str,
options: Sequence[str],
poll_type: str = "regular",
anonymous: bool = True,
multiple_answers: bool = False,
open_period: Optional[int] = None,
correct_option_id: Optional[int] = None,
explanation: str = "",
recipients=None,
silent=False,
protect=False,
) -> Handler:
pt = (poll_type or "regular").strip().lower()
pt = "quiz" if pt in ("quiz", "test") else "regular"
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
payload = {
"question": question,
"options": list(options),
"type": pt,
"is_anonymous": bool(anonymous),
"allows_multiple_answers": bool(multiple_answers),
"disable_notification": bool(silent),
"protect_content": bool(protect),
}
if open_period is not None:
payload["open_period"] = int(open_period)
if pt == "quiz":
if correct_option_id is not None:
payload["correct_option_id"] = int(correct_option_id)
if explanation:
payload["explanation"] = explanation
await self._send_to_many(ctx, recipients, ctx.bot.send_poll, **payload)
return _a
def action_send_sticker(self, *, sticker: str, recipients=None, silent=False, protect=False) -> Handler:
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
await self._send_to_many(
ctx,
recipients,
ctx.bot.send_sticker,
sticker=sticker,
disable_notification=bool(silent),
protect_content=bool(protect),
)
return _a
def action_send_game(self, *, game_type: str = "dice", recipients=None, silent=False, protect=False) -> Handler:
key = (game_type or "dice").strip().lower()
emoji = self._GAME_EMOJI.get(key)
if not emoji:
raise ValueError(f"Unknown game_type: {game_type}")
async def _a(ctx: Any) -> None:
if not isinstance(ctx, Message):
return
await self._send_to_many(
ctx,
recipients,
ctx.bot.send_dice,
emoji=emoji,
disable_notification=bool(silent),
protect_content=bool(protect),
)
return _a