feat(im): default markdown format for yunzhijia replies
SendReply now sets param.formatType=markdown by default since WeKnora replies are authored in Markdown. Callers can override via reply.Extra["yunzhijia_format_type"] (empty string disables the param).
This commit is contained in:
@@ -22,6 +22,9 @@ import (
|
|||||||
// textMessageType is the Yunzhijia message type value for plain text messages.
|
// textMessageType is the Yunzhijia message type value for plain text messages.
|
||||||
const textMessageType = 2
|
const textMessageType = 2
|
||||||
|
|
||||||
|
// markdownFormatType requests Yunzhijia to render Content as Markdown.
|
||||||
|
const markdownFormatType = "markdown"
|
||||||
|
|
||||||
// Compile-time check.
|
// Compile-time check.
|
||||||
var _ im.Adapter = (*Adapter)(nil)
|
var _ im.Adapter = (*Adapter)(nil)
|
||||||
|
|
||||||
@@ -205,6 +208,19 @@ func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, r
|
|||||||
payload := sendMessagePayload{
|
payload := sendMessagePayload{
|
||||||
MsgType: textMessageType,
|
MsgType: textMessageType,
|
||||||
Content: reply.Content,
|
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).
|
// When groupType == 3, don't set notifyParams (per reference implementation).
|
||||||
|
|||||||
@@ -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" {
|
if len(payload.NotifyParams) != 1 || len(payload.NotifyParams[0].Values) != 1 || payload.NotifyParams[0].Values[0] != "user" {
|
||||||
t.Fatalf("notify params = %#v", payload.NotifyParams)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,10 @@ type callbackMessage struct {
|
|||||||
|
|
||||||
// sendMessagePayload is the JSON body POSTed to sendMsgUrl to reply.
|
// sendMessagePayload is the JSON body POSTed to sendMsgUrl to reply.
|
||||||
type sendMessagePayload struct {
|
type sendMessagePayload struct {
|
||||||
MsgType int `json:"msgtype"`
|
MsgType int `json:"msgtype"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
NotifyParams []notifyParam `json:"notifyParams,omitempty"`
|
NotifyParams []notifyParam `json:"notifyParams,omitempty"`
|
||||||
|
Param *sendMessageParam `json:"param,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// notifyParam specifies recipients in a Yunzhijia send message request.
|
// notifyParam specifies recipients in a Yunzhijia send message request.
|
||||||
@@ -25,3 +26,9 @@ type notifyParam struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Values []string `json:"values"`
|
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"`
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user