Compare commits
6 Commits
1d6e9258f9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 68981e9eb5 | |||
| b8152b3c6a | |||
| c25735f9c9 | |||
| 5f9b838af5 | |||
| d510488d3f | |||
| f9b6c39353 |
18
Dockerfile
Normal file
18
Dockerfile
Normal 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"]
|
||||||
@@ -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
14
main.go
@@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
21
output/stdout.go
Normal 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"
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user