mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-24 04:02:37 +01:00
Refactor telegram
This commit is contained in:
parent
064b6a915f
commit
ad6440b603
@ -6,11 +6,11 @@ import (
|
|||||||
"html"
|
"html"
|
||||||
)
|
)
|
||||||
|
|
||||||
type customHtml struct {
|
type customHTML struct {
|
||||||
blackfriday.Renderer
|
blackfriday.Renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) Paragraph(out *bytes.Buffer, text func() bool) {
|
func (options *customHTML) Paragraph(out *bytes.Buffer, text func() bool) {
|
||||||
marker := out.Len()
|
marker := out.Len()
|
||||||
|
|
||||||
if !text() {
|
if !text() {
|
||||||
@ -20,32 +20,32 @@ func (options *customHtml) Paragraph(out *bytes.Buffer, text func() bool) {
|
|||||||
out.WriteString("\n")
|
out.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
func (options *customHTML) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
||||||
out.WriteString("<pre>")
|
out.WriteString("<pre>")
|
||||||
|
|
||||||
out.WriteString(html.EscapeString(string(text)))
|
out.WriteString(html.EscapeString(string(text)))
|
||||||
out.WriteString("</pre>\n")
|
out.WriteString("</pre>\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) Header(out *bytes.Buffer, text func() bool, level int, id string) {
|
func (options *customHTML) Header(out *bytes.Buffer, text func() bool, level int, id string) {
|
||||||
options.Paragraph(out, text)
|
options.Paragraph(out, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) HRule(out *bytes.Buffer) {
|
func (options *customHTML) HRule(out *bytes.Buffer) {
|
||||||
out.WriteByte('\n')
|
out.WriteByte('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) BlockQuote(out *bytes.Buffer, text []byte) {
|
func (options *customHTML) BlockQuote(out *bytes.Buffer, text []byte) {
|
||||||
out.WriteString("> ")
|
out.WriteString("> ")
|
||||||
out.Write(text)
|
out.Write(text)
|
||||||
out.WriteByte('\n')
|
out.WriteByte('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) List(out *bytes.Buffer, text func() bool, flags int) {
|
func (options *customHTML) List(out *bytes.Buffer, text func() bool, flags int) {
|
||||||
options.Paragraph(out, text)
|
options.Paragraph(out, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (options *customHtml) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
func (options *customHTML) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
||||||
out.WriteString("- ")
|
out.WriteString("- ")
|
||||||
out.Write(text)
|
out.Write(text)
|
||||||
out.WriteByte('\n')
|
out.WriteByte('\n')
|
||||||
@ -53,7 +53,7 @@ func (options *customHtml) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
|||||||
|
|
||||||
func makeHTML(input string) string {
|
func makeHTML(input string) string {
|
||||||
return string(blackfriday.Markdown([]byte(input),
|
return string(blackfriday.Markdown([]byte(input),
|
||||||
&customHtml{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")},
|
&customHTML{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")},
|
||||||
blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
|
blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
|
||||||
blackfriday.EXTENSION_FENCED_CODE|
|
blackfriday.EXTENSION_FENCED_CODE|
|
||||||
blackfriday.EXTENSION_AUTOLINK|
|
blackfriday.EXTENSION_AUTOLINK|
|
||||||
|
@ -50,7 +50,6 @@ func (b *Btelegram) Connect() error {
|
|||||||
|
|
||||||
func (b *Btelegram) Disconnect() error {
|
func (b *Btelegram) Disconnect() error {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
||||||
@ -59,6 +58,8 @@ func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
|||||||
|
|
||||||
func (b *Btelegram) Send(msg config.Message) (string, error) {
|
func (b *Btelegram) Send(msg config.Message) (string, error) {
|
||||||
flog.Debugf("Receiving %#v", msg)
|
flog.Debugf("Receiving %#v", msg)
|
||||||
|
|
||||||
|
// get the chatid
|
||||||
chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
|
chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -66,20 +67,14 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
// map the file SHA to our user (caches the avatar)
|
// map the file SHA to our user (caches the avatar)
|
||||||
if msg.Event == config.EVENT_AVATAR_DOWNLOAD {
|
if msg.Event == config.EVENT_AVATAR_DOWNLOAD {
|
||||||
fi := msg.Extra["file"][0].(config.FileInfo)
|
return b.cacheAvatar(&msg)
|
||||||
/* if we have a sha we have successfully uploaded the file to the media server,
|
|
||||||
so we can now cache the sha */
|
|
||||||
if fi.SHA != "" {
|
|
||||||
flog.Debugf("Added %s to %s in avatarMap", fi.SHA, msg.UserID)
|
|
||||||
b.avatarMap[msg.UserID] = fi.SHA
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Config.MessageFormat == "HTML" {
|
if b.Config.MessageFormat == "HTML" {
|
||||||
msg.Text = makeHTML(msg.Text)
|
msg.Text = makeHTML(msg.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete message
|
||||||
if msg.Event == config.EVENT_MSG_DELETE {
|
if msg.Event == config.EVENT_MSG_DELETE {
|
||||||
if msg.ID == "" {
|
if msg.ID == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
@ -92,6 +87,17 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upload a file if it exists
|
||||||
|
if msg.Extra != nil {
|
||||||
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
|
b.sendMessage(chatid, rmsg.Username+rmsg.Text)
|
||||||
|
}
|
||||||
|
// check if we have files to upload (from slack, telegram or mattermost)
|
||||||
|
if len(msg.Extra["file"]) > 0 {
|
||||||
|
b.handleUploadFile(&msg, chatid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// edit the message if we have a msg ID
|
// edit the message if we have a msg ID
|
||||||
if msg.ID != "" {
|
if msg.ID != "" {
|
||||||
msgid, err := strconv.Atoi(msg.ID)
|
msgid, err := strconv.Atoi(msg.ID)
|
||||||
@ -114,111 +120,79 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Extra != nil {
|
// Post normal message
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
|
||||||
b.sendMessage(chatid, rmsg.Username+rmsg.Text)
|
|
||||||
}
|
|
||||||
// check if we have files to upload (from slack, telegram or mattermost)
|
|
||||||
if len(msg.Extra["file"]) > 0 {
|
|
||||||
var c tgbotapi.Chattable
|
|
||||||
for _, f := range msg.Extra["file"] {
|
|
||||||
fi := f.(config.FileInfo)
|
|
||||||
file := tgbotapi.FileBytes{fi.Name, *fi.Data}
|
|
||||||
re := regexp.MustCompile(".(jpg|png)$")
|
|
||||||
if re.MatchString(fi.Name) {
|
|
||||||
c = tgbotapi.NewPhotoUpload(chatid, file)
|
|
||||||
} else {
|
|
||||||
c = tgbotapi.NewDocumentUpload(chatid, file)
|
|
||||||
}
|
|
||||||
_, err := b.c.Send(c)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("file upload failed: %#v", err)
|
|
||||||
}
|
|
||||||
if fi.Comment != "" {
|
|
||||||
b.sendMessage(chatid, msg.Username+fi.Comment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return b.sendMessage(chatid, msg.Username+msg.Text)
|
return b.sendMessage(chatid, msg.Username+msg.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
||||||
for update := range updates {
|
for update := range updates {
|
||||||
flog.Debugf("Receiving from telegram: %#v", update.Message)
|
flog.Debugf("Receiving from telegram: %#v", update.Message)
|
||||||
if update.Message == nil {
|
|
||||||
|
if update.Message == nil && update.ChannelPost == nil {
|
||||||
flog.Error("Getting nil messages, this shouldn't happen.")
|
flog.Error("Getting nil messages, this shouldn't happen.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var message *tgbotapi.Message
|
|
||||||
username := ""
|
|
||||||
channel := ""
|
|
||||||
text := ""
|
|
||||||
|
|
||||||
fmsg := config.Message{Extra: make(map[string][]interface{})}
|
var message *tgbotapi.Message
|
||||||
|
|
||||||
|
rmsg := config.Message{Account: b.Account, Extra: make(map[string][]interface{})}
|
||||||
|
|
||||||
// handle channels
|
// handle channels
|
||||||
if update.ChannelPost != nil {
|
if update.ChannelPost != nil {
|
||||||
message = update.ChannelPost
|
message = update.ChannelPost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// edited channel message
|
||||||
if update.EditedChannelPost != nil && !b.Config.EditDisable {
|
if update.EditedChannelPost != nil && !b.Config.EditDisable {
|
||||||
message = update.EditedChannelPost
|
message = update.EditedChannelPost
|
||||||
message.Text = message.Text + b.Config.EditSuffix
|
rmsg.Text = rmsg.Text + message.Text + b.Config.EditSuffix
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle groups
|
// handle groups
|
||||||
if update.Message != nil {
|
if update.Message != nil {
|
||||||
message = update.Message
|
message = update.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// edited group message
|
||||||
if update.EditedMessage != nil && !b.Config.EditDisable {
|
if update.EditedMessage != nil && !b.Config.EditDisable {
|
||||||
message = update.EditedMessage
|
message = update.EditedMessage
|
||||||
message.Text = message.Text + b.Config.EditSuffix
|
rmsg.Text = rmsg.Text + message.Text + b.Config.EditSuffix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the ID's from the channel or group message
|
||||||
|
rmsg.ID = strconv.Itoa(message.MessageID)
|
||||||
|
rmsg.UserID = strconv.Itoa(message.From.ID)
|
||||||
|
rmsg.Channel = strconv.FormatInt(message.Chat.ID, 10)
|
||||||
|
|
||||||
|
// handle username
|
||||||
if message.From != nil {
|
if message.From != nil {
|
||||||
if b.Config.UseFirstName {
|
if b.Config.UseFirstName {
|
||||||
username = message.From.FirstName
|
rmsg.Username = message.From.FirstName
|
||||||
}
|
}
|
||||||
if username == "" {
|
if rmsg.Username == "" {
|
||||||
username = message.From.UserName
|
rmsg.Username = message.From.UserName
|
||||||
if username == "" {
|
if rmsg.Username == "" {
|
||||||
username = message.From.FirstName
|
rmsg.Username = message.From.FirstName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
text = message.Text
|
|
||||||
channel = strconv.FormatInt(message.Chat.ID, 10)
|
|
||||||
// only download avatars if we have a place to upload them (configured mediaserver)
|
// only download avatars if we have a place to upload them (configured mediaserver)
|
||||||
if b.General.MediaServerUpload != "" {
|
if b.General.MediaServerUpload != "" {
|
||||||
b.handleDownloadAvatar(message.From.ID, channel)
|
b.handleDownloadAvatar(message.From.ID, rmsg.Channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if username == "" {
|
// if we really didn't find a username, set it to unknown
|
||||||
username = "unknown"
|
if rmsg.Username == "" {
|
||||||
}
|
rmsg.Username = "unknown"
|
||||||
if message.Sticker != nil {
|
|
||||||
b.handleDownload(message.Sticker, message.Caption, &fmsg)
|
|
||||||
}
|
|
||||||
if message.Video != nil {
|
|
||||||
b.handleDownload(message.Video, message.Caption, &fmsg)
|
|
||||||
}
|
|
||||||
if message.Photo != nil {
|
|
||||||
b.handleDownload(message.Photo, message.Caption, &fmsg)
|
|
||||||
}
|
|
||||||
if message.Document != nil {
|
|
||||||
b.handleDownload(message.Document, message.Caption, &fmsg)
|
|
||||||
}
|
|
||||||
if message.Voice != nil {
|
|
||||||
b.handleDownload(message.Voice, message.Caption, &fmsg)
|
|
||||||
}
|
|
||||||
if message.Audio != nil {
|
|
||||||
b.handleDownload(message.Audio, message.Caption, &fmsg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If UseInsecureURL is used we'll have a text in fmsg.Text
|
// handle any downloads
|
||||||
if fmsg.Text != "" {
|
err := b.handleDownload(message, &rmsg)
|
||||||
text = text + fmsg.Text
|
if err != nil {
|
||||||
|
flog.Errorf("download failed: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle forwarded messages
|
||||||
if message.ForwardFrom != nil {
|
if message.ForwardFrom != nil {
|
||||||
usernameForward := ""
|
usernameForward := ""
|
||||||
if b.Config.UseFirstName {
|
if b.Config.UseFirstName {
|
||||||
@ -233,7 +207,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
if usernameForward == "" {
|
if usernameForward == "" {
|
||||||
usernameForward = "unknown"
|
usernameForward = "unknown"
|
||||||
}
|
}
|
||||||
text = "Forwarded from " + usernameForward + ": " + text
|
rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
// quote the previous message
|
// quote the previous message
|
||||||
@ -253,15 +227,15 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
if usernameReply == "" {
|
if usernameReply == "" {
|
||||||
usernameReply = "unknown"
|
usernameReply = "unknown"
|
||||||
}
|
}
|
||||||
text = text + " (re @" + usernameReply + ":" + message.ReplyToMessage.Text + ")"
|
rmsg.Text = rmsg.Text + " (re @" + usernameReply + ":" + message.ReplyToMessage.Text + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
if text != "" || len(fmsg.Extra) > 0 {
|
if rmsg.Text != "" || len(rmsg.Extra) > 0 {
|
||||||
avatar := helper.GetAvatar(b.avatarMap, strconv.Itoa(message.From.ID), b.General)
|
rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.Itoa(message.From.ID), b.General)
|
||||||
flog.Debugf("Sending message from %s on %s to gateway", username, b.Account)
|
|
||||||
msg := config.Message{Username: username, Text: text, Channel: channel, Account: b.Account, UserID: strconv.Itoa(message.From.ID), ID: strconv.Itoa(message.MessageID), Extra: fmsg.Extra, Avatar: avatar}
|
flog.Debugf("Sending message from %s on %s to gateway", rmsg.Username, b.Account)
|
||||||
flog.Debugf("Message is %#v", msg)
|
flog.Debugf("Message is %#v", rmsg)
|
||||||
b.Remote <- msg
|
b.Remote <- rmsg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -278,60 +252,42 @@ func (b *Btelegram) getFileDirectURL(id string) string {
|
|||||||
// sends a EVENT_AVATAR_DOWNLOAD message to the gateway if successful.
|
// sends a EVENT_AVATAR_DOWNLOAD message to the gateway if successful.
|
||||||
// logs an error message if it fails
|
// logs an error message if it fails
|
||||||
func (b *Btelegram) handleDownloadAvatar(userid int, channel string) {
|
func (b *Btelegram) handleDownloadAvatar(userid int, channel string) {
|
||||||
msg := config.Message{Username: "system", Text: "avatar", Channel: channel, Account: b.Account, UserID: strconv.Itoa(userid), Event: config.EVENT_AVATAR_DOWNLOAD, Extra: make(map[string][]interface{})}
|
rmsg := config.Message{Username: "system", Text: "avatar", Channel: channel, Account: b.Account, UserID: strconv.Itoa(userid), Event: config.EVENT_AVATAR_DOWNLOAD, Extra: make(map[string][]interface{})}
|
||||||
if _, ok := b.avatarMap[strconv.Itoa(userid)]; !ok {
|
if _, ok := b.avatarMap[strconv.Itoa(userid)]; !ok {
|
||||||
photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1})
|
photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
flog.Errorf("Userprofile download failed for %#v %s", userid, err)
|
flog.Errorf("Userprofile download failed for %#v %s", userid, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(photos.Photos) > 0 {
|
if len(photos.Photos) > 0 {
|
||||||
photo := photos.Photos[0][0]
|
photo := photos.Photos[0][0]
|
||||||
url := b.getFileDirectURL(photo.FileID)
|
url := b.getFileDirectURL(photo.FileID)
|
||||||
name := strconv.Itoa(userid) + ".png"
|
name := strconv.Itoa(userid) + ".png"
|
||||||
flog.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize)
|
flog.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize)
|
||||||
if photo.FileSize <= b.General.MediaDownloadSize {
|
|
||||||
data, err := helper.DownloadFile(url)
|
err := helper.HandleDownloadSize(flog, &rmsg, name, int64(photo.FileSize), b.General)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
flog.Errorf("download %s failed %#v", url, err)
|
flog.Error(err)
|
||||||
} else {
|
return
|
||||||
flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url))
|
|
||||||
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data, Avatar: true})
|
|
||||||
flog.Debugf("Sending avatar download message from %#v on %s to gateway", userid, b.Account)
|
|
||||||
flog.Debugf("Message is %#v", msg)
|
|
||||||
b.Remote <- msg
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, photo.FileSize, b.General.MediaDownloadSize)
|
|
||||||
}
|
}
|
||||||
|
data, err := helper.DownloadFile(url)
|
||||||
|
if err != nil {
|
||||||
|
flog.Errorf("download %s failed %#v", url, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
helper.HandleDownloadData(flog, &rmsg, name, rmsg.Text, "", data, b.General)
|
||||||
|
b.Remote <- rmsg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Btelegram) handleDownload(file interface{}, comment string, msg *config.Message) {
|
// handleDownloadFile handles file download
|
||||||
|
func (b *Btelegram) handleDownload(message *tgbotapi.Message, rmsg *config.Message) error {
|
||||||
size := 0
|
size := 0
|
||||||
url := ""
|
var url, name, text string
|
||||||
name := ""
|
|
||||||
text := ""
|
if message.Sticker != nil {
|
||||||
fileid := ""
|
v := message.Sticker
|
||||||
switch v := file.(type) {
|
|
||||||
case *tgbotapi.Audio:
|
|
||||||
size = v.FileSize
|
|
||||||
url = b.getFileDirectURL(v.FileID)
|
|
||||||
urlPart := strings.Split(url, "/")
|
|
||||||
name = urlPart[len(urlPart)-1]
|
|
||||||
text = " " + url
|
|
||||||
fileid = v.FileID
|
|
||||||
case *tgbotapi.Voice:
|
|
||||||
size = v.FileSize
|
|
||||||
url = b.getFileDirectURL(v.FileID)
|
|
||||||
urlPart := strings.Split(url, "/")
|
|
||||||
name = urlPart[len(urlPart)-1]
|
|
||||||
text = " " + url
|
|
||||||
if !strings.HasSuffix(name, ".ogg") {
|
|
||||||
name = name + ".ogg"
|
|
||||||
}
|
|
||||||
fileid = v.FileID
|
|
||||||
case *tgbotapi.Sticker:
|
|
||||||
size = v.FileSize
|
size = v.FileSize
|
||||||
url = b.getFileDirectURL(v.FileID)
|
url = b.getFileDirectURL(v.FileID)
|
||||||
urlPart := strings.Split(url, "/")
|
urlPart := strings.Split(url, "/")
|
||||||
@ -340,48 +296,93 @@ func (b *Btelegram) handleDownload(file interface{}, comment string, msg *config
|
|||||||
name = name + ".webp"
|
name = name + ".webp"
|
||||||
}
|
}
|
||||||
text = " " + url
|
text = " " + url
|
||||||
fileid = v.FileID
|
}
|
||||||
case *tgbotapi.Video:
|
if message.Video != nil {
|
||||||
|
v := message.Video
|
||||||
size = v.FileSize
|
size = v.FileSize
|
||||||
url = b.getFileDirectURL(v.FileID)
|
url = b.getFileDirectURL(v.FileID)
|
||||||
urlPart := strings.Split(url, "/")
|
urlPart := strings.Split(url, "/")
|
||||||
name = urlPart[len(urlPart)-1]
|
name = urlPart[len(urlPart)-1]
|
||||||
text = " " + url
|
text = " " + url
|
||||||
fileid = v.FileID
|
}
|
||||||
case *[]tgbotapi.PhotoSize:
|
if message.Photo != nil {
|
||||||
photos := *v
|
photos := *message.Photo
|
||||||
size = photos[len(photos)-1].FileSize
|
size = photos[len(photos)-1].FileSize
|
||||||
url = b.getFileDirectURL(photos[len(photos)-1].FileID)
|
url = b.getFileDirectURL(photos[len(photos)-1].FileID)
|
||||||
urlPart := strings.Split(url, "/")
|
urlPart := strings.Split(url, "/")
|
||||||
name = urlPart[len(urlPart)-1]
|
name = urlPart[len(urlPart)-1]
|
||||||
text = " " + url
|
text = " " + url
|
||||||
case *tgbotapi.Document:
|
}
|
||||||
|
if message.Document != nil {
|
||||||
|
v := message.Document
|
||||||
size = v.FileSize
|
size = v.FileSize
|
||||||
url = b.getFileDirectURL(v.FileID)
|
url = b.getFileDirectURL(v.FileID)
|
||||||
name = v.FileName
|
name = v.FileName
|
||||||
text = " " + v.FileName + " : " + url
|
text = " " + v.FileName + " : " + url
|
||||||
fileid = v.FileID
|
|
||||||
}
|
}
|
||||||
|
if message.Voice != nil {
|
||||||
|
v := message.Voice
|
||||||
|
size = v.FileSize
|
||||||
|
url = b.getFileDirectURL(v.FileID)
|
||||||
|
urlPart := strings.Split(url, "/")
|
||||||
|
name = urlPart[len(urlPart)-1]
|
||||||
|
text = " " + url
|
||||||
|
if !strings.HasSuffix(name, ".ogg") {
|
||||||
|
name = name + ".ogg"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if message.Audio != nil {
|
||||||
|
v := message.Audio
|
||||||
|
size = v.FileSize
|
||||||
|
url = b.getFileDirectURL(v.FileID)
|
||||||
|
urlPart := strings.Split(url, "/")
|
||||||
|
name = urlPart[len(urlPart)-1]
|
||||||
|
text = " " + url
|
||||||
|
}
|
||||||
|
// 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.Config.UseInsecureURL {
|
if b.Config.UseInsecureURL {
|
||||||
flog.Debugf("Setting message text to :%s", text)
|
flog.Debugf("Setting message text to :%s", text)
|
||||||
msg.Text = text
|
rmsg.Text = rmsg.Text + text
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
||||||
flog.Debugf("trying to download %#v fileid %#v with size %#v", name, fileid, size)
|
err := helper.HandleDownloadSize(flog, rmsg, name, int64(size), b.General)
|
||||||
if size <= b.General.MediaDownloadSize {
|
if err != nil {
|
||||||
data, err := helper.DownloadFile(url)
|
return err
|
||||||
if err != nil {
|
|
||||||
flog.Errorf("download %s failed %#v", url, err)
|
|
||||||
} else {
|
|
||||||
flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url))
|
|
||||||
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data, Comment: comment})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, size, b.General.MediaDownloadSize)
|
|
||||||
msg.Event = config.EVENT_FILE_FAILURE_SIZE
|
|
||||||
msg.Extra[msg.Event] = append(msg.Extra[msg.Event], config.FileInfo{Name: name, Comment: comment, Size: int64(size)})
|
|
||||||
}
|
}
|
||||||
|
data, err := helper.DownloadFile(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
helper.HandleDownloadData(flog, rmsg, name, message.Caption, "", data, b.General)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleUploadFile handles native upload of files
|
||||||
|
func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string, error) {
|
||||||
|
var c tgbotapi.Chattable
|
||||||
|
for _, f := range msg.Extra["file"] {
|
||||||
|
fi := f.(config.FileInfo)
|
||||||
|
file := tgbotapi.FileBytes{fi.Name, *fi.Data}
|
||||||
|
re := regexp.MustCompile(".(jpg|png)$")
|
||||||
|
if re.MatchString(fi.Name) {
|
||||||
|
c = tgbotapi.NewPhotoUpload(chatid, file)
|
||||||
|
} else {
|
||||||
|
c = tgbotapi.NewDocumentUpload(chatid, file)
|
||||||
|
}
|
||||||
|
_, err := b.c.Send(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("file upload failed: %#v", err)
|
||||||
|
}
|
||||||
|
if fi.Comment != "" {
|
||||||
|
b.sendMessage(chatid, msg.Username+fi.Comment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Btelegram) sendMessage(chatid int64, text string) (string, error) {
|
func (b *Btelegram) sendMessage(chatid int64, text string) (string, error) {
|
||||||
@ -400,3 +401,14 @@ func (b *Btelegram) sendMessage(chatid int64, text string) (string, error) {
|
|||||||
}
|
}
|
||||||
return strconv.Itoa(res.MessageID), nil
|
return strconv.Itoa(res.MessageID), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Btelegram) cacheAvatar(msg *config.Message) (string, error) {
|
||||||
|
fi := msg.Extra["file"][0].(config.FileInfo)
|
||||||
|
/* if we have a sha we have successfully uploaded the file to the media server,
|
||||||
|
so we can now cache the sha */
|
||||||
|
if fi.SHA != "" {
|
||||||
|
flog.Debugf("Added %s to %s in avatarMap", fi.SHA, msg.UserID)
|
||||||
|
b.avatarMap[msg.UserID] = fi.SHA
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user