-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/kyong0612/fitness-saporter/infra/line" | ||
) | ||
|
||
func PostLINEWebhook(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
events, err := line.ParseWebhookRequest(ctx, r) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if len(events) == 0 { | ||
w.WriteHeader(http.StatusOK) | ||
return | ||
} | ||
|
||
line, err := line.NewClient() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
for _, event := range events { | ||
if _, err := line.ReplyMessage(ctx, event.ReplyToken, []string{event.Content}); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package line | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/kyong0612/fitness-saporter/infra/config" | ||
"github.com/line/line-bot-sdk-go/v8/linebot/messaging_api" | ||
) | ||
|
||
var _ Client = client{} | ||
|
||
type Client interface { | ||
ReplyMessage(ctx context.Context, replyToken string, messages []string) (*http.Response, error) | ||
} | ||
|
||
type client struct { | ||
bot *messaging_api.MessagingApiAPI | ||
} | ||
|
||
func NewClient() (Client, error) { | ||
bot, err := messaging_api.NewMessagingApiAPI( | ||
config.Get().LINEChannelToken, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client{ | ||
bot, | ||
}, nil | ||
} | ||
|
||
func (c client) ReplyMessage(ctx context.Context, replyToken string, messages []string) (*http.Response, error) { | ||
textMessages := make([]messaging_api.MessageInterface, 0, len(messages)) | ||
for _, message := range messages { | ||
textMessages = append(textMessages, messaging_api.TextMessage{ | ||
Text: message, | ||
}) | ||
} | ||
|
||
resp, _, err := c.bot.ReplyMessageWithHttpInfo( | ||
&messaging_api.ReplyMessageRequest{ | ||
ReplyToken: replyToken, | ||
Messages: textMessages, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
slog.InfoContext(ctx, fmt.Sprintf("status code: (%v), x-line-request-id: (%v)", resp.StatusCode, resp.Header.Get("x-line-request-id"))) | ||
|
||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package line | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httputil" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/kyong0612/fitness-saporter/infra/config" | ||
"github.com/line/line-bot-sdk-go/v8/linebot/webhook" | ||
) | ||
|
||
type MessageType string | ||
|
||
const ( | ||
MessageTypeText MessageType = "text" | ||
MessageTypeImage MessageType = "image" | ||
) | ||
|
||
type MessageEvent struct { | ||
ReplyToken string | ||
Type MessageType | ||
Content string | ||
} | ||
|
||
func ParseWebhookRequest(ctx context.Context, req *http.Request) ([]MessageEvent, error) { | ||
cb, err := webhook.ParseRequest( | ||
config.Get().LINEChannelToken, | ||
req, | ||
) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to parse webhook request") | ||
} | ||
|
||
dump, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to dump request") | ||
} | ||
|
||
slog.InfoContext( | ||
ctx, | ||
"webhook request parsed", | ||
slog.Group( | ||
"request", slog.String("body", string(dump)), | ||
), | ||
) | ||
|
||
result := make([]MessageEvent, 0, len(cb.Events)) | ||
for _, event := range cb.Events { | ||
switch e := event.(type) { | ||
case webhook.MessageEvent: | ||
replyToken := e.ReplyToken | ||
switch message := e.Message.(type) { | ||
case webhook.TextMessageContent: | ||
result = append(result, MessageEvent{ | ||
ReplyToken: replyToken, | ||
Type: "text", | ||
Content: message.Text, | ||
}) | ||
case webhook.ImageMessageContent: | ||
result = append(result, MessageEvent{ | ||
ReplyToken: replyToken, | ||
Type: "image", | ||
Content: message.ContentProvider.OriginalContentUrl, | ||
}) | ||
default: | ||
slog.WarnContext( | ||
ctx, | ||
"unsupported message type", | ||
slog.String("type", message.GetType()), | ||
) | ||
} | ||
default: | ||
slog.WarnContext( | ||
ctx, | ||
"unsupported event type", | ||
slog.String("type", e.GetType()), | ||
) | ||
} | ||
} | ||
|
||
return result, nil | ||
} |