teleproto - v1.229.0
    Preparing search index...

    Class ClientUpdates

    The update pipeline of a client, reachable as client.updates.

    Handlers form one chain: next() passes the update on, returning without it consumes the update. on matches first, by raw schema name or by an event builder; watch also keeps Telegram sending updates for the chats it is given.

    A raw name gives the untouched Api.TypeUpdate, a builder gives its event object. Direct and small-group messages arrive as the updateShortMessage containers, which are not part of the Update union; the chain expands them, so "newMessage" sees them too.

    Each update carries a state object for middleware to leave things in. Throws go to catch, or to the client's error handler.

    client.addEventHandler keeps working and runs before this chain; a StopPropagation there ends that loop only.

    client.updates.use(async (update, next) => {
    const started = Date.now();
    await next();
    console.log(`${update.className} handled in ${Date.now() - started}ms`);
    });

    client.updates.on("newChannelMessage", (update) => {
    console.log(update.message.id);
    }, { chats: ["durov"] });

    client.updates.on(new NewMessage({ pattern: /^/start/ }), (event) =>
    event.message.reply({ message: "hi" }));

    const stop = client.updates.watch("obitoscasino", (update) =>
    console.log(update.message.id));
    Index
    • Installs the handler for anything thrown inside the chain.

      Parameters

      • handler: (error: Error, update?: AnyUpdate) => unknown

      Returns this

    • Handles updates of the given raw names, or the events of a builder.

      A raw name gives the untouched update typed as its schema class; a builder gives that builder's event object, as addEventHandler does. An array of handlers runs in order, each deciding with next() whether the rest of them run.

      Type Parameters

      Parameters

      Returns Unsubscribe

      A function that removes the handler.

      client.updates.on("newChannelMessage", (update) => console.log(update.message.id));
      client.updates.on(["newMessage", "newChannelMessage"], handler);
      client.updates.on("botCallbackQuery", [checkAuth, handleClick]);
      client.updates.on("newChannelMessage", handler, { chats: ["durov"] });
      client.updates.on(new NewMessage({}), (event) => event.message.reply({ message: "hi" }));
    • Handles updates of the given raw names, or the events of a builder.

      A raw name gives the untouched update typed as its schema class; a builder gives that builder's event object, as addEventHandler does. An array of handlers runs in order, each deciding with next() whether the rest of them run.

      Type Parameters

      • T = any

      Parameters

      Returns Unsubscribe

      A function that removes the handler.

      client.updates.on("newChannelMessage", (update) => console.log(update.message.id));
      client.updates.on(["newMessage", "newChannelMessage"], handler);
      client.updates.on("botCallbackQuery", [checkAuth, handleClick]);
      client.updates.on("newChannelMessage", handler, { chats: ["durov"] });
      client.updates.on(new NewMessage({}), (event) => event.message.reply({ message: "hi" }));
    • Handles updates from the given chats only, and keeps them coming.

      Telegram streams channel updates to a session only while it keeps the channel open, so for channels the account is not a member of this is the difference between receiving their messages and receiving nothing. Chats that need no such subscription are simply filtered.

      Synchronous like on: resolving the chats and subscribing happen in the background, waiting for the client to connect, so it can be called before start(). A reconnect re-subscribes on its own. The poll runs at the interval Telegram names in the difference, falling back to the client's channelPollInterval.

      Parameters

      • chats: EntityLike | EntityLike[]

        Chats to listen to: usernames, ids or entities.

      • Optionalhandler: UpdateMiddleware<any> | UpdateMiddleware<any>[]

        Optional handler; without it only the subscription is kept.

      • options: WatchOptions = {}

        Which updates to handle, see WatchOptions.

      Returns Unsubscribe

      A function that stops watching and removes the handler.

      const stop = client.updates.watch(
      ["obitoscasino", "toporlive"],
      (update) => console.log(update.message.id),
      );
      stop();