modify line command multiple reply or push message

This commit is contained in:
Jay 2019-03-05 21:47:10 +08:00
parent afc0987d40
commit cc67ba4738
2 changed files with 65 additions and 29 deletions

View File

@ -104,9 +104,9 @@ func checkMessageObject(m interface{}) interface{} {
}
// PushMessage -
func PushMessage(target string, message interface{}) {
func PushMessage(target string, message ...interface{}) {
log.Println("push target :::: ", target)
if len(target) == 0 {
if len(target) == 0 || len(message) == 0 {
return
}
urlPath := "/v2/bot/message/push"
@ -115,9 +115,19 @@ func PushMessage(target string, message interface{}) {
To: target,
}
message = checkMessageObject(message)
checked := make([]interface{}, 0)
for _, v := range message {
tmp := checkMessageObject(v)
if tmp == nil {
continue
}
checked = append(checked, tmp)
}
body.Messages = append(body.Messages, message)
body.Messages = append(body.Messages, checked...)
if len(body.Messages) > 5 {
body.Messages = body.Messages[:5]
}
dataByte, err := json.Marshal(body)
if err != nil {
log.Println("to json error ::::", err)
@ -153,8 +163,8 @@ func PushMessage(target string, message interface{}) {
}
// ReplyMessage -
func ReplyMessage(replyToken string, message interface{}) {
if len(replyToken) == 0 {
func ReplyMessage(replyToken string, message ...interface{}) {
if len(replyToken) == 0 || len(message) == 0 {
return
}
urlPath := "/v2/bot/message/reply"
@ -163,9 +173,19 @@ func ReplyMessage(replyToken string, message interface{}) {
ReplyToken: replyToken,
}
message = checkMessageObject(message)
checked := make([]interface{}, 0)
for _, v := range message {
tmp := checkMessageObject(v)
if tmp == nil {
continue
}
checked = append(checked, tmp)
}
body.Messages = append(body.Messages, message)
body.Messages = append(body.Messages, checked...)
if len(body.Messages) > 5 {
body.Messages = body.Messages[:5]
}
dataByte, err := json.Marshal(body)
if err != nil {
return

View File

@ -51,9 +51,13 @@ func ParseLineMsg(txt, replyToken string, source *lineobj.SourceObject) {
return
}
str := runCMD(strings.Join(strs[1:], " "), c.Message, source)
m := parseResult(str)
line.ReplyMessage(replyToken, m)
resStrs := runCMD(strings.Join(strs[1:], " "), c.Message, source)
msgs := make([]interface{}, 0)
for _, v := range resStrs {
m := parseResult(v)
msgs = append(msgs, m)
}
line.ReplyMessage(replyToken, msgs...)
} else {
// key cmd
@ -62,9 +66,13 @@ func ParseLineMsg(txt, replyToken string, source *lineobj.SourceObject) {
return
}
str := runCMD(strings.Join(strs[1:], " "), c.Message, source)
m := parseResult(str)
line.ReplyMessage(replyToken, m)
resStrs := runCMD(strings.Join(strs[1:], " "), c.Message, source)
msgs := make([]interface{}, 0)
for _, v := range resStrs {
m := parseResult(v)
msgs = append(msgs, m)
}
line.ReplyMessage(replyToken, msgs...)
}
}
@ -95,12 +103,18 @@ func parseResult(str string) interface{} {
return m
}
func runCMD(txt, c string, s *lineobj.SourceObject) (res string) {
func runCMD(txt, c string, s *lineobj.SourceObject) (res []string) {
cmds := strings.Split(c, "$#$")
if len(cmds) == 0 {
return
}
for _, c := range cmds {
cmdAct := parseCMD(c)
if len(cmdAct) == 0 {
return c
res = append(res, c)
continue
}
res = c
tmpRes := c
for _, v := range cmdAct {
if len(v) > 1 {
// run cmd
@ -110,8 +124,10 @@ func runCMD(txt, c string, s *lineobj.SourceObject) (res string) {
sub = strings.Join(m[1:], " ")
}
cmdRes := selectAct(m[0], sub, txt, s)
res = strings.Replace(res, v[0], cmdRes, 1)
tmpRes = strings.Replace(tmpRes, v[0], cmdRes, 1)
}
}
res = append(res, tmpRes)
}
return
}