2022-02-28 23:22:49 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2022-03-13 02:19:57 +01:00
|
|
|
mastodonapi "github.com/cking/go-mastodon"
|
2022-02-28 23:22:49 +01:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
"github.com/spf13/cobra"
|
2022-03-12 20:26:19 +01:00
|
|
|
|
2022-03-13 02:19:57 +01:00
|
|
|
"noa.mornie.org/eriol/telegram-group2mastodon/cfg"
|
|
|
|
"noa.mornie.org/eriol/telegram-group2mastodon/mastodon"
|
2022-03-12 20:26:19 +01:00
|
|
|
"noa.mornie.org/eriol/telegram-group2mastodon/utils"
|
2022-02-28 23:22:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// runCmd represents the run command
|
|
|
|
var runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Run the bot",
|
2022-03-13 03:15:51 +01:00
|
|
|
Long: `Start the bot making it connect bot to both Telegram and Mastodon.
|
2022-02-28 23:22:49 +01:00
|
|
|
|
|
|
|
Every messages posted in the Telegram groups the bot is in will be posted into
|
|
|
|
the specified Mastodon account.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-03-13 03:09:51 +01:00
|
|
|
mastodonInstance := cfg.GetMastodonServerAddress()
|
2022-03-13 02:19:57 +01:00
|
|
|
c := mastodonapi.NewClient(&mastodonapi.Config{
|
|
|
|
Server: mastodonInstance,
|
2022-03-13 03:09:51 +01:00
|
|
|
AccessToken: cfg.GetMastodonAccessToken(),
|
2022-02-28 23:22:49 +01:00
|
|
|
})
|
2022-03-13 02:19:57 +01:00
|
|
|
log.Println("Crating a new client for mastondon istance:", mastodonInstance)
|
2022-03-13 03:09:51 +01:00
|
|
|
allowedTelegramChat := cfg.GetTelegramChatID()
|
2022-03-13 02:19:57 +01:00
|
|
|
log.Println("Allowed telegram chat id:", allowedTelegramChat)
|
2022-02-28 23:22:49 +01:00
|
|
|
|
2022-03-13 03:09:51 +01:00
|
|
|
bot, err := tgbotapi.NewBotAPI(cfg.GetTelegramBotToken())
|
2022-02-28 23:22:49 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2022-03-13 03:09:51 +01:00
|
|
|
bot.Debug = cfg.GetTelegramDebug()
|
2022-02-28 23:22:49 +01:00
|
|
|
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 30
|
|
|
|
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
|
|
|
|
for update := range updates {
|
2022-03-08 02:52:07 +01:00
|
|
|
chatID := update.Message.Chat.ID
|
2022-03-13 02:19:57 +01:00
|
|
|
if chatID != allowedTelegramChat {
|
2022-03-08 02:52:07 +01:00
|
|
|
log.Printf("Error: telegram chat %d is not the allowed one: %d\n",
|
|
|
|
chatID,
|
2022-03-13 02:19:57 +01:00
|
|
|
allowedTelegramChat,
|
2022-03-08 02:52:07 +01:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-28 23:22:49 +01:00
|
|
|
if update.Message != nil {
|
2022-03-08 03:10:49 +01:00
|
|
|
messageID := update.Message.MessageID
|
2022-03-13 02:19:57 +01:00
|
|
|
maxChars := cfg.GetMastodonMaxCharacters()
|
|
|
|
tootVisibility := cfg.GetMastodonVisibility()
|
2022-03-13 03:09:51 +01:00
|
|
|
tootFooter := cfg.GetMastodonTootFooter()
|
2022-02-28 23:22:49 +01:00
|
|
|
|
2022-03-04 02:29:17 +01:00
|
|
|
if update.Message.Text != "" {
|
2022-03-08 03:10:49 +01:00
|
|
|
log.Printf("Text message received. Message id: %d\n", messageID)
|
2022-03-07 02:37:43 +01:00
|
|
|
|
2022-03-09 02:23:41 +01:00
|
|
|
text := update.Message.Text
|
2022-03-13 02:19:57 +01:00
|
|
|
messages := utils.SplitTextAtChunk(text, maxChars, tootFooter)
|
|
|
|
mastodon.PostToots(c, messages, tootVisibility)
|
2022-03-05 02:53:38 +01:00
|
|
|
|
2022-03-04 02:29:17 +01:00
|
|
|
} else if update.Message.Photo != nil {
|
2022-03-08 03:10:49 +01:00
|
|
|
log.Printf("Photo received. Message id: %d\n", messageID)
|
2022-03-07 02:37:43 +01:00
|
|
|
|
2022-03-04 02:29:17 +01:00
|
|
|
// Telegram provides multiple sizes of photo, just take the
|
|
|
|
// biggest.
|
|
|
|
biggest_photo := tgbotapi.PhotoSize{FileSize: 0}
|
|
|
|
for _, photo := range update.Message.Photo {
|
|
|
|
|
|
|
|
if photo.FileSize > biggest_photo.FileSize {
|
|
|
|
biggest_photo = photo
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
url, _ := bot.GetFileDirectURL(biggest_photo.FileID)
|
2022-03-05 02:53:38 +01:00
|
|
|
log.Printf("Downloading: %s\n", url)
|
2022-03-13 03:13:42 +01:00
|
|
|
file, err := utils.DownloadFile(url)
|
2022-03-05 02:53:38 +01:00
|
|
|
if err != nil {
|
2022-03-13 00:38:44 +01:00
|
|
|
log.Printf("Could not download file: %v", err)
|
2022-03-05 02:53:38 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-13 02:19:57 +01:00
|
|
|
mastodon.PostPhoto(
|
|
|
|
c,
|
|
|
|
file,
|
|
|
|
update.Message.Caption,
|
|
|
|
maxChars,
|
|
|
|
tootVisibility,
|
|
|
|
)
|
2022-02-28 23:22:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
}
|