2018-11-28 10:57:59 +01:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2022-02-03 00:20:25 +01:00
|
|
|
"fmt"
|
2018-12-12 23:50:08 +01:00
|
|
|
"html"
|
2021-04-03 23:15:19 +02:00
|
|
|
"path/filepath"
|
2018-11-28 10:57:59 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-07-08 22:19:45 +02:00
|
|
|
"unicode/utf16"
|
2018-11-28 10:57:59 +01:00
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2022-02-04 16:15:19 +01:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2023-03-14 23:03:04 +01:00
|
|
|
tgbotapi "github.com/matterbridge/telegram-bot-api/v6"
|
2018-11-28 10:57:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Btelegram) handleUpdate(rmsg *config.Message, message, posted, edited *tgbotapi.Message) *tgbotapi.Message {
|
|
|
|
// handle channels
|
|
|
|
if posted != nil {
|
2022-02-03 00:20:25 +01:00
|
|
|
if posted.Text == "/chatId" {
|
|
|
|
chatID := strconv.FormatInt(posted.Chat.ID, 10)
|
|
|
|
|
2023-03-14 23:03:04 +01:00
|
|
|
// Handle chat topics
|
|
|
|
if posted.IsTopicMessage {
|
|
|
|
chatID = chatID + "/" + strconv.Itoa(posted.MessageThreadID)
|
|
|
|
}
|
|
|
|
|
2022-02-03 00:20:25 +01:00
|
|
|
_, err := b.Send(config.Message{
|
|
|
|
Channel: chatID,
|
|
|
|
Text: fmt.Sprintf("ID of this chat: %s", chatID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Warnf("Unable to send chatID to %s", chatID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = posted
|
|
|
|
rmsg.Text = message.Text
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// edited channel message
|
|
|
|
if edited != nil && !b.GetBool("EditDisable") {
|
|
|
|
message = edited
|
|
|
|
rmsg.Text = rmsg.Text + message.Text + b.GetString("EditSuffix")
|
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleChannels checks if it's a channel message and if the message is a new or edited messages
|
|
|
|
func (b *Btelegram) handleChannels(rmsg *config.Message, message *tgbotapi.Message, update tgbotapi.Update) *tgbotapi.Message {
|
|
|
|
return b.handleUpdate(rmsg, message, update.ChannelPost, update.EditedChannelPost)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGroups checks if it's a group message and if the message is a new or edited messages
|
|
|
|
func (b *Btelegram) handleGroups(rmsg *config.Message, message *tgbotapi.Message, update tgbotapi.Update) *tgbotapi.Message {
|
|
|
|
return b.handleUpdate(rmsg, message, update.Message, update.EditedMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleForwarded handles forwarded messages
|
|
|
|
func (b *Btelegram) handleForwarded(rmsg *config.Message, message *tgbotapi.Message) {
|
2020-05-23 19:15:26 +02:00
|
|
|
if message.ForwardDate == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:20:39 +01:00
|
|
|
if message.ForwardFromChat != nil && message.ForwardFrom == nil {
|
|
|
|
rmsg.Text = "Forwarded from " + message.ForwardFromChat.Title + ": " + rmsg.Text
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 19:15:26 +02:00
|
|
|
if message.ForwardFrom == nil {
|
|
|
|
rmsg.Text = "Forwarded from " + unknownUser + ": " + rmsg.Text
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
usernameForward := ""
|
|
|
|
if b.GetBool("UseFirstName") {
|
|
|
|
usernameForward = message.ForwardFrom.FirstName
|
|
|
|
}
|
2022-03-25 21:42:28 +01:00
|
|
|
if b.GetBool("UseFullName") {
|
|
|
|
usernameForward = message.ForwardFrom.FirstName + " " + message.ForwardFrom.LastName
|
|
|
|
}
|
2020-05-23 19:15:26 +02:00
|
|
|
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = message.ForwardFrom.UserName
|
2018-11-28 10:57:59 +01:00
|
|
|
if usernameForward == "" {
|
2020-05-23 19:15:26 +02:00
|
|
|
usernameForward = message.ForwardFrom.FirstName
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 19:15:26 +02:00
|
|
|
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = unknownUser
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleQuoting handles quoting of previous messages
|
|
|
|
func (b *Btelegram) handleQuoting(rmsg *config.Message, message *tgbotapi.Message) {
|
2023-03-14 23:03:04 +01:00
|
|
|
// Used to check if the message was a reply to the root topic
|
|
|
|
if message.ReplyToMessage != nil && !(message.ReplyToMessage.MessageID == message.MessageThreadID) { //nolint:nestif
|
2018-11-28 10:57:59 +01:00
|
|
|
usernameReply := ""
|
|
|
|
if message.ReplyToMessage.From != nil {
|
|
|
|
if b.GetBool("UseFirstName") {
|
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName
|
|
|
|
}
|
2022-03-25 21:42:28 +01:00
|
|
|
if b.GetBool("UseFullName") {
|
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName + " " + message.ReplyToMessage.From.LastName
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = message.ReplyToMessage.From.UserName
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = unknownUser
|
|
|
|
}
|
|
|
|
if !b.GetBool("QuoteDisable") {
|
2022-03-30 22:23:52 +02:00
|
|
|
quote := message.ReplyToMessage.Text
|
|
|
|
if quote == "" {
|
|
|
|
quote = message.ReplyToMessage.Caption
|
|
|
|
}
|
|
|
|
rmsg.Text = b.handleQuote(rmsg.Text, usernameReply, quote)
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUsername handles the correct setting of the username
|
|
|
|
func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Message) {
|
|
|
|
if message.From != nil {
|
2021-12-12 00:35:32 +01:00
|
|
|
rmsg.UserID = strconv.FormatInt(message.From.ID, 10)
|
2018-11-28 10:57:59 +01:00
|
|
|
if b.GetBool("UseFirstName") {
|
|
|
|
rmsg.Username = message.From.FirstName
|
|
|
|
}
|
2022-03-25 21:42:28 +01:00
|
|
|
if b.GetBool("UseFullName") {
|
2023-03-09 22:02:31 +01:00
|
|
|
if message.From.FirstName != "" && message.From.LastName != "" {
|
|
|
|
rmsg.Username = message.From.FirstName + " " + message.From.LastName
|
|
|
|
}
|
2022-03-25 21:42:28 +01:00
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
if rmsg.Username == "" {
|
|
|
|
rmsg.Username = message.From.UserName
|
|
|
|
if rmsg.Username == "" {
|
|
|
|
rmsg.Username = message.From.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// only download avatars if we have a place to upload them (configured mediaserver)
|
2020-02-15 18:31:40 +01:00
|
|
|
if b.General.MediaServerUpload != "" || (b.General.MediaServerDownload != "" && b.General.MediaDownloadPath != "") {
|
2018-11-28 10:57:59 +01:00
|
|
|
b.handleDownloadAvatar(message.From.ID, rmsg.Channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 16:15:19 +01:00
|
|
|
if message.SenderChat != nil { //nolint:nestif
|
|
|
|
rmsg.UserID = strconv.FormatInt(message.SenderChat.ID, 10)
|
|
|
|
if b.GetBool("UseFirstName") {
|
|
|
|
rmsg.Username = message.SenderChat.FirstName
|
|
|
|
}
|
2022-03-25 21:42:28 +01:00
|
|
|
if b.GetBool("UseFullName") {
|
2023-03-09 22:02:31 +01:00
|
|
|
if message.SenderChat.FirstName != "" && message.SenderChat.LastName != "" {
|
|
|
|
rmsg.Username = message.SenderChat.FirstName + " " + message.SenderChat.LastName
|
|
|
|
}
|
2022-03-25 21:42:28 +01:00
|
|
|
}
|
2022-02-04 16:15:19 +01:00
|
|
|
|
|
|
|
if rmsg.Username == "" || rmsg.Username == "Channel_Bot" {
|
|
|
|
rmsg.Username = message.SenderChat.UserName
|
|
|
|
|
|
|
|
if rmsg.Username == "" || rmsg.Username == "Channel_Bot" {
|
|
|
|
rmsg.Username = message.SenderChat.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// only download avatars if we have a place to upload them (configured mediaserver)
|
|
|
|
if b.General.MediaServerUpload != "" || (b.General.MediaServerDownload != "" && b.General.MediaDownloadPath != "") {
|
|
|
|
b.handleDownloadAvatar(message.SenderChat.ID, rmsg.Channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:02:31 +01:00
|
|
|
// Fallback on author signature (used in "channel" type of chat)
|
|
|
|
if rmsg.Username == "" && message.AuthorSignature != "" {
|
|
|
|
rmsg.Username = message.AuthorSignature
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
// if we really didn't find a username, set it to unknown
|
|
|
|
if rmsg.Username == "" {
|
|
|
|
rmsg.Username = unknownUser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|
|
|
for update := range updates {
|
|
|
|
b.Log.Debugf("== Receiving event: %#v", update.Message)
|
|
|
|
|
2019-04-05 00:02:26 +02:00
|
|
|
if update.Message == nil && update.ChannelPost == nil &&
|
|
|
|
update.EditedMessage == nil && update.EditedChannelPost == nil {
|
2022-09-05 19:31:45 +02:00
|
|
|
b.Log.Info("Received event without messages, skipping.")
|
2018-11-28 10:57:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-04 16:15:19 +01:00
|
|
|
if b.GetInt("debuglevel") == 1 {
|
|
|
|
spew.Dump(update.Message)
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
var message *tgbotapi.Message
|
|
|
|
|
|
|
|
rmsg := config.Message{Account: b.Account, Extra: make(map[string][]interface{})}
|
|
|
|
|
|
|
|
// handle channels
|
2018-11-29 23:03:50 +01:00
|
|
|
message = b.handleChannels(&rmsg, message, update)
|
2018-11-28 10:57:59 +01:00
|
|
|
|
|
|
|
// handle groups
|
2018-11-29 23:03:50 +01:00
|
|
|
message = b.handleGroups(&rmsg, message, update)
|
2018-11-28 10:57:59 +01:00
|
|
|
|
2019-04-05 00:04:08 +02:00
|
|
|
if message == nil {
|
|
|
|
b.Log.Error("message is nil, this shouldn't happen.")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
// set the ID's from the channel or group message
|
|
|
|
rmsg.ID = strconv.Itoa(message.MessageID)
|
|
|
|
rmsg.Channel = strconv.FormatInt(message.Chat.ID, 10)
|
2023-03-14 23:03:04 +01:00
|
|
|
if message.MessageThreadID != 0 {
|
|
|
|
rmsg.Channel += "/" + strconv.Itoa(message.MessageThreadID)
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
|
2022-03-25 21:58:52 +01:00
|
|
|
// preserve threading from telegram reply
|
2023-03-14 23:03:04 +01:00
|
|
|
if message.ReplyToMessage != nil &&
|
|
|
|
// Used to check if the message was a reply to the root topic
|
|
|
|
!(message.ReplyToMessage.MessageID == message.MessageThreadID) {
|
2022-03-25 21:58:52 +01:00
|
|
|
rmsg.ParentID = strconv.Itoa(message.ReplyToMessage.MessageID)
|
|
|
|
}
|
|
|
|
|
2022-03-19 11:34:46 +01:00
|
|
|
// handle entities (adding URLs)
|
|
|
|
b.handleEntities(&rmsg, message)
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
// handle username
|
|
|
|
b.handleUsername(&rmsg, message)
|
|
|
|
|
|
|
|
// handle any downloads
|
|
|
|
err := b.handleDownload(&rmsg, message)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("download failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle forwarded messages
|
|
|
|
b.handleForwarded(&rmsg, message)
|
|
|
|
|
|
|
|
// quote the previous message
|
|
|
|
b.handleQuoting(&rmsg, message)
|
|
|
|
|
|
|
|
if rmsg.Text != "" || len(rmsg.Extra) > 0 {
|
2022-03-12 00:19:02 +01:00
|
|
|
// Comment the next line out due to avoid removing empty lines in Telegram
|
|
|
|
// rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text)
|
2018-11-28 10:57:59 +01:00
|
|
|
// channels don't have (always?) user information. see #410
|
|
|
|
if message.From != nil {
|
2021-12-12 00:35:32 +01:00
|
|
|
rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.FormatInt(message.From.ID, 10), b.General)
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", rmsg.Username, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleDownloadAvatar downloads the avatar of userid from channel
|
|
|
|
// sends a EVENT_AVATAR_DOWNLOAD message to the gateway if successful.
|
|
|
|
// logs an error message if it fails
|
2021-12-12 00:35:32 +01:00
|
|
|
func (b *Btelegram) handleDownloadAvatar(userid int64, channel string) {
|
2021-02-25 23:28:54 +01:00
|
|
|
rmsg := config.Message{
|
|
|
|
Username: "system",
|
|
|
|
Text: "avatar",
|
|
|
|
Channel: channel,
|
|
|
|
Account: b.Account,
|
2021-12-12 00:35:32 +01:00
|
|
|
UserID: strconv.FormatInt(userid, 10),
|
2021-02-25 23:28:54 +01:00
|
|
|
Event: config.EventAvatarDownload,
|
|
|
|
Extra: make(map[string][]interface{}),
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
|
2021-12-12 00:35:32 +01:00
|
|
|
if _, ok := b.avatarMap[strconv.FormatInt(userid, 10)]; ok {
|
|
|
|
return
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
|
2021-12-12 00:35:32 +01:00
|
|
|
photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1})
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Userprofile download failed for %#v %s", userid, err)
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
|
2021-12-12 00:35:32 +01:00
|
|
|
if len(photos.Photos) > 0 {
|
|
|
|
photo := photos.Photos[0][0]
|
|
|
|
url := b.getFileDirectURL(photo.FileID)
|
|
|
|
name := strconv.FormatInt(userid, 10) + ".png"
|
|
|
|
b.Log.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize)
|
|
|
|
|
|
|
|
err := helper.HandleDownloadSize(b.Log, &rmsg, name, int64(photo.FileSize), b.General)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Error(err)
|
|
|
|
return
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
2021-12-12 00:35:32 +01:00
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("download %s failed %#v", url, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.HandleDownloadData(b.Log, &rmsg, name, rmsg.Text, "", data, b.General)
|
|
|
|
b.Remote <- rmsg
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Support Telegram animated stickers (tgs) format (#1173)
This is half a fix for #874
This patch introduces a new config flag:
- MediaConvertTgs
These need to be treated independently from the existing
MediaConvertWebPToPNG flag because Tgs→WebP results in an
*animated* WebP, and the WebP→PNG converter can't handle
animated WebP files yet.
Furthermore, some platforms (like discord) don't even support
animated WebP files, so the user may want to fall back to
static PNGs (not APNGs).
The final reason why this is only half a fix is that this
introduces an external dependency, namely lottie, to be
installed like this:
$ pip3 install lottie cairosvg
This patch works by writing the tgs to a temporary file in /tmp,
calling lottie to convert it (this conversion may take several seconds!),
and then deleting the temporary file.
The temporary file is absolutely necessary, as lottie refuses to
work on non-seekable files.
If anyone comes up with a reasonable use case where /tmp is
unavailable, I can add yet another config option for that, if desired.
Telegram will bail out if the option is configured but lottie isn't found.
2020-08-23 22:34:28 +02:00
|
|
|
func (b *Btelegram) maybeConvertTgs(name *string, data *[]byte) {
|
2021-08-24 22:32:50 +02:00
|
|
|
format := b.GetString("MediaConvertTgs")
|
|
|
|
if helper.SupportsFormat(format) {
|
|
|
|
b.Log.Debugf("Format supported by %s, converting %v", helper.LottieBackend(), name)
|
|
|
|
} else {
|
Support Telegram animated stickers (tgs) format (#1173)
This is half a fix for #874
This patch introduces a new config flag:
- MediaConvertTgs
These need to be treated independently from the existing
MediaConvertWebPToPNG flag because Tgs→WebP results in an
*animated* WebP, and the WebP→PNG converter can't handle
animated WebP files yet.
Furthermore, some platforms (like discord) don't even support
animated WebP files, so the user may want to fall back to
static PNGs (not APNGs).
The final reason why this is only half a fix is that this
introduces an external dependency, namely lottie, to be
installed like this:
$ pip3 install lottie cairosvg
This patch works by writing the tgs to a temporary file in /tmp,
calling lottie to convert it (this conversion may take several seconds!),
and then deleting the temporary file.
The temporary file is absolutely necessary, as lottie refuses to
work on non-seekable files.
If anyone comes up with a reasonable use case where /tmp is
unavailable, I can add yet another config option for that, if desired.
Telegram will bail out if the option is configured but lottie isn't found.
2020-08-23 22:34:28 +02:00
|
|
|
// Otherwise, no conversion was requested. Trying to run the usual webp
|
|
|
|
// converter would fail, because '.tgs.webp' is actually a gzipped JSON
|
|
|
|
// file, and has nothing to do with WebP.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := helper.ConvertTgsToX(data, format, b.Log)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("conversion failed: %v", err)
|
|
|
|
} else {
|
|
|
|
*name = strings.Replace(*name, "tgs.webp", format, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) maybeConvertWebp(name *string, data *[]byte) {
|
|
|
|
if b.GetBool("MediaConvertWebPToPNG") {
|
|
|
|
b.Log.Debugf("WebP to PNG conversion enabled, converting %v", name)
|
|
|
|
err := helper.ConvertWebPToPNG(data)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("conversion failed: %v", err)
|
|
|
|
} else {
|
|
|
|
*name = strings.Replace(*name, ".webp", ".png", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
// handleDownloadFile handles file download
|
|
|
|
func (b *Btelegram) handleDownload(rmsg *config.Message, message *tgbotapi.Message) error {
|
2023-03-14 23:03:04 +01:00
|
|
|
size := int64(0)
|
2018-11-28 10:57:59 +01:00
|
|
|
var url, name, text string
|
|
|
|
switch {
|
|
|
|
case message.Sticker != nil:
|
|
|
|
text, name, url = b.getDownloadInfo(message.Sticker.FileID, ".webp", true)
|
2023-03-14 23:03:04 +01:00
|
|
|
size = int64(message.Sticker.FileSize)
|
2018-11-28 10:57:59 +01:00
|
|
|
case message.Voice != nil:
|
|
|
|
text, name, url = b.getDownloadInfo(message.Voice.FileID, ".ogg", true)
|
|
|
|
size = message.Voice.FileSize
|
|
|
|
case message.Video != nil:
|
|
|
|
text, name, url = b.getDownloadInfo(message.Video.FileID, "", true)
|
|
|
|
size = message.Video.FileSize
|
|
|
|
case message.Audio != nil:
|
|
|
|
text, name, url = b.getDownloadInfo(message.Audio.FileID, "", true)
|
|
|
|
size = message.Audio.FileSize
|
|
|
|
case message.Document != nil:
|
|
|
|
_, _, url = b.getDownloadInfo(message.Document.FileID, "", false)
|
|
|
|
size = message.Document.FileSize
|
|
|
|
name = message.Document.FileName
|
|
|
|
text = " " + message.Document.FileName + " : " + url
|
|
|
|
case message.Photo != nil:
|
2021-12-12 00:35:32 +01:00
|
|
|
photos := message.Photo
|
2023-03-14 23:03:04 +01:00
|
|
|
size = int64(photos[len(photos)-1].FileSize)
|
2018-11-28 10:57:59 +01:00
|
|
|
text, name, url = b.getDownloadInfo(photos[len(photos)-1].FileID, "", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if name is empty we didn't match a thing to download
|
|
|
|
if name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// use the URL instead of native upload
|
|
|
|
if b.GetBool("UseInsecureURL") {
|
|
|
|
b.Log.Debugf("Setting message text to :%s", text)
|
|
|
|
rmsg.Text += text
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
|
|
|
err := helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Support Telegram animated stickers (tgs) format (#1173)
This is half a fix for #874
This patch introduces a new config flag:
- MediaConvertTgs
These need to be treated independently from the existing
MediaConvertWebPToPNG flag because Tgs→WebP results in an
*animated* WebP, and the WebP→PNG converter can't handle
animated WebP files yet.
Furthermore, some platforms (like discord) don't even support
animated WebP files, so the user may want to fall back to
static PNGs (not APNGs).
The final reason why this is only half a fix is that this
introduces an external dependency, namely lottie, to be
installed like this:
$ pip3 install lottie cairosvg
This patch works by writing the tgs to a temporary file in /tmp,
calling lottie to convert it (this conversion may take several seconds!),
and then deleting the temporary file.
The temporary file is absolutely necessary, as lottie refuses to
work on non-seekable files.
If anyone comes up with a reasonable use case where /tmp is
unavailable, I can add yet another config option for that, if desired.
Telegram will bail out if the option is configured but lottie isn't found.
2020-08-23 22:34:28 +02:00
|
|
|
|
|
|
|
if strings.HasSuffix(name, ".tgs.webp") {
|
|
|
|
b.maybeConvertTgs(&name, data)
|
|
|
|
} else if strings.HasSuffix(name, ".webp") {
|
|
|
|
b.maybeConvertWebp(&name, data)
|
2019-02-27 00:41:50 +01:00
|
|
|
}
|
Support Telegram animated stickers (tgs) format (#1173)
This is half a fix for #874
This patch introduces a new config flag:
- MediaConvertTgs
These need to be treated independently from the existing
MediaConvertWebPToPNG flag because Tgs→WebP results in an
*animated* WebP, and the WebP→PNG converter can't handle
animated WebP files yet.
Furthermore, some platforms (like discord) don't even support
animated WebP files, so the user may want to fall back to
static PNGs (not APNGs).
The final reason why this is only half a fix is that this
introduces an external dependency, namely lottie, to be
installed like this:
$ pip3 install lottie cairosvg
This patch works by writing the tgs to a temporary file in /tmp,
calling lottie to convert it (this conversion may take several seconds!),
and then deleting the temporary file.
The temporary file is absolutely necessary, as lottie refuses to
work on non-seekable files.
If anyone comes up with a reasonable use case where /tmp is
unavailable, I can add yet another config option for that, if desired.
Telegram will bail out if the option is configured but lottie isn't found.
2020-08-23 22:34:28 +02:00
|
|
|
|
2021-01-02 00:55:20 +01:00
|
|
|
// rename .oga to .ogg https://github.com/42wim/matterbridge/issues/906#issuecomment-741793512
|
|
|
|
if strings.HasSuffix(name, ".oga") && message.Audio != nil {
|
|
|
|
name = strings.Replace(name, ".oga", ".ogg", 1)
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
helper.HandleDownloadData(b.Log, rmsg, name, message.Caption, "", data, b.General)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) getDownloadInfo(id string, suffix string, urlpart bool) (string, string, string) {
|
|
|
|
url := b.getFileDirectURL(id)
|
|
|
|
name := ""
|
|
|
|
if urlpart {
|
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
|
|
|
}
|
2022-04-17 13:35:11 +02:00
|
|
|
if suffix != "" && !strings.HasSuffix(name, suffix) && !strings.HasSuffix(name, ".webm") {
|
2018-11-28 10:57:59 +01:00
|
|
|
name += suffix
|
|
|
|
}
|
|
|
|
text := " " + url
|
|
|
|
return text, name, url
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:50:08 +01:00
|
|
|
// handleDelete handles message deleting
|
|
|
|
func (b *Btelegram) handleDelete(msg *config.Message, chatid int64) (string, error) {
|
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2021-12-12 00:35:32 +01:00
|
|
|
|
2018-12-12 23:50:08 +01:00
|
|
|
msgid, err := strconv.Atoi(msg.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-12-12 00:35:32 +01:00
|
|
|
|
|
|
|
cfg := tgbotapi.NewDeleteMessage(chatid, msgid)
|
2022-05-01 22:00:50 +02:00
|
|
|
_, err = b.c.Request(cfg)
|
2021-12-12 00:35:32 +01:00
|
|
|
|
2018-12-12 23:50:08 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleEdit handles message editing.
|
|
|
|
func (b *Btelegram) handleEdit(msg *config.Message, chatid int64) (string, error) {
|
|
|
|
msgid, err := strconv.Atoi(msg.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
|
|
|
|
b.Log.Debug("Using mode HTML - nick only")
|
|
|
|
msg.Text = html.EscapeString(msg.Text)
|
|
|
|
}
|
|
|
|
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
|
|
|
switch b.GetString("MessageFormat") {
|
|
|
|
case HTMLFormat:
|
|
|
|
b.Log.Debug("Using mode HTML")
|
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
case "Markdown":
|
|
|
|
b.Log.Debug("Using mode markdown")
|
|
|
|
m.ParseMode = tgbotapi.ModeMarkdown
|
2020-07-12 22:40:22 +02:00
|
|
|
case MarkdownV2:
|
|
|
|
b.Log.Debug("Using mode MarkdownV2")
|
|
|
|
m.ParseMode = MarkdownV2
|
2018-12-12 23:50:08 +01:00
|
|
|
}
|
|
|
|
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
|
|
|
|
b.Log.Debug("Using mode HTML - nick only")
|
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
|
|
|
_, err = b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:57:59 +01:00
|
|
|
// handleUploadFile handles native upload of files
|
2023-03-14 23:03:04 +01:00
|
|
|
func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64, threadid int, parentID int) (string, error) {
|
2022-03-30 22:23:52 +02:00
|
|
|
var media []interface{}
|
2018-11-28 10:57:59 +01:00
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
file := tgbotapi.FileBytes{
|
|
|
|
Name: fi.Name,
|
|
|
|
Bytes: *fi.Data,
|
|
|
|
}
|
2022-11-26 23:50:46 +01:00
|
|
|
|
|
|
|
if b.GetString("MessageFormat") == HTMLFormat {
|
|
|
|
fi.Comment = makeHTML(html.EscapeString(fi.Comment))
|
|
|
|
}
|
|
|
|
|
2021-04-03 23:15:19 +02:00
|
|
|
switch filepath.Ext(fi.Name) {
|
|
|
|
case ".jpg", ".jpe", ".png":
|
2022-03-30 22:23:52 +02:00
|
|
|
pc := tgbotapi.NewInputMediaPhoto(file)
|
|
|
|
if fi.Comment != "" {
|
|
|
|
pc.Caption, pc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
|
|
|
}
|
|
|
|
media = append(media, pc)
|
2021-04-03 23:15:19 +02:00
|
|
|
case ".mp4", ".m4v":
|
2022-03-30 22:23:52 +02:00
|
|
|
vc := tgbotapi.NewInputMediaVideo(file)
|
|
|
|
if fi.Comment != "" {
|
|
|
|
vc.Caption, vc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
|
|
|
}
|
|
|
|
media = append(media, vc)
|
2021-04-03 23:15:19 +02:00
|
|
|
case ".mp3", ".oga":
|
2022-03-30 22:23:52 +02:00
|
|
|
ac := tgbotapi.NewInputMediaAudio(file)
|
|
|
|
if fi.Comment != "" {
|
|
|
|
ac.Caption, ac.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
|
|
|
}
|
|
|
|
media = append(media, ac)
|
2021-04-03 23:15:19 +02:00
|
|
|
case ".ogg":
|
2021-12-12 00:35:32 +01:00
|
|
|
voc := tgbotapi.NewVoice(chatid, file)
|
2021-04-03 23:15:19 +02:00
|
|
|
voc.Caption, voc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
2022-03-30 22:23:52 +02:00
|
|
|
voc.ReplyToMessageID = parentID
|
|
|
|
res, err := b.c.Send(voc)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strconv.Itoa(res.MessageID), nil
|
2021-04-03 23:15:19 +02:00
|
|
|
default:
|
2022-03-30 22:23:52 +02:00
|
|
|
dc := tgbotapi.NewInputMediaDocument(file)
|
|
|
|
if fi.Comment != "" {
|
|
|
|
dc.Caption, dc.ParseMode = TGGetParseMode(b, msg.Username, fi.Comment)
|
|
|
|
}
|
|
|
|
media = append(media, dc)
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-30 22:23:52 +02:00
|
|
|
|
2023-03-14 23:03:04 +01:00
|
|
|
return b.sendMediaFiles(msg, chatid, threadid, parentID, media)
|
2018-11-28 10:57:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) handleQuote(message, quoteNick, quoteMessage string) string {
|
|
|
|
format := b.GetString("quoteformat")
|
|
|
|
if format == "" {
|
|
|
|
format = "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"
|
|
|
|
}
|
2021-02-25 23:28:54 +01:00
|
|
|
quoteMessagelength := len([]rune(quoteMessage))
|
2020-01-30 00:02:33 +01:00
|
|
|
if b.GetInt("QuoteLengthLimit") != 0 && quoteMessagelength >= b.GetInt("QuoteLengthLimit") {
|
|
|
|
runes := []rune(quoteMessage)
|
|
|
|
quoteMessage = string(runes[0:b.GetInt("QuoteLengthLimit")])
|
|
|
|
if quoteMessagelength > b.GetInt("QuoteLengthLimit") {
|
|
|
|
quoteMessage += "..."
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 10:57:59 +01:00
|
|
|
format = strings.Replace(format, "{MESSAGE}", message, -1)
|
|
|
|
format = strings.Replace(format, "{QUOTENICK}", quoteNick, -1)
|
|
|
|
format = strings.Replace(format, "{QUOTEMESSAGE}", quoteMessage, -1)
|
|
|
|
return format
|
|
|
|
}
|
2019-03-02 22:38:44 +01:00
|
|
|
|
|
|
|
// handleEntities handles messageEntities
|
|
|
|
func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Message) {
|
|
|
|
if message.Entities == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-07 21:26:28 +01:00
|
|
|
|
2021-12-10 22:13:54 +01:00
|
|
|
indexMovedBy := 0
|
2022-04-22 01:00:57 +02:00
|
|
|
prevLinkOffset := -1
|
2021-12-07 21:26:28 +01:00
|
|
|
|
2021-12-12 00:35:32 +01:00
|
|
|
for _, e := range message.Entities {
|
2022-03-19 11:34:46 +01:00
|
|
|
|
|
|
|
asRunes := utf16.Encode([]rune(rmsg.Text))
|
|
|
|
|
2019-03-02 22:38:44 +01:00
|
|
|
if e.Type == "text_link" {
|
2022-03-19 11:34:46 +01:00
|
|
|
offset := e.Offset + indexMovedBy
|
2019-03-02 22:38:44 +01:00
|
|
|
url, err := e.ParseURL()
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("entity text_link url parse failed: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-07-08 22:19:45 +02:00
|
|
|
utfEncodedString := utf16.Encode([]rune(rmsg.Text))
|
2022-03-19 11:34:46 +01:00
|
|
|
if offset+e.Length > len(utfEncodedString) {
|
|
|
|
b.Log.Errorf("entity length is too long %d > %d", offset+e.Length, len(utfEncodedString))
|
2019-07-08 22:19:45 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-03-19 11:34:46 +01:00
|
|
|
rmsg.Text = string(utf16.Decode(asRunes[:offset+e.Length])) + " (" + url.String() + ")" + string(utf16.Decode(asRunes[offset+e.Length:]))
|
|
|
|
indexMovedBy += len(url.String()) + 3
|
2022-04-22 01:00:57 +02:00
|
|
|
prevLinkOffset = e.Offset
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Offset == prevLinkOffset {
|
|
|
|
continue
|
2019-03-02 22:38:44 +01:00
|
|
|
}
|
2021-12-07 21:26:28 +01:00
|
|
|
|
|
|
|
if e.Type == "code" {
|
2021-12-10 22:13:54 +01:00
|
|
|
offset := e.Offset + indexMovedBy
|
2022-03-19 11:34:46 +01:00
|
|
|
rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "`" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "`" + string(utf16.Decode(asRunes[offset+e.Length:]))
|
2021-12-07 21:26:28 +01:00
|
|
|
indexMovedBy += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Type == "pre" {
|
2021-12-10 22:13:54 +01:00
|
|
|
offset := e.Offset + indexMovedBy
|
2022-03-19 11:34:46 +01:00
|
|
|
rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "```\n" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "```\n" + string(utf16.Decode(asRunes[offset+e.Length:]))
|
2021-12-07 21:26:28 +01:00
|
|
|
indexMovedBy += 8
|
|
|
|
}
|
2022-03-12 00:19:02 +01:00
|
|
|
|
|
|
|
if e.Type == "bold" {
|
|
|
|
offset := e.Offset + indexMovedBy
|
2022-03-19 11:34:46 +01:00
|
|
|
rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "*" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "*" + string(utf16.Decode(asRunes[offset+e.Length:]))
|
2022-03-12 00:19:02 +01:00
|
|
|
indexMovedBy += 2
|
|
|
|
}
|
|
|
|
if e.Type == "italic" {
|
|
|
|
offset := e.Offset + indexMovedBy
|
2022-03-19 11:34:46 +01:00
|
|
|
rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "_" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "_" + string(utf16.Decode(asRunes[offset+e.Length:]))
|
2022-03-12 00:19:02 +01:00
|
|
|
indexMovedBy += 2
|
|
|
|
}
|
|
|
|
if e.Type == "strike" {
|
|
|
|
offset := e.Offset + indexMovedBy
|
2022-03-19 11:34:46 +01:00
|
|
|
rmsg.Text = string(utf16.Decode(asRunes[:offset])) + "~" + string(utf16.Decode(asRunes[offset:offset+e.Length])) + "~" + string(utf16.Decode(asRunes[offset+e.Length:]))
|
2022-03-12 00:19:02 +01:00
|
|
|
indexMovedBy += 2
|
|
|
|
}
|
2019-03-02 22:38:44 +01:00
|
|
|
}
|
|
|
|
}
|