前提
這是我上週所開發的流浪動物領養聊天機器人得到大家的一些建議.
裡面有個建議是希望能夠新增好友的時候能夠增加”歡迎訊息”. 於是開始實作.
相關問題與解決方式
如何在新增朋友的時候發送預設簡訊
目標:
想要再新增你的機器人的時候,能夠發送一些簡單的介紹.
遇到困難:
其實根據 Line API Spec (Receive Operations)應該要能直接收到透過 content.From
來查詢誰發的好友需求才是.
不過不清楚的官方文件是這樣說沒錯啦,實際上去跑的時候.
content
沒資料 (ID, From, To)OpContent
(透過ontent.OperationContent()
拿的)一樣都沒資料 (ID, From, To)
在不斷地來來回回,總算找到可能是放在 result.RawContent
.令人驚訝的是竟然是放在 result.RawContent.Param[0]
.
以下是全部相關程式碼
程式碼:
funccallbackHandler(whttp.ResponseWriter,r*http.Request){received,err:=bot.ParseRequest(r)iferr!=nil{iferr==linebot.ErrInvalidSignature{w.WriteHeader(400)}else{w.WriteHeader(500)}return}for_,result:=rangereceived.Results{content:=result.Content()//Add with new friend.ifcontent!=nil&&content.IsOperation&&content.OpType==OpTypeAddedAsFriend{out:=fmt.Sprintf("(Welcom MSG)歡迎訊息..")//result.RawContent.Params[0] is who send your bot friend added operation, otherwise you cannot get in content or operation content._,err=bot.SendText([]string{result.RawContent.Params[0]},out)iferr!=nil{log.Println(err)}log.Println("New friend add event.")}}}