kutana.context module

class kutana.context.Context(app=None, config=None, update=None, backend=None)

Bases: object

Use Context.create for instantiation of Context object. This object can store ane attributes provided by routers or other plugins.

Variables:
  • app – Context’s kutana application.
  • update – Update currently being processed.
  • backend – Backend who created update.
  • config – Application’s configuration
  • default_target_id – Default receiver of responses
  • group_uid – Group’s id plus backend identity
  • user_uid – User’s id plus backend identity
  • group_state – State of group targeted by update
  • user_state – State of user targeted by update
static create(app=None, config=None, update=None, backend=None)

Create context with specified parameters.

get(name, default=None)
replace_method(method_name, method)

Replaces this instance’s method with specified name with passed method.

Example:

async def replacement(self, message, attachments=(), **kwargs):
    print(message)
ctx = Context()
ctx.replace_method("reply", replacement)
await ctx.reply("hey")  # will print "hey"

Note

Many methods are coroutine, so you should replace coroutines with coroutines and normal functions with functions.

Parameters:
  • method_name – method’s name to replace
  • method – method to replace with
reply(message, attachments=(), **kwargs)

Reply with specified message and attachments. Other kwargs are passed to the backend. Often as additional arguments to sending the message.

If message is too long - it will be splitted.

request(method, **kwargs)

Perform request to backend’s API and return response.

send_message(target_id, message, attachments=(), **kwargs)

Send message to specified target_id with specifiend message and attachments. Other kwargs are passed to the backend. Often as additional arguments to sending the message.

If message is too long - it will be splitted.

set_state(group_state=None, user_state=None)

Set group_state and/or user_state for current group and/or user. States can be represented by any string or None (if it shouldn’t change).

static split_large_text(text, length=4096)

Split text into chunks with specified length.

Parameters:
  • text – text for splitting
  • length – maximum size for one chunk
Returns:

tuple of chunks