Skip to content
New issue

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

proposal: bot.HandleFunc #7

Closed
mattn opened this issue Sep 29, 2016 · 2 comments
Closed

proposal: bot.HandleFunc #7

mattn opened this issue Sep 29, 2016 · 2 comments

Comments

@mattn
Copy link

mattn commented Sep 29, 2016

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.

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)
    }
}
@mattn
Copy link
Author

mattn commented Sep 29, 2016

日本語ですみません。
最初になんでこれが欲しいと思ったかは、400と500の切り分けが今後さらに増えたりしないかなーと思ったのがきっかけで、であれば line 自身で模範解答があるのがいいのかなーと思った次第です。

@sugyan
Copy link
Contributor

sugyan commented Oct 6, 2016

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!

@sugyan sugyan closed this as completed Oct 6, 2016
kyutan pushed a commit to kyutan/line-bot-sdk-go that referenced this issue Sep 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants