Skip to content

Commit c865c3c

Browse files
committed
Updated ReadMe
1 parent 75c0dae commit c865c3c

File tree

1 file changed

+37
-72
lines changed

1 file changed

+37
-72
lines changed

README.md

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,37 @@ _Package is in preview state and theses are all in preview and may change later.
1717
See [Wiki](https://github.com/python-telegrambots/custom-telegrambots/wiki/) for more working examples.
1818

1919
```py
20-
import asyncio
21-
22-
from telegrambots.custom import TelegramBot, Dispatcher
23-
from telegrambots.custom.contexts import MessageContext
24-
2520
from telegrambots.custom import (
21+
TelegramBot,
22+
MessageContext,
2623
message_filters as mf,
2724
) # -> filters for each update type are in separate modules.
2825

29-
# you can easily add new filters by importing them and adding them to the list.
3026

3127
# Create main bot object, this object may contains all available api methods.
3228
bot = TelegramBot("BOT_TOKEN")
3329

34-
35-
# A function to handle errors inside handlers.
36-
async def handle_error(_: TelegramBot, e: Exception):
37-
print(e)
38-
39-
4030
# Dispatcher is to process updates and dispatches them to handlers.
41-
dp = Dispatcher(
42-
bot, handle_error
43-
) # By default, the dispatcher will process updates sequentially.
31+
dp = bot.dispatcher # By default, the dispatcher will process updates sequentially.
32+
33+
dp.add_default_exception_handler() # Simple err handler that prints error message.
4434

4535

4636
# Use decorator to register handler for each update type.
4737
# You can use filters and combine them.
48-
@dp.add.handlers.via_decorator.message(mf.regex("^/start") & mf.private)
38+
@dp.add.handlers.via_decorator.message(mf.Regex("^/start") & mf.private)
4939
async def handle_message(
5040
context: MessageContext,
5141
): # -> async callback function to handle update
5242
await context.reply_text(
5343
"Started"
5444
) # -> bound method for messages, only available in `MessageContext`
55-
await asyncio.sleep(5)
56-
await context.reply_text("Done")
57-
58-
59-
async def main():
60-
me = await bot.get_me()
61-
print(me.pretty_str()) # let's know who we are.
62-
63-
print("Streaming updates ...")
64-
# For now you should fetch updates manually and feed them to dispatcher.
65-
async for update in bot.stream_updates(["message"]):
66-
await dp.feed_update(update)
6745

6846

6947
if __name__ == "__main__":
7048
# Fire up the event loop.
71-
asyncio.run(main())
49+
dp.unlimited()
50+
7251
```
7352

7453
### Process updates in parallel
@@ -85,7 +64,6 @@ from src.telegrambots.custom.processor import ParallelProcessor
8564
# Dispatcher is to process updates and dispatches them to handlers.
8665
dp = Dispatcher(
8766
bot,
88-
handle_error=handle_error,
8967
processor_type=ParallelProcessor, # Now the dispatcher will use ParallelProcessor.
9068
# And it will process updates in parallel.
9169
)
@@ -132,12 +110,15 @@ class AdvancedMessageFilter(Filter[Message]):
132110

133111
def __check__(self, update: Optional[Message]) -> bool:
134112
# ---- check if update is a valid for your case ----
113+
self._set_metadata("balh", "Ablah")
135114
return True
136115

137116
# ---- or anything you like ----
138117

139118
# @dp.add.handlers.via_decorator.message(AdvancedMessageFilter())
140-
# ...
119+
# ---- sniff ----
120+
121+
context["balh"] # Ablah
141122
```
142123

143124
#### Using factories ( Fast, Low options )
@@ -215,7 +196,7 @@ So you need a way to keep track of a user, right?
215196

216197
Here's where this package mages!!
217198

218-
You can use `@context.continue_with_this_message` decorator inside your handler.
199+
You can use `@context.continue_with.this.message` decorator inside your handler.
219200

220201
It's much likely similar to `@dp.register_message_handler`, except it takes one more parameter before filter. And it's a `keys`.
221202

@@ -272,8 +253,8 @@ async def handle_message(
272253
if context.update.from_user:
273254

274255
# Again
275-
@context.continue_with.this.message(
276-
keys=[MessageSenderId(context.update.from_user.id)],
256+
@context.continue_with.this.message_from( # same as keys=[MessageSenderId(context.update from_user.id)],
257+
277258
filter=mf.text_message & mf.private,
278259
tag="give_age", # Another name, it's important!
279260
name=context.update.text, # -> you can pass custom data to handler. they're available in callback's *args or **kwargs.
@@ -369,6 +350,8 @@ Let's modify unrelated method.
369350
```py
370351
@dp.add.handlers.via_decorator.message(
371352
filter=mf.any_message, continue_after=["handle_message", "unrelated"] # notice we added this methods name to `continue_after`, so it can be continued with after itself ( user sends multiple unrelated updates in a row )
353+
354+
# You can use allow_continue_after_self = True, which dose the same.
372355
)
373356
async def unrelated(context: MessageContext):
374357
await context.reply_text("Please try again with a text message.")
@@ -397,28 +380,18 @@ now everything is ready! fast and clear.
397380
Let see full example
398381

399382
```py
400-
import asyncio
401-
from typing import Any
402-
import logging
403-
import sys
404-
405-
from telegrambots.custom import TelegramBot, Dispatcher
406-
from telegrambots.custom.contexts import MessageContext
407-
from telegrambots.custom import message_filters as mf
408-
from telegrambots.custom.processor import ParallelProcessor
409-
from telegrambots.custom.key_resolvers import MessageSenderId
410-
from telegrambots.custom.contexts import ContinueWithInfo
383+
from telegrambots.custom import (
384+
TelegramBot,
385+
message_filters as mf,
386+
ContinueWithInfo,
387+
MessageContext,
388+
)
411389

412390

413391
bot = TelegramBot("BOT_TOKEN")
392+
dp = bot.dispatcher
414393

415-
416-
# A function to handle errors inside handlers.
417-
async def handle_error(_: TelegramBot, e: Exception):
418-
print(e)
419-
420-
421-
dp = Dispatcher(bot, handle_error=handle_error)
394+
dp.add_default_exception_handler()
422395

423396

424397
@dp.add.handlers.via_decorator.message(
@@ -430,43 +403,35 @@ async def give_name(context: MessageContext):
430403

431404

432405
@dp.add.handlers.via_decorator.message(
433-
filter=mf.any_message & mf.private, continue_after=["handle_message", "unrelated"]
406+
filter=mf.any_message & mf.private,
407+
continue_after=["handle_message"],
408+
allow_continue_after_self=True,
434409
)
435410
async def unrelated(context: MessageContext):
436411
await context.reply_text("Please try again with a text message.")
412+
437413
if context.update.from_user:
438-
keys = [MessageSenderId(context.update.from_user.id)]
414+
user_id = context.update.from_user.id
439415
context.continue_with.many(
440-
ContinueWithInfo.with_message("give_name", keys, priority=1),
441-
ContinueWithInfo.with_message("unrelated", keys, priority=0),
416+
ContinueWithInfo.with_message_from("give_name", user_id, priority=1),
417+
ContinueWithInfo.with_message_from("unrelated", user_id, priority=0),
442418
)
443419

444420

445-
@dp.add.handlers.via_decorator.message(mf.regex("^/start") & mf.private)
421+
@dp.add.handlers.via_decorator.message(mf.Regex("^/start") & mf.private)
446422
async def handle_message(context: MessageContext):
447423
await context.reply_text("Please gimme you name ...")
448424

449425
if context.update.from_user:
450-
keys = [MessageSenderId(context.update.from_user.id)]
426+
user_id = context.update.from_user.id
451427
context.continue_with.many(
452-
ContinueWithInfo.with_message("give_name", keys, priority=1),
453-
ContinueWithInfo.with_message("unrelated", keys, priority=0),
428+
ContinueWithInfo.with_message_from("give_name", user_id, priority=1),
429+
ContinueWithInfo.with_message_from("unrelated", user_id, priority=0),
454430
)
455431

456432

457-
async def main():
458-
me = await bot.get_me()
459-
print(me.pretty_str()) # let's know who we are.
460-
461-
print("Streaming updates ...")
462-
# For now you should fetch updates manually and feed them to dispatcher.
463-
async for update in bot.stream_updates(["message", "callback_query"]):
464-
await dp.feed_update(update)
465-
466-
467433
if __name__ == "__main__":
468-
# Fire up the event loop.
469-
asyncio.run(main())
434+
dp.unlimited()
470435

471436
```
472437

0 commit comments

Comments
 (0)