2016-11-15 14:15:57 -08:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2017-11-04 06:50:01 -07:00
|
|
|
"regexp"
|
2017-01-06 14:32:17 -08:00
|
|
|
"strconv"
|
2017-11-12 13:04:35 -08:00
|
|
|
"strings"
|
2017-01-06 14:32:17 -08:00
|
|
|
|
2018-02-26 15:33:21 -08:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-11-15 14:15:57 -08:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-11-04 06:50:01 -07:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2016-11-15 14:15:57 -08:00
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Btelegram struct {
|
2017-12-19 14:15:03 -08:00
|
|
|
c *tgbotapi.BotAPI
|
2018-03-04 14:52:14 -08:00
|
|
|
*bridge.Config
|
2018-02-19 15:54:35 -08:00
|
|
|
avatarMap map[string]string // keep cache of userid and avatar sha
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
|
|
|
|
2018-03-04 14:52:14 -08:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
return &Btelegram{Config: cfg, avatarMap: make(map[string]string)}
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) Connect() error {
|
|
|
|
var err error
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connecting")
|
2018-03-04 14:52:14 -08:00
|
|
|
b.c, err = tgbotapi.NewBotAPI(b.GetString("Token"))
|
2016-11-15 14:15:57 -08:00
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("%#v", err)
|
2016-11-15 14:15:57 -08:00
|
|
|
return err
|
|
|
|
}
|
2018-01-29 03:07:26 -08:00
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates, err := b.c.GetUpdatesChan(u)
|
2016-11-15 14:15:57 -08:00
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("%#v", err)
|
2016-11-15 14:15:57 -08:00
|
|
|
return err
|
|
|
|
}
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Info("Connection succeeded")
|
2016-11-15 14:15:57 -08:00
|
|
|
go b.handleRecv(updates)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:12:02 -08:00
|
|
|
func (b *Btelegram) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-12 05:51:41 -07:00
|
|
|
func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
2016-11-15 14:15:57 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 13:59:37 -07:00
|
|
|
func (b *Btelegram) Send(msg config.Message) (string, error) {
|
2018-02-28 13:23:29 -08:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-25 14:54:20 -08:00
|
|
|
|
|
|
|
// get the chatid
|
2016-11-15 14:15:57 -08:00
|
|
|
chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
|
|
|
|
if err != nil {
|
2017-08-27 13:59:37 -07:00
|
|
|
return "", err
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
2017-01-06 14:32:17 -08:00
|
|
|
|
2018-02-19 15:54:35 -08:00
|
|
|
// map the file SHA to our user (caches the avatar)
|
|
|
|
if msg.Event == config.EVENT_AVATAR_DOWNLOAD {
|
2018-02-25 14:54:20 -08:00
|
|
|
return b.cacheAvatar(&msg)
|
2018-02-19 15:54:35 -08:00
|
|
|
}
|
|
|
|
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetString("MessageFormat") == "HTML" {
|
2017-02-24 09:49:52 -08:00
|
|
|
msg.Text = makeHTML(msg.Text)
|
|
|
|
}
|
2017-08-28 11:38:37 -07:00
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// Delete message
|
2017-09-11 14:12:33 -07:00
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
msgid, err := strconv.Atoi(msg.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = b.c.DeleteMessage(tgbotapi.DeleteMessageConfig{ChatID: chatid, MessageID: msgid})
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// Upload a file if it exists
|
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-02-28 14:25:00 -08:00
|
|
|
b.sendMessage(chatid, rmsg.Username, rmsg.Text)
|
2018-02-25 14:54:20 -08:00
|
|
|
}
|
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
b.handleUploadFile(&msg, chatid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 11:38:37 -07:00
|
|
|
// edit the message if we have a msg ID
|
|
|
|
if msg.ID != "" {
|
|
|
|
msgid, err := strconv.Atoi(msg.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetString("MessageFormat") == "HTML" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debug("Using mode HTML")
|
2017-12-10 06:16:17 -08:00
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetString("MessageFormat") == "Markdown" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debug("Using mode markdown")
|
2018-02-03 14:31:21 -08:00
|
|
|
m.ParseMode = tgbotapi.ModeMarkdown
|
|
|
|
}
|
2017-08-28 11:38:37 -07:00
|
|
|
_, err = b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// Post normal message
|
2018-02-28 14:25:00 -08:00
|
|
|
return b.sendMessage(chatid, msg.Username, msg.Text)
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|
|
|
for update := range updates {
|
2018-02-28 13:23:29 -08:00
|
|
|
b.Log.Debugf("== Receiving event: %#v", update.Message)
|
2018-02-25 14:54:20 -08:00
|
|
|
|
|
|
|
if update.Message == nil && update.ChannelPost == nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Error("Getting nil messages, this shouldn't happen.")
|
2018-02-07 05:28:48 -08:00
|
|
|
continue
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
2017-04-01 09:18:38 -07:00
|
|
|
var message *tgbotapi.Message
|
2017-11-04 06:50:01 -07:00
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
rmsg := config.Message{Account: b.Account, Extra: make(map[string][]interface{})}
|
2017-11-04 06:50:01 -07:00
|
|
|
|
2017-01-27 14:26:06 -08:00
|
|
|
// handle channels
|
|
|
|
if update.ChannelPost != nil {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.ChannelPost
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
|
|
|
// edited channel message
|
2018-03-04 14:52:14 -08:00
|
|
|
if update.EditedChannelPost != nil && !b.GetBool("EditDisable") {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.EditedChannelPost
|
2018-03-04 14:52:14 -08:00
|
|
|
rmsg.Text = rmsg.Text + message.Text + b.GetString("EditSuffix")
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
2017-01-27 14:26:06 -08:00
|
|
|
// handle groups
|
|
|
|
if update.Message != nil {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.Message
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
|
|
|
// edited group message
|
2018-03-04 14:52:14 -08:00
|
|
|
if update.EditedMessage != nil && !b.GetBool("EditDisable") {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.EditedMessage
|
2018-03-04 14:52:14 -08:00
|
|
|
rmsg.Text = rmsg.Text + message.Text + b.GetString("EditSuffix")
|
2017-04-01 09:18:38 -07:00
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
|
|
|
// 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
|
2017-04-01 09:18:38 -07:00
|
|
|
if message.From != nil {
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetBool("UseFirstName") {
|
2018-02-25 14:54:20 -08:00
|
|
|
rmsg.Username = message.From.FirstName
|
2017-05-15 14:23:10 -07:00
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
if rmsg.Username == "" {
|
|
|
|
rmsg.Username = message.From.UserName
|
|
|
|
if rmsg.Username == "" {
|
|
|
|
rmsg.Username = message.From.FirstName
|
2017-05-15 14:23:10 -07:00
|
|
|
}
|
2017-01-27 14:26:06 -08:00
|
|
|
}
|
2018-03-04 15:43:59 -08:00
|
|
|
rmsg.Text += message.Text
|
2018-02-19 15:54:35 -08:00
|
|
|
// only download avatars if we have a place to upload them (configured mediaserver)
|
|
|
|
if b.General.MediaServerUpload != "" {
|
2018-02-25 14:54:20 -08:00
|
|
|
b.handleDownloadAvatar(message.From.ID, rmsg.Channel)
|
2018-02-19 15:54:35 -08:00
|
|
|
}
|
2017-01-27 14:59:24 -08:00
|
|
|
}
|
2017-04-01 09:18:38 -07:00
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// if we really didn't find a username, set it to unknown
|
|
|
|
if rmsg.Username == "" {
|
|
|
|
rmsg.Username = "unknown"
|
2017-12-10 06:08:23 -08:00
|
|
|
}
|
2017-09-19 14:58:05 -07:00
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// handle any downloads
|
|
|
|
err := b.handleDownload(message, &rmsg)
|
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Errorf("download failed: %s", err)
|
2018-02-21 04:30:38 -08:00
|
|
|
}
|
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// handle forwarded messages
|
2017-12-10 05:52:29 -08:00
|
|
|
if message.ForwardFrom != nil {
|
|
|
|
usernameForward := ""
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetBool("UseFirstName") {
|
2017-12-10 05:52:29 -08:00
|
|
|
usernameForward = message.ForwardFrom.FirstName
|
|
|
|
}
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = message.ForwardFrom.UserName
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = message.ForwardFrom.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = "unknown"
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
|
2017-12-10 05:52:29 -08:00
|
|
|
}
|
|
|
|
|
2017-09-19 14:58:05 -07:00
|
|
|
// quote the previous message
|
|
|
|
if message.ReplyToMessage != nil {
|
|
|
|
usernameReply := ""
|
|
|
|
if message.ReplyToMessage.From != nil {
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetBool("UseFirstName") {
|
2017-09-19 14:58:05 -07:00
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName
|
|
|
|
}
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = message.ReplyToMessage.From.UserName
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = "unknown"
|
|
|
|
}
|
2018-04-17 14:26:41 -07:00
|
|
|
if !b.GetBool("QuoteDisable") {
|
2018-04-19 03:53:49 -07:00
|
|
|
rmsg.Text = rmsg.Text + " (re @" + usernameReply + ":" + message.ReplyToMessage.Text + ")"
|
2018-04-17 14:26:41 -07:00
|
|
|
}
|
2017-09-19 14:58:05 -07:00
|
|
|
}
|
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
if rmsg.Text != "" || len(rmsg.Extra) > 0 {
|
2018-04-19 03:53:49 -07:00
|
|
|
rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text)
|
2018-02-25 14:54:20 -08:00
|
|
|
rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.Itoa(message.From.ID), b.General)
|
|
|
|
|
2018-02-28 13:23:29 -08:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", rmsg.Username, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
2018-02-25 14:54:20 -08:00
|
|
|
b.Remote <- rmsg
|
2017-01-27 14:26:06 -08:00
|
|
|
}
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 09:25:17 -07:00
|
|
|
|
|
|
|
func (b *Btelegram) getFileDirectURL(id string) string {
|
|
|
|
res, err := b.c.GetFileDirectURL(id)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2017-11-04 06:50:01 -07:00
|
|
|
|
2018-02-19 15:54:35 -08:00
|
|
|
// 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
|
|
|
|
func (b *Btelegram) handleDownloadAvatar(userid int, channel string) {
|
2018-02-25 14:54:20 -08:00
|
|
|
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{})}
|
2018-02-19 15:54:35 -08:00
|
|
|
if _, ok := b.avatarMap[strconv.Itoa(userid)]; !ok {
|
|
|
|
photos, err := b.c.GetUserProfilePhotos(tgbotapi.UserProfilePhotosConfig{UserID: userid, Limit: 1})
|
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Errorf("Userprofile download failed for %#v %s", userid, err)
|
2018-02-19 15:54:35 -08:00
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
2018-02-19 15:54:35 -08:00
|
|
|
if len(photos.Photos) > 0 {
|
|
|
|
photo := photos.Photos[0][0]
|
|
|
|
url := b.getFileDirectURL(photo.FileID)
|
|
|
|
name := strconv.Itoa(userid) + ".png"
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("trying to download %#v fileid %#v with size %#v", name, photo.FileID, photo.FileSize)
|
2018-02-25 14:54:20 -08:00
|
|
|
|
2018-02-26 15:33:21 -08:00
|
|
|
err := helper.HandleDownloadSize(b.Log, &rmsg, name, int64(photo.FileSize), b.General)
|
2018-02-25 14:54:20 -08:00
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Error(err)
|
2018-02-25 14:54:20 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Errorf("download %s failed %#v", url, err)
|
2018-02-25 14:54:20 -08:00
|
|
|
return
|
2018-02-19 15:54:35 -08:00
|
|
|
}
|
2018-02-26 15:33:21 -08:00
|
|
|
helper.HandleDownloadData(b.Log, &rmsg, name, rmsg.Text, "", data, b.General)
|
2018-02-25 14:54:20 -08:00
|
|
|
b.Remote <- rmsg
|
2018-02-19 15:54:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:54:20 -08:00
|
|
|
// handleDownloadFile handles file download
|
|
|
|
func (b *Btelegram) handleDownload(message *tgbotapi.Message, rmsg *config.Message) error {
|
2017-11-04 06:50:01 -07:00
|
|
|
size := 0
|
2018-02-25 14:54:20 -08:00
|
|
|
var url, name, text string
|
|
|
|
|
|
|
|
if message.Sticker != nil {
|
|
|
|
v := message.Sticker
|
2017-11-04 06:50:01 -07:00
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
2017-11-12 13:04:35 -08:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-20 13:12:51 -08:00
|
|
|
if !strings.HasSuffix(name, ".webp") {
|
|
|
|
name = name + ".webp"
|
|
|
|
}
|
2017-11-04 06:50:01 -07:00
|
|
|
text = " " + url
|
2018-02-25 14:54:20 -08:00
|
|
|
}
|
|
|
|
if message.Video != nil {
|
|
|
|
v := message.Video
|
2017-11-04 06:50:01 -07:00
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
2017-11-12 13:04:35 -08:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-04 06:50:01 -07:00
|
|
|
text = " " + url
|
2018-02-25 14:54:20 -08:00
|
|
|
}
|
|
|
|
if message.Photo != nil {
|
|
|
|
photos := *message.Photo
|
2017-11-04 06:50:01 -07:00
|
|
|
size = photos[len(photos)-1].FileSize
|
|
|
|
url = b.getFileDirectURL(photos[len(photos)-1].FileID)
|
2017-11-12 13:04:35 -08:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-04 06:50:01 -07:00
|
|
|
text = " " + url
|
2018-02-25 14:54:20 -08:00
|
|
|
}
|
|
|
|
if message.Document != nil {
|
|
|
|
v := message.Document
|
2017-11-04 06:50:01 -07:00
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
|
|
|
name = v.FileName
|
|
|
|
text = " " + v.FileName + " : " + url
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
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
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetBool("UseInsecureURL") {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("Setting message text to :%s", text)
|
2018-02-25 14:54:20 -08:00
|
|
|
rmsg.Text = rmsg.Text + text
|
|
|
|
return nil
|
2017-11-04 06:50:01 -07:00
|
|
|
}
|
|
|
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
2018-02-26 15:33:21 -08:00
|
|
|
err := helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General)
|
2018-02-25 14:54:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-26 15:33:21 -08:00
|
|
|
helper.HandleDownloadData(b.Log, rmsg, name, message.Caption, "", data, b.General)
|
2018-02-25 14:54:20 -08:00
|
|
|
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)
|
2017-11-04 06:50:01 -07:00
|
|
|
} else {
|
2018-02-25 14:54:20 -08:00
|
|
|
c = tgbotapi.NewDocumentUpload(chatid, file)
|
|
|
|
}
|
|
|
|
_, err := b.c.Send(c)
|
|
|
|
if err != nil {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Errorf("file upload failed: %#v", err)
|
2018-02-25 14:54:20 -08:00
|
|
|
}
|
|
|
|
if fi.Comment != "" {
|
2018-02-28 14:25:00 -08:00
|
|
|
b.sendMessage(chatid, msg.Username, fi.Comment)
|
2017-11-04 06:50:01 -07:00
|
|
|
}
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
return "", nil
|
2017-11-04 06:50:01 -07:00
|
|
|
}
|
2017-11-12 15:20:31 -08:00
|
|
|
|
2018-02-28 14:25:00 -08:00
|
|
|
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
|
|
|
m := tgbotapi.NewMessage(chatid, "")
|
|
|
|
m.Text = username + text
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetString("MessageFormat") == "HTML" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debug("Using mode HTML")
|
2018-02-28 14:25:00 -08:00
|
|
|
m.Text = username + text
|
2017-11-12 15:20:31 -08:00
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2018-03-04 14:52:14 -08:00
|
|
|
if b.GetString("MessageFormat") == "Markdown" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debug("Using mode markdown")
|
2018-02-03 14:31:21 -08:00
|
|
|
m.ParseMode = tgbotapi.ModeMarkdown
|
|
|
|
}
|
2017-11-12 15:20:31 -08:00
|
|
|
res, err := b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strconv.Itoa(res.MessageID), nil
|
|
|
|
}
|
2018-02-25 14:54:20 -08:00
|
|
|
|
|
|
|
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 != "" {
|
2018-02-26 15:33:21 -08:00
|
|
|
b.Log.Debugf("Added %s to %s in avatarMap", fi.SHA, msg.UserID)
|
2018-02-25 14:54:20 -08:00
|
|
|
b.avatarMap[msg.UserID] = fi.SHA
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|