App и Bot¶
App¶
App
dataclass
¶
App(
prefixes=list(),
name="VK Flow App",
filter=None,
commands=list(),
event_handlers=(lambda: defaultdict(list))(),
message_handlers=list(),
startup_handlers=list(),
shutdown_handlers=list(),
button_onclick_handlers=dict(),
button_callback_handlers=dict(),
inviting_handlers=list(),
fsm_storage=None,
fsm_strategy="user_chat",
packages=list(),
debug=False,
strict_mode=False,
description="Чат-бот для ВКонтакте, написанный на Python с использованием VK Flow",
addons=list(),
payload_factory=None,
experimental=dict(),
)
Bases: Package, Generic[AppPayloadFieldTypevar]
run
¶
Source code in vkflow\app\bot.py
start
async
¶
Source code in vkflow\app\bot.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
startup
async
¶
shutdown
async
¶
add_cog
async
¶
Source code in vkflow\app\bot.py
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 | |
remove_cog
async
¶
Source code in vkflow\app\bot.py
get_cog
¶
load_extension
async
¶
Source code in vkflow\app\bot.py
unload_extension
async
¶
Source code in vkflow\app\bot.py
reload_extension
async
¶
Source code in vkflow\app\bot.py
get_context
async
¶
Create a Context for the given message.
Override this method in a subclass to use a custom Context class or to add custom attributes/initialization to the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
NewMessage
|
The NewMessage that triggered the command |
required |
cls
|
type | None
|
The Context class to use. Defaults to Context from ext.commands. |
None
|
command
|
Any
|
The Command being invoked |
None
|
prefix
|
str | None
|
The prefix used to invoke the command |
None
|
invoked_with
|
str | None
|
The name/alias used to invoke the command |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Context instance |
Example
class MyContext(commands.Context): @property def db(self): return self.app.database
class MyApp(App): async def get_context(self, message, , cls=MyContext, kwargs): return await super().get_context(message, cls=cls, *kwargs)
Source code in vkflow\app\bot.py
wait_for
async
¶
Source code in vkflow\app\bot.py
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 | |
dispatch_event
async
¶
Source code in vkflow\app\bot.py
get_fsm
¶
Get FSMContext for a message context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
NewMessage | CallbackButtonPressed
|
NewMessage or CallbackButtonPressed |
required |
strategy
|
str
|
Key generation strategy |
'user_chat'
|
Returns:
| Type | Description |
|---|---|
Any
|
FSMContext instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If FSM storage is not configured |
Example
@app.command("start_order") async def start_order(ctx: NewMessage): fsm = app.get_fsm(ctx) await fsm.set_state(OrderStates.waiting_name) await ctx.reply("Enter your name:")
Source code in vkflow\app\bot.py
set_fsm_storage
¶
Set global FSM storage for App-level state decorators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
Any
|
BaseStorage instance (e.g., MemoryStorage) |
required |
Example
from vkflow.app.fsm import MemoryStorage
app = App(prefixes=["/"]) app.set_fsm_storage(MemoryStorage())
@app.state(OrderStates.waiting_name) async def handle_name(ctx, msg): ...
Source code in vkflow\app\bot.py
include_fsm_router
¶
Include an FSM router for state handling.
FSM routers are checked before regular command routing. If a router handles a message, command routing is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
Any
|
FSMRouter instance |
required |
Example
from vkflow.app.fsm import FSMRouter, MemoryStorage
storage = MemoryStorage() router = FSMRouter(storage)
@router.state(OrderStates.waiting_name) async def handle_name(ctx, msg): ...
app.include_fsm_router(router)
Source code in vkflow\app\bot.py
state
¶
Decorator to register an FSM state handler at App level.
Requires set_fsm_storage() to be called first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_obj
|
Any
|
State object or state name string |
required |
strategy
|
str
|
Key generation strategy |
'user_chat'
|
Returns:
| Type | Description |
|---|---|
Callable
|
Decorator function |
Example
app.set_fsm_storage(MemoryStorage())
@app.state(OrderStates.waiting_name) async def handle_name(ctx, msg): await ctx.update_data(name=msg.msg.text) await ctx.set_state(OrderStates.waiting_phone) await msg.answer("Enter phone:")
Source code in vkflow\app\bot.py
add_addon
¶
get_addon
¶
on_command_error
async
¶
Called when any command raises an error, regardless of other handlers.
This is informational and does NOT affect the error handling flow. Override this method to add global logging, metrics, or notifications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
NewMessage | CallbackButtonPressed
|
The command context (NewMessage or Context) |
required |
error
|
Exception
|
The exception that was raised |
required |
Example
class MyApp(App): async def on_command_error(self, ctx, error): print(f"Error: {error}") await self.send_error_to_admin(error)
Source code in vkflow\app\bot.py
on_command_error_fallback
async
¶
Called when a command error is not handled by any error handler.
This is the last fallback before the error is re-raised. Override this method to handle all otherwise unhandled errors. If this method completes without raising, the error is considered handled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
NewMessage | CallbackButtonPressed
|
The command context (NewMessage or Context) |
required |
error
|
Exception
|
The exception that was raised |
required |
Example
class MyApp(App): async def on_command_error_fallback(self, ctx, error): await ctx.reply(f"Произошла ошибка: {error}")
Source code in vkflow\app\bot.py
wait_until_ready
async
¶
Source code in vkflow\app\bot.py
run_when_ready
¶
Source code in vkflow\app\bot.py
Bot¶
Bot
dataclass
¶
Bases: Generic[AppPayloadFieldTypevar, BotPayloadFieldTypevar]
via_token
async
classmethod
¶
Source code in vkflow\app\bot.py
run_polling
async
¶
Source code in vkflow\app\bot.py
handle_event
async
¶
Source code in vkflow\app\bot.py
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 | |