From ab61333ab79540177f9ca268058317eefe0f2cb0 Mon Sep 17 00:00:00 2001 From: claustra01 Date: Mon, 29 Apr 2024 13:38:25 +0900 Subject: [PATCH] add: calender api expired message --- bot/google/calendar.go | 8 +++++++- bot/handler/callback.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/bot/google/calendar.go b/bot/google/calendar.go index 7afea94..0141a3f 100644 --- a/bot/google/calendar.go +++ b/bot/google/calendar.go @@ -9,6 +9,10 @@ import ( "time" ) +var ( + ErrCalenderExpired = errors.New("calender api session expired") +) + type CalendarContentInterface interface { GetType() string } @@ -98,7 +102,9 @@ func (c *OAuthClient) RegisterCalenderEvent(content CalendarContent, accessToken } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { + if resp.StatusCode == http.StatusUnauthorized { + return ErrCalenderExpired + } else if resp.StatusCode != http.StatusOK { return fmt.Errorf("Failed to register event: %s", resp.Status) } diff --git a/bot/handler/callback.go b/bot/handler/callback.go index 4235e77..1900a7a 100644 --- a/bot/handler/callback.go +++ b/bot/handler/callback.go @@ -162,6 +162,21 @@ func Callback(w http.ResponseWriter, req *http.Request, bot *linebot.LineBot) { } err = googleClient.RegisterCalenderEvent(content, accessToken) + if err == google.ErrCalenderExpired { + log.Printf("Failed to register event: %+v\n", err) + replyText := fmt.Sprintf("セッション切れです。再度ログインしてください。\n%s", liffUrl) + _, err = bot.ReplyMessage( + &linebot.ReplyMessageRequest{ + ReplyToken: e.ReplyToken, + Messages: []linebot.MessageInterface{ + linebot.NewTextMessage(replyText), + }, + }, + ) + if err != nil { + log.Print(err) + } + } if err != nil { log.Printf("Failed to register event: %+v\n", err) _, err = bot.ReplyMessage( @@ -328,6 +343,21 @@ func Callback(w http.ResponseWriter, req *http.Request, bot *linebot.LineBot) { } err = googleClient.RegisterCalenderEvent(content, accessToken) + if err == google.ErrCalenderExpired { + log.Printf("Failed to register event: %+v\n", err) + replyText := fmt.Sprintf("セッション切れです。再度ログインしてください。\n%s", liffUrl) + _, err = bot.ReplyMessage( + &linebot.ReplyMessageRequest{ + ReplyToken: e.ReplyToken, + Messages: []linebot.MessageInterface{ + linebot.NewTextMessage(replyText), + }, + }, + ) + if err != nil { + log.Print(err) + } + } if err != nil { log.Printf("Failed to register event: %+v\n", err) _, err = bot.ReplyMessage(