Skip to content

Commit

Permalink
Feat: use zerolog library
Browse files Browse the repository at this point in the history
  • Loading branch information
vgjm committed Mar 17, 2024
1 parent c4bf2d5 commit 3607d48
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ module github.com/vgjm/linebot

go 1.22.1

require github.com/line/line-bot-sdk-go/v8 v8.4.0
require (
github.com/line/line-bot-sdk-go/v8 v8.4.0
github.com/rs/zerolog v1.32.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
)

require (
cloud.google.com/go v0.110.8 // indirect
Expand All @@ -21,7 +29,7 @@ require (
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/api v0.149.0
google.golang.org/appengine v1.6.7 // indirect
Expand Down
19 changes: 17 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand Down Expand Up @@ -57,8 +59,18 @@ github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/line/line-bot-sdk-go/v8 v8.4.0 h1:rxFfOmbJbUzfuobDHT3pR6fKXxcojNVoFtd1C8Yz08M=
github.com/line/line-bot-sdk-go/v8 v8.4.0/go.mod h1:n9Ly8OHM6xCeQktLzRpQHe/yBda95kFgmQUefUQeFCs=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down Expand Up @@ -96,8 +108,11 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
5 changes: 2 additions & 3 deletions lineclient/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package lineclient

import (
"log"

"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api"
"github.com/rs/zerolog/log"
)

type LineClient struct {
Expand Down Expand Up @@ -33,7 +32,7 @@ func (client *LineClient) ReplyMessage(replyToken string, text string) error {
); err != nil {
return err
} else {
log.Println("Sent text reply.")
log.Debug().Msg("Sent text reply.")
}
return nil
}
30 changes: 17 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,42 @@ package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"strings"

"github.com/line/line-bot-sdk-go/v8/linebot/webhook"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/vgjm/linebot/geminiclient"
"github.com/vgjm/linebot/lineclient"
"github.com/vgjm/linebot/router"
)

func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

channelSecret := os.Getenv("LINE_CHANNEL_SECRET")

lineClient, err := lineclient.New(os.Getenv("LINE_CHANNEL_TOKEN"))
if err != nil {
log.Fatal(err)
log.Fatal().Err(err).Msg("Failed to start Line client")
}

geminiClient, err := geminiclient.New(os.Getenv("GEMINI_API_KEY"))
if err != nil {
log.Fatal(err)
log.Fatal().Err(err).Msg("Failed to start Gemini client")
}
defer geminiClient.Close()

// Setup HTTP Server for receiving requests from LINE platform
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
log.Println("/callback called...")
log.Debug().Msg("/callback called...")

cb, err := webhook.ParseRequest(channelSecret, req)
if err != nil {
log.Printf("Cannot parse request: %+v\n", err)
log.Err(err).Msg("Cannot parse request")
if errors.Is(err, webhook.ErrInvalidSignature) {
w.WriteHeader(400)
} else {
Expand All @@ -57,9 +61,9 @@ func main() {
return
}

log.Println("Handling events...")
log.Debug().Msg("Handling events...")
for _, event := range cb.Events {
log.Printf("/callback called%+v...\n", event)
log.Debug().Msgf("Event type: %v", event.GetType())

switch e := event.(type) {
case webhook.MessageEvent:
Expand All @@ -69,23 +73,23 @@ func main() {
switch mType {
case router.MENU:
if err := lineClient.ReplyMessage(e.ReplyToken, "菜单"); err != nil {
log.Printf("Failed to reply message: %+v\n", err)
log.Err(err).Msg("Failed to reply message")
}
case router.AI_REPLY:
resp, err := geminiClient.SingleQuestion(strings.Replace(message.Text, "/", "", 1))
if err != nil {
log.Printf("Failed to call gemini: %+v\n", err)
log.Err(err).Msg("Failed to call Gemini API")
} else {
if err := lineClient.ReplyMessage(e.ReplyToken, resp); err != nil {
log.Printf("Failed to reply message: %+v\n", err)
log.Err(err).Msg("Failed to reply message")
}
}
}
default:
log.Printf("Unsupported message content: %T\n", e.Message)
log.Info().Msgf("Unsupported message content: %T\n", e.Message)
}
default:
log.Printf("Unsupported message: %T\n", event)
log.Info().Msgf("Unsupported message: %T\n", event)
}
}
})
Expand All @@ -98,6 +102,6 @@ func main() {
}
fmt.Println("http://localhost:" + port + "/")
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
log.Fatal().Err(err)
}
}

0 comments on commit 3607d48

Please sign in to comment.