Compare commits

..

6 Commits

Author SHA1 Message Date
68981e9eb5 add Dockerfile 2025-10-26 16:53:07 +09:00
b8152b3c6a remove redundant stdout 2025-03-20 14:49:46 +09:00
c25735f9c9 add StdOutput 2025-03-20 14:48:08 +09:00
5f9b838af5 change channel struct 2025-03-20 14:40:52 +09:00
d510488d3f remove unnecessary output to stdout at beginning 2025-03-16 19:25:10 +09:00
f9b6c39353 change error handling 2025-03-16 19:24:56 +09:00
7 changed files with 61 additions and 17 deletions

18
Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
FROM golang:alpine AS build-env
RUN apk --no-cache add git make build-base
RUN git clone --depth 1 https://git.sirrow.work/sirrow/tweetdistributor.git
WORKDIR tweetdistributor
RUN mkdir -p /build
RUN go mod tidy
RUN go build -a -tags "netgo" -tags timetzdata -installsuffix netgo -ldflags="-s -w -extldflags \"-static\"" -o=/build/tweetdistributor main.go
FROM alpine:3.22
RUN apk --no-cache add ca-certificates
COPY --from=build-env /build/tweetdistributor /tweetdistributor
RUN chmod u+x /tweetdistributor
CMD ["/tweetdistributor"]

View File

@@ -19,11 +19,11 @@ func Discord(token string, channelId string) *discord {
} }
} }
func (d *discord) BeginRead(tweetchannel chan<- string) { func (d *discord) BeginRead(tweetchannel chan<- *discordgo.MessageCreate) {
d.read(tweetchannel) d.read(tweetchannel)
} }
func (d *discord) read(tweetchannel chan<- string) { func (d *discord) read(tweetchannel chan<- *discordgo.MessageCreate) {
dgsession, err := discordgo.New("Bot " + d.Token) dgsession, err := discordgo.New("Bot " + d.Token)
if err != nil { if err != nil {
return return
@@ -31,7 +31,7 @@ func (d *discord) read(tweetchannel chan<- string) {
dgsession.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { dgsession.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID == d.ChannelID && m.Author.ID != s.State.User.ID { if m.ChannelID == d.ChannelID && m.Author.ID != s.State.User.ID {
tweetchannel <- m.Content tweetchannel <- m
} }
}) })
d.dgsession = dgsession d.dgsession = dgsession

14
main.go
View File

@@ -5,25 +5,31 @@ import (
"os" "os"
"tweetdistributor/discord" "tweetdistributor/discord"
"tweetdistributor/output" "tweetdistributor/output"
"github.com/bwmarrin/discordgo"
) )
func main() { func main() {
fmt.Println("Hello, World!")
d := discord.Discord(os.Getenv("DISCORD_TOKEN"), os.Getenv("DISCORD_CHANNEL")) d := discord.Discord(os.Getenv("DISCORD_TOKEN"), os.Getenv("DISCORD_CHANNEL"))
tweetchannel := make(chan string, 1) tweetchannel := make(chan *discordgo.MessageCreate, 1)
d.BeginRead(tweetchannel) d.BeginRead(tweetchannel)
d.Write("Tweetdistributor Started") d.Write("Tweetdistributor Started")
var outputs []output.OutputInterface var outputs []output.OutputInterface
outputs = append(outputs, output.StdOutput())
outputs = append(outputs, output.TwitterOutput(os.Getenv("TW_ACCESS_TOKEN"), os.Getenv("TW_ACCESS_SECRET"))) outputs = append(outputs, output.TwitterOutput(os.Getenv("TW_ACCESS_TOKEN"), os.Getenv("TW_ACCESS_SECRET")))
outputs = append(outputs, output.BlueskyOutput(os.Getenv("BSKY_IDENTIFIER"), os.Getenv("BSKY_PASSWORD"))) outputs = append(outputs, output.BlueskyOutput(os.Getenv("BSKY_IDENTIFIER"), os.Getenv("BSKY_PASSWORD")))
for tweet := range tweetchannel { for tweet := range tweetchannel {
fmt.Println(tweet)
for _, output := range outputs { for _, output := range outputs {
output.Write(tweet) err := output.Write(tweet.Content)
if err != nil {
errstr := fmt.Sprintf("%s Error: %s", output.GetName(), err)
fmt.Fprintln(os.Stderr, errstr)
d.Write(errstr)
}
} }
} }
} }

View File

@@ -2,9 +2,7 @@ package output
import ( import (
"context" "context"
"fmt"
"log" "log"
"os"
"time" "time"
"github.com/bluesky-social/indigo/api/atproto" "github.com/bluesky-social/indigo/api/atproto"
@@ -60,9 +58,9 @@ func (bo *blueskyoutput) Write(str string) error {
} }
_, recerr := atproto.RepoCreateRecord(context.TODO(), cli, Recordinput) _, recerr := atproto.RepoCreateRecord(context.TODO(), cli, Recordinput)
if recerr != nil {
fmt.Fprint(os.Stderr, "bsky err: ")
fmt.Fprintln(os.Stderr, recerr)
}
return recerr return recerr
} }
func (bo *blueskyoutput) GetName() string {
return "bluesky"
}

View File

@@ -2,4 +2,5 @@ package output
type OutputInterface interface { type OutputInterface interface {
Write(string) error Write(string) error
GetName() string
} }

21
output/stdout.go Normal file
View File

@@ -0,0 +1,21 @@
package output
import (
"fmt"
)
type stdoutput struct {
}
func StdOutput() *stdoutput {
return &stdoutput{}
}
func (so *stdoutput) Write(str string) error {
fmt.Println(str)
return nil
}
func (so *stdoutput) GetName() string {
return "stdout"
}

View File

@@ -47,9 +47,9 @@ func (to *twitteroutput) Write(str string) error {
} }
_, err := managetweet.Create(context.Background(), to.client, p) _, err := managetweet.Create(context.Background(), to.client, p)
if err != nil {
fmt.Fprint(os.Stderr, "twitter err: ")
fmt.Fprintln(os.Stderr, err)
}
return err return err
} }
func (to *twitteroutput) GetName() string {
return "twitter"
}