Skip to content

Commit

Permalink
divide message sending part to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 committed Sep 9, 2024
1 parent 0e14c5c commit 1377afe
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -148,23 +149,10 @@ func postPost(c *fiber.Ctx) error {
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}

contentJson, err := json.Marshal(struct {
Content string `json:"content"`
}{
Content: `## 新しいポストリクエストが作成されました
err = sendDiscordWebhook(`## 新しいポストリクエストが作成されました
ポストリクエストを承認してください。
http://localhost:9000/reviews/` + pr.ID.String(),
})
if err != nil {
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}

_, err = http.Post(
webhookURL,
"application/json",
bytes.NewBuffer(contentJson),
)
http://localhost:9000/reviews/` + pr.ID.String())
if err != nil {
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
Expand Down Expand Up @@ -205,28 +193,12 @@ func postReview(c *fiber.Ctx) error {
}

if dev {
contentJson, err := json.Marshal(struct {
Content string `json:"content"`
}{
Content: "## ポスト送信テスト\n\n```\n" + pr.Content + "\n```",
})
if err != nil {
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}

_, err = http.Post(
webhookURL,
"application/json",
bytes.NewBuffer(contentJson),
)
err = sendDiscordWebhook("## ポスト送信テスト\n\n```\n" + pr.Content + "\n```")
if err != nil {
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
} else {
client := gotwtr.New(xKey)
_, err := client.PostTweet(c.Context(), &gotwtr.PostTweetOption{
Text: pr.Content,
})
err = sendPost(c.Context(), pr.Content)
if err != nil {
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
Expand All @@ -242,3 +214,37 @@ func postReview(c *fiber.Ctx) error {
"Content": pr.Content,
}, "layout")
}

func sendDiscordWebhook(content string) error {
contentJson, err := json.Marshal(struct {
Content string `json:"content"`
}{
Content: content,
})
if err != nil {
return err
}

_, err = http.Post(
webhookURL,
"application/json",
bytes.NewBuffer(contentJson),
)
if err != nil {
return err
}

return nil
}

func sendPost(ctx context.Context, content string) error {
client := gotwtr.New(xKey)
_, err := client.PostTweet(ctx, &gotwtr.PostTweetOption{
Text: content,
})
if err != nil {
return err
}

return nil
}

0 comments on commit 1377afe

Please sign in to comment.