We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Currently, response handler seems to be redundancy to handle events like below.
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) { events, err := bot.ParseRequest(req) if err != nil { if err == linebot.ErrInvalidSignature { w.WriteHeader(400) } else { w.WriteHeader(500) } return } // code here }
So I suggest to add new method client.HandleFunc like below.
client.HandleFunc
diff --git a/linebot/receive.go b/linebot/receive.go index 5354c8f..92068cb 100644 --- a/linebot/receive.go +++ b/linebot/receive.go @@ -279,6 +279,21 @@ func (client *Client) ParseRequest(r *http.Request) (events *ReceivedResults, e return } +func (client *Client) HandleFunc(handler func(requests *ReceivedResults)) http.HandlerFunc { + return func(w http.ResponseWriter, req *http.Request) { + events, err := client.ParseRequest(req) + if err != nil { + if err == ErrInvalidSignature { + w.WriteHeader(400) + } else { + w.WriteHeader(500) + } + return + } + handler(events) + } +} + func (client *Client) validateSignature(signature string, body []byte) bool { decoded, err := base64.StdEncoding.DecodeString(signature) if err != nil {
Using this method, this part will be shorter.
func main() { bot, err := linebot.New( os.Getenv("CHANNEL_SECRET"), os.Getenv("CHANNEL_TOKEN"), ) if err != nil { log.Fatal(err) } // Setup HTTP Server for receiving requests from LINE platform http.HandleFunc("/callback", bot.HandleFunc(events *linebot.ReceivedResults) { for _, event := range events { if event.Type == linebot.EventTypeMessage { switch message := event.Message.(type) { case *linebot.TextMessage: source := event.Source if source.Type == linebot.EventSourceTypeUser { if _, err = bot.PushMessage(source.UserID, linebot.NewTextMessage(message.Text)).Do(); err != nil { log.Print(err) } } } } } }) if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil { log.Fatal(err) } }
The text was updated successfully, but these errors were encountered:
日本語ですみません。 最初になんでこれが欲しいと思ったかは、400と500の切り分けが今後さらに増えたりしないかなーと思ったのがきっかけで、であれば line 自身で模範解答があるのがいいのかなーと思った次第です。
Sorry, something went wrong.
We added line-bot-sdk-go/linebot/httphandler that implements http.Handler. You can handle events easily by using this. https://github.com/line/line-bot-sdk-go/blob/7de5817af0beb177af416ed4bb48cca4248b8849/examples/echo_bot_handler/server.go Thanks!
line-bot-sdk-go/linebot/httphandler
http.Handler
Merge pull request line#7 from heroku/tt/build-static-binaries
f16100a
Build static binaries
No branches or pull requests
Currently, response handler seems to be redundancy to handle events like below.
So I suggest to add new method
client.HandleFunc
like below.Using this method, this part will be shorter.
The text was updated successfully, but these errors were encountered: