Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/gobwas/glob v0.2.3
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/larksuite/oapi-sdk-go/v3 v3.5.3
github.com/mark3labs/mcp-go v0.47.1
github.com/mattn/go-sqlite3 v1.14.34
Expand All @@ -59,6 +60,7 @@ require (
go.opentelemetry.io/otel/sdk v1.42.0
go.opentelemetry.io/otel/sdk/metric v1.42.0
go.opentelemetry.io/otel/trace v1.42.0
go.uber.org/goleak v1.3.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/sqlite v1.6.0
Expand Down Expand Up @@ -117,7 +119,6 @@ require (
github.com/google/s2a-go v0.1.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.14 // indirect
github.com/googleapis/gax-go/v2 v2.18.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackmordaunt/icns/v3 v3.0.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion imbot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go 1.25.6
require (
github.com/bwmarrin/discordgo v0.29.0
github.com/go-telegram/bot v1.20.0
github.com/gorilla/websocket v1.5.3
github.com/larksuite/oapi-sdk-go/v3 v3.5.3
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1
github.com/slack-go/slack v0.19.0
Expand All @@ -20,7 +21,6 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mdp/qrterminal/v3 v3.2.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions imbot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -59,6 +60,7 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -67,8 +69,10 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.42.0 h1:UiKe+zDFmJobeJ5ggPwOshJIVt6/Ft0rcfrXZDLWAWY=
golang.org/x/term v0.42.0/go.mod h1:Dq/D+snpsbazcBG5+F9Q1n2rXV8Ma+71xEjTRufARgY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
166 changes: 166 additions & 0 deletions imbot/platform/tingly/wire/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Package wire defines the JSON-over-WebSocket protocol used between tingly
// platform participants: the tingly server, bots, and chat clients.
//
// The protocol is intentionally small. Every transmission is a Frame whose
// Kind discriminates the payload, plus a few routing fields lifted out of the
// payload for cheap dispatch (Bot, Chat).
//
// Direction matrix:
//
// bot → server: bot.send / bot.edit / bot.delete / bot.react
// chat → server: chat.send / chat.callback
// server→ bot: chat.send / chat.callback (forwarded from chats)
// server→ chat: bot.send / bot.edit / bot.delete / bot.react (forwarded)
// server→ either: welcome / ack / error
//
// All clients open with a Hello frame; the server replies with Welcome.
//
// Wire types live in imbot rather than the top-level tingly module because
// the bot transport must construct them and imbot is its own Go module.
package wire

import (
"encoding/json"

"github.com/tingly-dev/tingly-box/imbot/core"
)

// Version is bumped on incompatible wire changes.
const Version = 1

// Role identifies the connecting client's role in the Hello frame.
type Role string

const (
RoleBot Role = "bot"
RoleChat Role = "chat"
)

// Kind discriminates the payload carried in a Frame.
type Kind string

const (
KindHello Kind = "hello"
KindWelcome Kind = "welcome"
KindAck Kind = "ack"
KindError Kind = "error"

KindBotSend Kind = "bot.send"
KindBotEdit Kind = "bot.edit"
KindBotDelete Kind = "bot.delete"
KindBotReact Kind = "bot.react"

KindChatSend Kind = "chat.send"
KindChatCallback Kind = "chat.callback"
)

// Frame is the on-wire envelope. Data is decoded according to Kind.
type Frame struct {
Kind Kind `json:"kind"`
ID string `json:"id,omitempty"` // optional client request id; echoed in ack
Bot string `json:"bot,omitempty"` // bot UUID (always present after Hello)
Chat string `json:"chat,omitempty"` // chat ID (present on chat-scoped frames)
Data json.RawMessage `json:"data,omitempty"`
}

// Hello is the first frame sent by every client.
type Hello struct {
Version int `json:"version"`
Role Role `json:"role"`
BotID string `json:"botId"`
ChatID string `json:"chatId,omitempty"` // chat clients only
Token string `json:"token,omitempty"`
Sender *core.Sender `json:"sender,omitempty"` // chat clients only
// HistoryLimit asks the server to include up to N recent messages in
// Welcome. 0 means none.
HistoryLimit int `json:"historyLimit,omitempty"`
}

// Welcome is the server's response to Hello.
type Welcome struct {
Version int `json:"version"`
ServerID string `json:"serverId,omitempty"`
History []HistoryEntry `json:"history,omitempty"`
Now int64 `json:"now,omitempty"` // server unix seconds
}

// HistoryEntry is a stored event in chronological order. Frame is the
// rebuilt frame as it would appear on the wire (chat.send or bot.send/edit/
// delete/react).
type HistoryEntry struct {
Frame Frame `json:"frame"`
}

// Ack is sent by the server once a client frame is accepted. For bot/chat
// sends it carries the assigned MessageID and timestamp. For edits/deletes/
// reactions only the ID echo and timestamp are meaningful.
type Ack struct {
MessageID string `json:"messageId,omitempty"`
Timestamp int64 `json:"timestamp"`
}

// ErrorPayload is the payload of a KindError frame.
type ErrorPayload struct {
Code string `json:"code"`
Message string `json:"message"`
}

// BotSend is the payload for KindBotSend. Mirrors core.SendMessageOptions
// but kept independent so the wire shape is stable across imbot versions.
type BotSend struct {
Text string `json:"text,omitempty"`
Media []core.MediaAttachment `json:"media,omitempty"`
ParseMode core.ParseMode `json:"parseMode,omitempty"`
ReplyTo string `json:"replyTo,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}

// BotEdit payload.
type BotEdit struct {
MessageID string `json:"messageId"`
Text string `json:"text"`
}

// BotDelete payload.
type BotDelete struct {
MessageID string `json:"messageId"`
}

// BotReact payload.
type BotReact struct {
MessageID string `json:"messageId"`
Emoji string `json:"emoji"`
}

// ChatSend is the payload for KindChatSend (text or media from a chat).
type ChatSend struct {
Text string `json:"text,omitempty"`
Media []core.MediaAttachment `json:"media,omitempty"`
ChatType core.ChatType `json:"chatType,omitempty"`
Sender core.Sender `json:"sender"`
Metadata map[string]any `json:"metadata,omitempty"`
}

// ChatCallback is the payload for KindChatCallback (button click etc.).
type ChatCallback struct {
Sender core.Sender `json:"sender"`
CallbackData string `json:"callbackData"`
ChatType core.ChatType `json:"chatType,omitempty"`
}

// EncodeData JSON-encodes a payload struct into a Frame.Data slice. Errors
// are surfaced because malformed payloads here always indicate a bug.
func EncodeData(v any) (json.RawMessage, error) {
if v == nil {
return nil, nil
}
return json.Marshal(v)
}

// DecodeData JSON-decodes Frame.Data into the provided pointer.
func DecodeData(raw json.RawMessage, out any) error {
if len(raw) == 0 {
return nil
}
return json.Unmarshal(raw, out)
}
Loading