diff --git a/README.md b/README.md index d34f4ef..c91de99 100644 --- a/README.md +++ b/README.md @@ -65,10 +65,12 @@ robot.Rec.SetGroupEvent(receiver.EventDeleteFromChat, func(event *receiver.CallE * [ ] 发送NewsNotice模版卡片 * [ ] 发送Button模版卡片 * [ ] 发送Vote模版卡片 +* [x] 发送多项选择模版卡片 ### 接收 * [x] 回调校验 * [x] 回调处理,包括单聊和群聊 * [x] 事件处理 +* [ ] 卡片事件回调 ### 其他 * [x] 多机器人管理 * [x] http server diff --git a/wxprotoc/receiver/receiver.go b/wxprotoc/receiver/receiver.go index fe33a19..698d725 100644 --- a/wxprotoc/receiver/receiver.go +++ b/wxprotoc/receiver/receiver.go @@ -114,9 +114,6 @@ func (r *receiver) passiveReply(event *CallEvent) ([]byte, error) { } func (r *receiver) doPassiveReply(content string, replyType PassiveReplyType) ([]byte, error) { - if len(content) == 0 { - return nil, fmt.Errorf("PassiveReply content is empty") - } var reply PassiveReplyResp switch replyType { diff --git a/wxprotoc/receiver/reply_model.go b/wxprotoc/receiver/reply_model.go index 50401ea..3c26d20 100644 --- a/wxprotoc/receiver/reply_model.go +++ b/wxprotoc/receiver/reply_model.go @@ -53,10 +53,7 @@ func (br *baseReply) MsgType() []MsgType { } func (br *baseReply) SupportPassiveReply() bool { - if br.passiveReply != nil { - return true - } - return false + return br.passiveReply != nil } func (br *baseReply) PassiveReply(event *CallEvent) (string, PassiveReplyType) { diff --git a/wxprotoc/sender/model.go b/wxprotoc/sender/model.go index bf65c68..a6c699e 100644 --- a/wxprotoc/sender/model.go +++ b/wxprotoc/sender/model.go @@ -120,6 +120,12 @@ type TemplateCard struct { //按钮列表,列表长度不超过6 ButtonList []*TemplateCardButtonList `json:"button_list,omitempty"` + //下拉式的选择器列表,multiple_interaction类型的卡片该字段不可为空,一个消息最多支持 3 个选择器 + SelectList []*TemplateCardSelectList `json:"select_list,omitempty"` + + //提交按钮样式 + SubmitButton *TemplateCardSubmitButton `json:"submit_button,omitempty"` + //任务id,当文本通知模版卡片有action_menu字段的时候,该字段必填。 //同一个机器人任务id不能重复,只能由数字、字母和“_-@”组成,最长128字节。任务id只在发消息时候有效,更新消息的时候无效。 //任务id将会在相应的回调事件中返回 @@ -275,3 +281,34 @@ type TemplateCardButtonList struct { //按钮key值,用户点击后,会产生回调事件将本参数作为event_key返回,最长支持1024字节,不可重复 Key string `json:"key"` } + +type TemplateCardSelectList struct { + //下拉式的选择器题目的key,用户提交选项后,会产生回调事件,回调事件会带上该key值表示该题,最长支持1024字节,不可重复 + QuestionKey string `json:"question_key"` + + //选择器的标题,建议不超过13个字 + Title string `json:"title,omitempty"` + + //下拉式的选择器是否不可选,false为可选,true为不可选。仅在更新模版卡片的时候该字段有效 + Disable bool `json:"disable,omitempty"` + + //默认选定的id,不填或错填默认第一个 + SelectedID string `json:"selected_id,omitempty"` + + //选项列表,下拉选项不超过 10 个,最少1个 + OptionList []TemplateCardSelectOption `json:"option_list"` +} + +type TemplateCardSelectOption struct { + //下拉式的选择器选项的id,用户提交选项后,会产生回调事件,回调事件会带上该id值表示该选项,最长支持128字节,不可重复 + ID string `json:"id"` + //下拉式的选择器选项的文案,建议不超过10个字 + Text string `json:"text"` +} + +type TemplateCardSubmitButton struct { + //按钮文案,建议不超过10个字 + Text string `json:"text"` + //提交按钮的key,会产生回调事件将本参数作为EventKey返回,最长支持1024字节 + Key string `json:"key"` +} diff --git a/wxprotoc/sender/sender.go b/wxprotoc/sender/sender.go index 865ac49..7ec4a79 100644 --- a/wxprotoc/sender/sender.go +++ b/wxprotoc/sender/sender.go @@ -21,7 +21,7 @@ type Sender interface { NewsNoticeCard() error ButtonCard() error VoteCard() error - MultipleCard() error + MultipleCard(chatID string, t *TemplateCard) error } type sender struct { @@ -140,9 +140,16 @@ func (s *sender) VoteCard() error { return nil } -func (s *sender) MultipleCard() error { - panic("need to implement") - return nil +func (s *sender) MultipleCard(chatID string, card *TemplateCard) error { + card.CardType = "multiple_interaction" + req := SendReq{ + CommonSend: CommonSend{ + ChatID: chatID, + MsgType: "template_card", + }, + TemplateCard: card, + } + return s.send(req) } func (s *sender) send(content interface{}) error { diff --git a/wxprotoc/sender/sender_test.go b/wxprotoc/sender/sender_test.go index 0416f55..892e497 100644 --- a/wxprotoc/sender/sender_test.go +++ b/wxprotoc/sender/sender_test.go @@ -108,3 +108,46 @@ func TestTemplateCard(t *testing.T) { t.Fatal(err) } } + +func TestMultipleCard(t *testing.T) { + s := newTestSender() + uid, _ := uuid.GenerateUUID() + err := s.MultipleCard("", &TemplateCard{ + Source: &TemplateCardSource{ + IconURL: "https://www.bilibili.com/favicon.ico?v=1", + Desc: "企业微信", + }, + + MainTitle: &TemplateCardMainTitle{ + Title: "欢迎使用企业微信", + Desc: "您的好友正在邀请您加入企业微信", + }, + SelectList: []*TemplateCardSelectList{ + { + QuestionKey: "question_key1", + Title: "选择器标签1", + SelectedID: "id_one", + OptionList: []TemplateCardSelectOption{ + { + ID: "id_one", + Text: "选项1", + }, + { + ID: "id_two", + Text: "选项2", + }, + }, + }, + }, + SubmitButton: &TemplateCardSubmitButton{ + Text: "提交", + Key: "submit_key", + }, + + TaskID: uid, + }) + + if err != nil { + t.Fatal(err) + } +}