-
Notifications
You must be signed in to change notification settings - Fork 1
MessageBuilder
MessageBuilder is a utility that allows you to build up large Telegram messages which automatically are broken up as needed into several smaller messages to fit within Telegram's size limits. Using is it simple:
- Create a MessageBuilder
- Add text items
- Call Send when done
Telefrag will append as many text items as it can in a single message. If it nears the maximum message size, it will fit as many items as possible, send the message, and begin a new message. You can force Telefrag to send what it has and start a new message by calling Break().
var builder = context.Builder(SendMode.Reply);
builder.Add("<b>Search Results</b>\n" as HTML);
// This collection could safely contain thousands of items
// without worry of exceeding message size limits
foreach(var r in results)
{
builder.Add($"- {r.Text}\n" as HTML);
}
builder.Add("End of results");
await builder.Send();var builder = context.Builder(SendMode.Message);
// Override the default destination chat
builder.Target = Settings.StaffChat;
builder.Add("<b>Security Alert</b>\n" as HTML);
string severity = null;
foreach(var a in alerts.OrderBy(a => a.Severity))
{
if (severity != a.Severity)
{
// For each grouping of severity, start a new message
await builder.Break();
// Write a group header
builder.Add($"Severity <b>{a.Severity.Safe()}</b> alerts:\n");
severity = a.Severity;
}
// Write the alert
builder.Add($"- {a.Text}"); // Type of a.Text will determine
// the ParseMode via FormattedString
}
// Send it
await builder.Send(SendOptions.DisableNotification);You can obtain a MessageBuilder object from any Context by calling the Builder method on that Context, specifying a SendMode based on how you want your message to be sent to the chat associated with the Context:
| Value | Description |
|---|---|
| Message | Sends the message as a standard message to Context.Chat
|
| Reply | Sends the message as a reply to Context.Message
|
The Target property can be used to change the target of the sent messages; by default the value of Context.Chat is used.
The ReplyTo property can be changed to override the message being replied to, if using SendMode.Reply. By default the value of Context.Message is used.
Call Add to add text items to the MessageBuilder. Items should be atomic articles of text that should not be broken up. Do not write all of the text into the MessageBuilder using a single call to Add as there will be no places for the MessageBuilder to break the message.
To explicitly begin a new message, call Break.
When finished building the message, await the call to the Send method. You can specify any SendOptions such as DisableNotifications.