Files
tweetdistributor/main.go
2026-07-03 10:03:19 +09:00

45 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"tweetdistributor/discord"
"tweetdistributor/output"
"unicode/utf8"
"github.com/bwmarrin/discordgo"
)
const maxTweetLength = 140
func main() {
d := discord.Discord(os.Getenv("DISCORD_TOKEN"), os.Getenv("DISCORD_CHANNEL"))
tweetchannel := make(chan *discordgo.MessageCreate, 1)
d.BeginRead(tweetchannel)
d.Write("Tweetdistributor Started")
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.BlueskyOutput(os.Getenv("BSKY_IDENTIFIER"), os.Getenv("BSKY_PASSWORD")))
for tweet := range tweetchannel {
if length := utf8.RuneCountInString(tweet.Content); length > maxTweetLength {
errstr := fmt.Sprintf("Error: message is %d characters, exceeding the %d character limit; not posted", length, maxTweetLength)
fmt.Fprintln(os.Stderr, errstr)
d.Write(errstr)
continue
}
for _, output := range outputs {
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)
}
}
}
}