diff --git a/internal/im/yunzhijia/adapter.go b/internal/im/yunzhijia/adapter.go index 0a1a55cb..a1ff9777 100644 --- a/internal/im/yunzhijia/adapter.go +++ b/internal/im/yunzhijia/adapter.go @@ -22,6 +22,9 @@ import ( // textMessageType is the Yunzhijia message type value for plain text messages. const textMessageType = 2 +// markdownFormatType requests Yunzhijia to render Content as Markdown. +const markdownFormatType = "markdown" + // Compile-time check. var _ im.Adapter = (*Adapter)(nil) @@ -205,6 +208,19 @@ func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, r payload := sendMessagePayload{ MsgType: textMessageType, Content: reply.Content, + // WeKnora replies are authored in Markdown by default (see im.ReplyMessage.Content), + // so request Markdown rendering from Yunzhijia unless explicitly overridden via + // reply.Extra["yunzhijia_format_type"] (empty string disables the param entirely). + Param: &sendMessageParam{FormatType: markdownFormatType}, + } + if reply.Extra != nil { + if formatType, ok := reply.Extra["yunzhijia_format_type"]; ok { + if formatType == "" { + payload.Param = nil + } else { + payload.Param = &sendMessageParam{FormatType: formatType} + } + } } // When groupType == 3, don't set notifyParams (per reference implementation). diff --git a/internal/im/yunzhijia/adapter_test.go b/internal/im/yunzhijia/adapter_test.go index 437df0df..72ca2243 100644 --- a/internal/im/yunzhijia/adapter_test.go +++ b/internal/im/yunzhijia/adapter_test.go @@ -92,4 +92,46 @@ func TestSendReplyAcceptsAny2xxAndBuildsPayload(t *testing.T) { if len(payload.NotifyParams) != 1 || len(payload.NotifyParams[0].Values) != 1 || payload.NotifyParams[0].Values[0] != "user" { t.Fatalf("notify params = %#v", payload.NotifyParams) } + if payload.Param == nil || payload.Param.FormatType != markdownFormatType { + t.Fatalf("expected markdown format param by default, got %#v", payload.Param) + } +} + +func TestSendReplyFormatTypeOverride(t *testing.T) { + adapter := NewAdapter("https://www.yunzhijia.com/send", "", 10, "yunzhijia.com") + var payload sendMessagePayload + adapter.httpClient = &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { + if err := json.NewDecoder(req.Body).Decode(&payload); err != nil { + t.Fatalf("decode payload: %v", err) + } + return &http.Response{ + StatusCode: http.StatusNoContent, + Body: io.NopCloser(bytes.NewReader(nil)), + Header: make(http.Header), + }, nil + })} + + incoming := &im.IncomingMessage{UserID: "user"} + + // Empty string explicitly disables the param. + if err := adapter.SendReply(context.Background(), incoming, &im.ReplyMessage{ + Content: "answer", + Extra: map[string]string{"yunzhijia_format_type": ""}, + }); err != nil { + t.Fatalf("SendReply() error = %v", err) + } + if payload.Param != nil { + t.Fatalf("expected no param when format type overridden to empty, got %#v", payload.Param) + } + + // Non-empty override replaces the default markdown formatType. + if err := adapter.SendReply(context.Background(), incoming, &im.ReplyMessage{ + Content: "answer", + Extra: map[string]string{"yunzhijia_format_type": "text"}, + }); err != nil { + t.Fatalf("SendReply() error = %v", err) + } + if payload.Param == nil || payload.Param.FormatType != "text" { + t.Fatalf("expected overridden format type 'text', got %#v", payload.Param) + } } diff --git a/internal/im/yunzhijia/types.go b/internal/im/yunzhijia/types.go index 8aefd0e4..d154b197 100644 --- a/internal/im/yunzhijia/types.go +++ b/internal/im/yunzhijia/types.go @@ -15,9 +15,10 @@ type callbackMessage struct { // sendMessagePayload is the JSON body POSTed to sendMsgUrl to reply. type sendMessagePayload struct { - MsgType int `json:"msgtype"` - Content string `json:"content"` - NotifyParams []notifyParam `json:"notifyParams,omitempty"` + MsgType int `json:"msgtype"` + Content string `json:"content"` + NotifyParams []notifyParam `json:"notifyParams,omitempty"` + Param *sendMessageParam `json:"param,omitempty"` } // notifyParam specifies recipients in a Yunzhijia send message request. @@ -25,3 +26,9 @@ type notifyParam struct { Type string `json:"type"` Values []string `json:"values"` } + +// sendMessageParam carries extra rendering options for a Yunzhijia reply, +// such as requesting Markdown rendering of Content via formatType. +type sendMessageParam struct { + FormatType string `json:"formatType,omitempty"` +}