1
0
Fork 0

Split longer telegram messages to the max mastodon toot length
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Daniele Tricoli 2022-03-07 02:37:43 +01:00
parent c5754e8ad0
commit 86868d786f
2 changed files with 47 additions and 18 deletions

View File

@ -17,11 +17,12 @@ import (
)
const (
DEBUG = "DEBUG"
TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN"
MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS"
MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN"
MASTODON_TOOT_VISIBILITY = "MASTODON_TOOT_VISIBILITY"
DEBUG = "DEBUG"
TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN"
MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS"
MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN"
MASTODON_TOOT_VISIBILITY = "MASTODON_TOOT_VISIBILITY"
MASTODON_TOOT_MAX_CHARACTERS = "MASTODON_TOOT_MAX_CHARACTERS"
)
// runCmd represents the run command
@ -57,18 +58,36 @@ the specified Mastodon account.`,
if update.Message.Text != "" {
log.Println("Text message received.")
status, err := c.PostStatus(context.Background(), &mastodon.Toot{
Status: update.Message.Text,
Visibility: parseMastodonVisibility(os.Getenv(MASTODON_TOOT_VISIBILITY)),
})
if err != nil {
log.Printf("Could not post status: %v", err)
continue
max_characters := parseMastodonMaxCharacters(
os.Getenv(MASTODON_TOOT_MAX_CHARACTERS))
message := update.Message.Text
length := len([]rune(message))
in_reply_to := ""
for start := 0; start < length; start += max_characters {
end := start + max_characters
if end > length {
end = length
}
message_to_post := string([]rune(message)[start:end])
status, err := c.PostStatus(context.Background(), &mastodon.Toot{
Status: message_to_post,
Visibility: parseMastodonVisibility(os.Getenv(MASTODON_TOOT_VISIBILITY)),
InReplyToID: mastodon.ID(in_reply_to),
})
if err != nil {
log.Printf("Could not post status: %v", err)
continue
}
log.Printf("Posted status %s", status.URL)
in_reply_to = string(status.ID)
}
log.Printf("Posted status %s", status.URL)
} else if update.Message.Photo != nil {
log.Println("Photo received.")
// Telegram provides multiple sizes of photo, just take the
// biggest.
biggest_photo := tgbotapi.PhotoSize{FileSize: 0}
@ -108,10 +127,7 @@ the specified Mastodon account.`,
continue
}
log.Printf("Posted status %s", status.URL)
}
// fmt.Printf("%#v\n\n", update.Message)
}
}
},
@ -157,3 +173,12 @@ func downloadFile(url string) (io.ReadCloser, error) {
return response.Body, nil
}
// Parse Mastodon max characters and return 500 as default in case of errors.
func parseMastodonMaxCharacters(s string) int {
if n, err := strconv.ParseUint(s, 10, 32); err == nil {
return int(n)
}
return 500
}

View File

@ -7,7 +7,6 @@ import (
)
func TestParseBoolOrFalse(t *testing.T) {
assert.Equal(t, parseBoolOrFalse("True"), true)
assert.Equal(t, parseBoolOrFalse("TRUE"), true)
assert.Equal(t, parseBoolOrFalse("true"), true)
@ -18,7 +17,6 @@ func TestParseBoolOrFalse(t *testing.T) {
}
func TestParseMastodonVisibility(t *testing.T) {
assert.Equal(t, parseMastodonVisibility("public"), "public")
assert.Equal(t, parseMastodonVisibility("direct"), "direct")
assert.Equal(t, parseMastodonVisibility("unlisted"), "unlisted")
@ -34,3 +32,9 @@ func TestParseMastodonVisibility(t *testing.T) {
assert.Equal(t, parseMastodonVisibility(""), "unlisted")
assert.Equal(t, parseMastodonVisibility(" "), "unlisted")
}
func TestParseMastodonMaxCharacters(t *testing.T) {
assert.Equal(t, parseMastodonMaxCharacters("42"), 42)
assert.Equal(t, parseMastodonMaxCharacters("-42"), 500)
assert.Equal(t, parseMastodonMaxCharacters("hello"), 500)
}