shared

package
v0.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KeyboardBuiltin = &object.Builtin{
	Fn: func(ctx context.Context, kwargs object.Kwargs, args ...object.Object) object.Object {
		if err := errors.ExactArgs(args, 1); err != nil {
			return err
		}
		return args[0]
	},
	HelpText: `keyboard(rows) - Build a platform-agnostic button keyboard; rows is a list of lists of {text, data} or {text, url} dicts`,
}

KeyboardBuiltin is a free-standing (non-client-bound) builtin that passes rows through. Added directly to each platform library so it is available as telegram.keyboard(...) / discord.keyboard(...) without being bound to a client instance.

Functions

func BindToInstance

func BindToInstance(inst *object.Instance, builtins map[string]*object.Builtin)

BindToInstance injects all shared builtins as instance fields so scripts can call client.command(...), client.run(), etc. Each builtin is wrapped to prepend the instance as args[0], matching the module-level calling convention.

func BuildCtxDict

func BuildCtxDict(c *Ctx) *object.Dict

BuildCtxDict builds the Scriptling dict passed to handlers.

func EncodeBase64

func EncodeBase64(data []byte) string

EncodeBase64 encodes bytes to a base64 string.

func ParseCommand

func ParseCommand(text string) (cmd string, args []string)

ParseCommand splits text into command + args if it starts with "/".

func SharedBuiltins

func SharedBuiltins() map[string]*object.Builtin

SharedBuiltins returns the map of Scriptling builtins that are identical across all platforms.

Types

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot holds the routing table and dispatch logic shared across all platform clients. Platform clients embed *Bot and call dispatch after normalising a raw event.

func NewBot

func NewBot(s Sender) *Bot

NewBot creates a Bot bound to a Sender.

func (*Bot) Auth

func (b *Bot) Auth(h Handler)

Auth registers an auth handler. Return nil error to allow, non-nil to deny.

func (*Bot) Command

func (b *Bot) Command(name, helpText string, h Handler)

Command registers a command handler. name must start with "/".

func (*Bot) Dispatch

func (b *Bot) Dispatch(goCtx context.Context, u *Update) error

Dispatch routes a normalised Update to the appropriate registered handler. Called by the platform client after normalising a raw event.

func (*Bot) HelpText

func (b *Bot) HelpText() string

HelpText returns a formatted list of registered commands.

func (*Bot) OnCallback

func (b *Bot) OnCallback(prefix string, h Handler)

OnCallback registers a handler for callback (button) events whose data has the given prefix. Use "" to match all callbacks.

func (*Bot) OnFile

func (b *Bot) OnFile(h Handler)

OnFile registers a handler for file attachments.

func (*Bot) OnMessage

func (b *Bot) OnMessage(h Handler)

OnMessage registers the default handler for plain (non-command) text messages.

type Ctx

type Ctx struct {
	Update *Update
	Sender Sender
}

Ctx is passed to every handler. It wraps the Update and provides reply helpers so handlers don't need to manage dest/token themselves.

func (*Ctx) Answer

func (c *Ctx) Answer(goCtx context.Context, text string) error

Answer acknowledges a callback (button press). On Telegram: calls answerCallbackQuery (toast notification). On Discord: responds to the interaction (visible reply if text non-empty, silent ack if empty).

func (*Ctx) Download

func (c *Ctx) Download(goCtx context.Context) ([]byte, error)

Download fetches the file in this update and returns raw bytes.

func (*Ctx) Reply

func (c *Ctx) Reply(goCtx context.Context, text string, opts *SendOptions) error

Reply sends a text message back to the source of this update.

func (*Ctx) ReplyRich

func (c *Ctx) ReplyRich(goCtx context.Context, msg *RichMessage) error

ReplyRich sends a rich message back to the source of this update.

func (*Ctx) Typing

func (c *Ctx) Typing(goCtx context.Context) error

Typing sends a typing indicator.

type FileInfo

type FileInfo struct {
	ID       string
	Name     string
	MimeType string
	Size     int64
	URL      string // download URL (Discord/Slack); empty for Telegram (use ID)
}

FileInfo holds normalised file metadata.

type Handler

type Handler func(ctx context.Context, c *Ctx) error

Handler is a Go-level update handler.

type Keyboard

type Keyboard [][]KeyboardButton

Keyboard is a platform-agnostic button grid. Each inner slice is a row of buttons.

type KeyboardButton

type KeyboardButton struct {
	Text string
	Data string // callback_data (Telegram) / custom_id (Discord)
	URL  string // link button — opens URL instead of firing a callback
}

KeyboardButton is a single button in a platform-agnostic keyboard. If URL is set it is a link button; otherwise Data is the callback payload.

type RichMessage

type RichMessage struct {
	Title string // bold heading
	Body  string // main text
	Color string // "red", "green", "blue", hex "#ff0000" — Discord embed color; ignored elsewhere
	Image string // URL or file path — attached as photo/embed image
	URL   string // click-through link — Discord embed URL; ignored elsewhere
}

RichMessage is a platform-agnostic rich content message. Each client translates it to the native format.

type ScriptSender

type ScriptSender interface {
	Sender
	// Bot framework accessors — called by the shared library functions
	BotCommand(name, helpText string, h Handler)
	BotOnCallback(prefix string, h Handler)
	BotOnMessage(h Handler)
	BotOnFile(h Handler)
	BotAuth(h Handler)
	BotRun(ctx context.Context) error
}

ScriptSender is the interface the shared Scriptling library calls on the wrapped client instance. It mirrors Sender but operates on the Scriptling object.Instance that scripts hold.

func ClientFrom

func ClientFrom(args []object.Object) (ScriptSender, *object.Instance, bool)

ClientFrom extracts the ScriptSender from args[0] (the instance self).

type SendOptions

type SendOptions struct {
	ParseMode string
	Keyboard  *Keyboard
}

SendOptions carries optional send parameters.

type Sender

type Sender interface {
	SendMessage(ctx context.Context, dest, text string, opts *SendOptions) error
	SendRichMessage(ctx context.Context, dest string, msg *RichMessage) error
	EditMessage(ctx context.Context, dest, msgID, text string) error
	DeleteMessage(ctx context.Context, dest, msgID string) error
	SendFile(ctx context.Context, dest, source, fileName, caption string, isB64 bool) error
	SendTyping(ctx context.Context, dest string) error
	AckCallback(ctx context.Context, id, token, text string) error
	Download(ctx context.Context, ref string) ([]byte, error)
	Platform() string
	Capabilities() []string
}

Sender is the interface the shared bot calls back into the platform client for.

type Update

type Update struct {
	Dest          string
	MessageID     string
	UserID        string
	UserName      string
	Text          string
	Command       string   // "/start" if text begins with /, else ""
	Args          []string // words after the command
	IsCallback    bool
	CallbackID    string
	CallbackToken string // Discord/Slack interaction token; empty on Telegram
	CallbackData  string
	File          *FileInfo
}

Update is the normalised event passed to all handlers regardless of platform. Dest is the reply target — chat_id (Telegram) or channel_id (Discord/Slack) as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL