NimarkoGram Docs
RU
Docs/Multiple accounts

Multiple accounts

Choose the right account in a callback and keep its preferences separate.

On this pageGet the account from the callbackStore a per-user preferenceTry two accounts

Get the account from the callback

The engine passes an account slot to on_send_message_hook(self, account, params). In a menu callback, read context.get("account"). Zero is a valid slot. Testing if account: skips the first account. Use is None when the context might omit it.

A message menu may supply message. A chat may supply dialog_id. The keys depend on where the action was invoked. Check each optional value before using the Java object. To identify a chat across accounts, keep both the signed-in user ID and the dialog ID.

Store a per-user preference

The settings store uses the plugin ID and the preference key. A slot can be reused after logout. Use the signed-in user ID for a preference that survives restarts. Download the counter plugin, install it and enable it. The chat menu now includes Count taps. The full source follows.

Python
__id__ = "sdk_tap_count"
__name__ = "Count taps"
__version__ = "1.0"
__host__ = "NimarkoGram"
__sdk_version__ = ">=1.0"
__author__ = "NimarkoGram"

from base_plugin import BasePlugin, MenuItemData, MenuItemType
from client_utils import get_user_config
from ui.bulletin import BulletinHelper


class TapCounter(BasePlugin):
    def on_plugin_load(self):
        self.add_menu_item(MenuItemData(
            menu_type=MenuItemType.CHAT_ACTION_MENU,
            text=__name__,
            on_click=self.count_taps,
        ))

    def count_taps(self, context):
        account = context.get("account")
        if account is None:
            return
        user_id = get_user_config(account).getClientUserId()
        if not user_id:
            return
        key = f"taps_{user_id}"
        count = self.get_setting(key, 0) + 1
        self.set_setting(key, count)
        BulletinHelper.show_info(str(count))

Try two accounts

Start with two accounts that have never used this counter. Tap the action twice in the first account. The first tap in the second should show 1. Return to the first and tap again to see 3. If the count is shared, inspect the key and the argument to get_user_config(account). When deferring work, capture the same account in your callback so a change of active screen cannot change the destination.