1
0
Fork 0

Check the allowed telegram chat id
continuous-integration/drone/push Build is passing Details

Message from a chat that is not the allowed one will be ignored.
This commit is contained in:
Daniele Tricoli 2022-03-08 02:52:07 +01:00
parent c338bd0b68
commit c7dc59ebc7
1 changed files with 20 additions and 0 deletions

View File

@ -42,6 +42,8 @@ the specified Mastodon account.`,
}) })
log.Println("Crating a new client for mastondon istance:", mastodon_instance) log.Println("Crating a new client for mastondon istance:", mastodon_instance)
max_characters := parseMastodonMaxCharacters(os.Getenv(MASTODON_TOOT_MAX_CHARACTERS)) max_characters := parseMastodonMaxCharacters(os.Getenv(MASTODON_TOOT_MAX_CHARACTERS))
allowed_telegram_chat := parseTelegramChatID(os.Getenv(TELEGRAM_CHAT_ID))
log.Println("Allowed telegram chat:", mastodon_instance)
bot, err := tgbotapi.NewBotAPI(os.Getenv(TELEGRAM_BOT_TOKEN)) bot, err := tgbotapi.NewBotAPI(os.Getenv(TELEGRAM_BOT_TOKEN))
if err != nil { if err != nil {
@ -56,6 +58,15 @@ the specified Mastodon account.`,
updates := bot.GetUpdatesChan(u) updates := bot.GetUpdatesChan(u)
for update := range updates { for update := range updates {
chatID := update.Message.Chat.ID
if chatID != allowed_telegram_chat {
log.Printf("Error: telegram chat %d is not the allowed one: %d\n",
chatID,
allowed_telegram_chat,
)
continue
}
if update.Message != nil { if update.Message != nil {
if update.Message.Text != "" { if update.Message.Text != "" {
@ -186,3 +197,12 @@ func parseMastodonMaxCharacters(s string) int {
return 500 return 500
} }
func parseTelegramChatID(s string) int64 {
r, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return r
}