Skip to content

Context

Nill edited this page Dec 28, 2021 · 2 revisions

Context is a (surprise surprise) context-bound object which can be used to reference a number of values which will be dependent on the current context, whether that be code executing in an event handler, or in a virtual/abstract method, or any number of ways Telefrag can invoke code. If you're at all familiar with ASP.NET, Context is similar to HttpContext.

Getting a Context Reference

  • You can call Context.Current to get the current Context
  • The current Context is passed into every event handler (see Events)

The Context Stack

Each time Telefrag dispatches or handles something, it pushes a ContextFrame onto the current Context. You can access the current frame using Context.CurrentFrame which exposes this ambient data:

Property Type Description
Time DateTime The time the frame was pushed onto the Context
User IUser The user, if known, performing the current action, otherwise null
Component string Describes the type of dispatch, such as "Event" or "Update"
StackTrace string The current .NET stacktrace of where the dispatch occurred
Activity Activity The current Activity, if there is one, otherwise null
Sender ISender The sender (Bot, Client, etc. instance) associated with the current execution (if applicable)
Parent ContextFrame Reference to the next frame in the stack (if any)

Additionally the Sender, User, Activity, and Component of the current Context frame are available directly via properties on Context itself:

public class Context
{
    public Activity Activity => CurrentFrame.Activity;
    public ISender Sender => CurrentFrame.Sender;
    public IUser User => CurrentFrame.User;
    public string Component => CurrentFrame.Component;
    ...
}

Other properties

Context also provides the following properties:

Property Type Description
Time DateTime The time the Context was created
ContextComponents Container DI container scoped for the current Context
Chat ISendTarget If the Context is related to a Telegram update, this is reference to the chat associated with that update (if applicable)
Message IMessage If the Context is related to a Telegram update, this is a reference to the message associated with that update (if applicable)

Methods

Method Signature Return Type Description
Builder() MessageBuilder Obtains a MessageBuilder that will ultimately send to the current Chat (possibly in reply to the current Message). See MessageBuilder for more info.
SendMessage(text, options) Task<IMessage> Shortcut for Context.Chat.SendMessage(); sends a basic message to the chat associated with the current Context
SendReply(text, options) Task<IMessage> Shortcut for Context.Message.SendReply(); sends a reply to the message associated with the current Context

Clone this wiki locally