2016-10-04 16:45:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-22 18:44:50 +03:00
|
|
|
"bufio"
|
2019-10-29 05:57:00 +03:00
|
|
|
"encoding/json"
|
2016-10-04 16:45:49 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-10-30 14:50:31 +03:00
|
|
|
"html"
|
2019-10-22 18:44:50 +03:00
|
|
|
"io/ioutil"
|
2016-10-04 16:45:49 +03:00
|
|
|
"log"
|
2019-12-07 06:29:47 +03:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-10-22 18:44:50 +03:00
|
|
|
"os"
|
2018-04-06 05:28:55 +03:00
|
|
|
"path/filepath"
|
2016-10-04 16:54:11 +03:00
|
|
|
"strconv"
|
2016-10-04 16:45:49 +03:00
|
|
|
"strings"
|
|
|
|
|
2023-05-26 11:38:36 +03:00
|
|
|
tgbotapi "git.belvedersky.ru/common/tgbotapi"
|
2021-07-03 07:39:54 +03:00
|
|
|
"github.com/appleboy/drone-template-lib/template"
|
2016-10-04 16:45:49 +03:00
|
|
|
)
|
|
|
|
|
2019-10-30 14:50:31 +03:00
|
|
|
const (
|
2021-07-12 10:17:02 +03:00
|
|
|
formatMarkdown = "Markdown"
|
|
|
|
formatHTML = "HTML"
|
2019-10-30 14:50:31 +03:00
|
|
|
)
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
type (
|
2019-02-17 11:03:10 +03:00
|
|
|
// GitHub information.
|
|
|
|
GitHub struct {
|
|
|
|
Workflow string
|
|
|
|
Workspace string
|
|
|
|
Action string
|
|
|
|
EventName string
|
|
|
|
EventPath string
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// Repo information.
|
|
|
|
Repo struct {
|
2019-02-17 10:39:47 +03:00
|
|
|
FullName string
|
|
|
|
Namespace string
|
|
|
|
Name string
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2017-09-11 07:34:47 +03:00
|
|
|
// Commit information.
|
|
|
|
Commit struct {
|
|
|
|
Sha string
|
2019-02-17 10:30:24 +03:00
|
|
|
Ref string
|
2017-09-11 07:34:47 +03:00
|
|
|
Branch string
|
|
|
|
Link string
|
|
|
|
Author string
|
2019-10-11 17:25:20 +03:00
|
|
|
Avatar string
|
2017-09-11 07:34:47 +03:00
|
|
|
Email string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// Build information.
|
|
|
|
Build struct {
|
2016-11-24 04:37:19 +03:00
|
|
|
Tag string
|
2016-10-31 04:57:34 +03:00
|
|
|
Event string
|
|
|
|
Number int
|
|
|
|
Status string
|
|
|
|
Link string
|
2020-07-20 09:50:22 +03:00
|
|
|
Started int64
|
|
|
|
Finished int64
|
2016-12-02 16:13:58 +03:00
|
|
|
PR string
|
2019-10-11 17:25:20 +03:00
|
|
|
DeployTo string
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config for the plugin.
|
|
|
|
Config struct {
|
2020-09-17 01:52:42 +03:00
|
|
|
Token string
|
|
|
|
Debug bool
|
|
|
|
MatchEmail bool
|
|
|
|
To []string
|
|
|
|
Message string
|
|
|
|
MessageFile string
|
|
|
|
TemplateVarsFile string
|
|
|
|
TemplateVars string
|
|
|
|
Photo []string
|
|
|
|
Document []string
|
|
|
|
Sticker []string
|
|
|
|
Audio []string
|
|
|
|
Voice []string
|
|
|
|
Location []string
|
|
|
|
Video []string
|
|
|
|
Venue []string
|
|
|
|
Format string
|
|
|
|
GitHub bool
|
|
|
|
Socks5 string
|
2020-10-03 04:20:17 +03:00
|
|
|
|
|
|
|
DisableWebPagePreview bool
|
2021-04-03 01:38:16 +03:00
|
|
|
DisableNotification bool
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plugin values.
|
|
|
|
Plugin struct {
|
2019-02-17 11:03:10 +03:00
|
|
|
GitHub GitHub
|
2016-10-04 16:45:49 +03:00
|
|
|
Repo Repo
|
2017-09-11 07:34:47 +03:00
|
|
|
Commit Commit
|
2016-10-04 16:45:49 +03:00
|
|
|
Build Build
|
|
|
|
Config Config
|
2019-10-29 05:57:00 +03:00
|
|
|
Tpl map[string]string
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
2016-10-14 09:27:34 +03:00
|
|
|
|
|
|
|
// Location format
|
|
|
|
Location struct {
|
2016-10-14 11:28:40 +03:00
|
|
|
Title string
|
|
|
|
Address string
|
2016-10-14 09:27:34 +03:00
|
|
|
Latitude float64
|
|
|
|
Longitude float64
|
|
|
|
}
|
2016-10-04 16:45:49 +03:00
|
|
|
)
|
|
|
|
|
2019-12-11 02:24:10 +03:00
|
|
|
var icons = map[string]string{
|
2019-12-11 05:34:52 +03:00
|
|
|
"failure": "❌",
|
2019-12-11 02:24:10 +03:00
|
|
|
"cancelled": "❕",
|
|
|
|
"success": "✅",
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
func trimElement(keys []string) []string {
|
|
|
|
var newKeys []string
|
|
|
|
|
|
|
|
for _, value := range keys {
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
if len(value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newKeys = append(newKeys, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:03:14 +03:00
|
|
|
func escapeMarkdown(keys []string) []string {
|
|
|
|
var newKeys []string
|
|
|
|
|
|
|
|
for _, value := range keys {
|
|
|
|
value = escapeMarkdownOne(value)
|
|
|
|
if len(value) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newKeys = append(newKeys, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
func escapeMarkdownOne(str string) string {
|
|
|
|
str = strings.Replace(str, `\_`, `_`, -1)
|
|
|
|
str = strings.Replace(str, `_`, `\_`, -1)
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2018-04-06 05:28:55 +03:00
|
|
|
func globList(keys []string) []string {
|
2016-10-07 09:34:31 +03:00
|
|
|
var newKeys []string
|
|
|
|
|
2018-04-06 05:28:55 +03:00
|
|
|
for _, pattern := range keys {
|
|
|
|
pattern = strings.Trim(pattern, " ")
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Glob error for %q: %s\n", pattern, err)
|
2016-10-07 09:34:31 +03:00
|
|
|
continue
|
|
|
|
}
|
2018-04-06 05:28:55 +03:00
|
|
|
newKeys = append(newKeys, matches...)
|
2016-10-07 09:34:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return newKeys
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:27:34 +03:00
|
|
|
func convertLocation(value string) (Location, bool) {
|
|
|
|
var latitude, longitude float64
|
2016-10-14 11:28:40 +03:00
|
|
|
var title, address string
|
2016-10-14 09:27:34 +03:00
|
|
|
var err error
|
2019-09-25 16:37:18 +03:00
|
|
|
values := trimElement(strings.Split(value, " "))
|
2016-10-14 09:27:34 +03:00
|
|
|
|
|
|
|
if len(values) < 2 {
|
|
|
|
return Location{}, true
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:28:40 +03:00
|
|
|
if len(values) > 2 {
|
|
|
|
title = values[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) > 3 {
|
|
|
|
title = values[2]
|
|
|
|
address = values[3]
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:27:34 +03:00
|
|
|
latitude, err = strconv.ParseFloat(values[0], 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
return Location{}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
longitude, err = strconv.ParseFloat(values[1], 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
return Location{}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return Location{
|
2016-10-14 11:28:40 +03:00
|
|
|
Title: title,
|
|
|
|
Address: address,
|
2016-10-14 09:27:34 +03:00
|
|
|
Latitude: latitude,
|
|
|
|
Longitude: longitude,
|
|
|
|
}, false
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:44:50 +03:00
|
|
|
func loadTextFromFile(filename string) ([]string, error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
r := bufio.NewReader(f)
|
|
|
|
content, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []string{string(content)}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
|
|
|
var emails []int64
|
|
|
|
var ids []int64
|
2016-11-09 07:23:19 +03:00
|
|
|
attachEmail := true
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2016-11-17 08:13:43 +03:00
|
|
|
for _, value := range trimElement(to) {
|
2016-11-09 07:00:36 +03:00
|
|
|
idArray := trimElement(strings.Split(value, ":"))
|
|
|
|
|
|
|
|
// check id
|
|
|
|
id, err := strconv.ParseInt(idArray[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check match author email
|
|
|
|
if len(idArray) > 1 {
|
|
|
|
if email := idArray[1]; email != authorEmail {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
emails = append(emails, id)
|
2016-11-09 07:23:19 +03:00
|
|
|
attachEmail = false
|
2016-11-09 07:00:36 +03:00
|
|
|
continue
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
2016-11-09 07:00:36 +03:00
|
|
|
|
|
|
|
ids = append(ids, id)
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:23:46 +03:00
|
|
|
if matchEmail && !attachEmail {
|
2016-11-09 07:00:36 +03:00
|
|
|
return emails
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:23:46 +03:00
|
|
|
ids = append(ids, emails...)
|
2016-11-04 04:38:21 +03:00
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
return ids
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2021-07-12 10:17:02 +03:00
|
|
|
func templateMessage(t string, plugin Plugin) (string, error) {
|
|
|
|
return template.RenderTrim(t, plugin)
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// Exec executes the plugin.
|
2019-10-22 18:44:50 +03:00
|
|
|
func (p Plugin) Exec() (err error) {
|
2016-10-04 16:45:49 +03:00
|
|
|
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
|
|
|
|
return errors.New("missing telegram token or user list")
|
|
|
|
}
|
|
|
|
|
|
|
|
var message []string
|
2019-10-22 18:44:50 +03:00
|
|
|
switch {
|
|
|
|
case len(p.Config.MessageFile) > 0:
|
|
|
|
message, err = loadTextFromFile(p.Config.MessageFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error loading message file '%s': %v", p.Config.MessageFile, err)
|
|
|
|
}
|
|
|
|
case len(p.Config.Message) > 0:
|
2020-06-20 08:51:48 +03:00
|
|
|
message = []string{p.Config.Message}
|
2019-10-22 18:44:50 +03:00
|
|
|
default:
|
2019-12-11 02:24:10 +03:00
|
|
|
p.Config.Format = formatMarkdown
|
2019-02-17 10:44:29 +03:00
|
|
|
message = p.Message()
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 05:57:00 +03:00
|
|
|
if p.Config.TemplateVars != "" {
|
|
|
|
p.Tpl = make(map[string]string)
|
|
|
|
if err = json.Unmarshal([]byte(p.Config.TemplateVars), &p.Tpl); err != nil {
|
|
|
|
return fmt.Errorf("unable to unmarshall template vars from JSON string '%s': %v", p.Config.TemplateVars, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 01:52:42 +03:00
|
|
|
if p.Config.TemplateVarsFile != "" {
|
|
|
|
content, err := ioutil.ReadFile(p.Config.TemplateVarsFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read file with template vars '%s': %v", p.Config.TemplateVarsFile, err)
|
|
|
|
}
|
2021-01-14 16:15:06 +03:00
|
|
|
vars := make(map[string]string)
|
2020-09-17 01:52:42 +03:00
|
|
|
if err = json.Unmarshal(content, &vars); err != nil {
|
|
|
|
return fmt.Errorf("unable to unmarshall template vars from JSON file '%s': %v", p.Config.TemplateVarsFile, err)
|
|
|
|
}
|
|
|
|
// Merging templates variables from file to the variables form plugin settings (variables from file takes precedence)
|
|
|
|
if p.Tpl == nil {
|
|
|
|
p.Tpl = vars
|
|
|
|
} else {
|
|
|
|
for k, v := range vars {
|
|
|
|
p.Tpl[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 06:29:47 +03:00
|
|
|
var proxyURL *url.URL
|
|
|
|
if proxyURL, err = url.Parse(p.Config.Socks5); err != nil {
|
|
|
|
return fmt.Errorf("unable to unmarshall socks5 proxy url from string '%s': %v", p.Config.Socks5, err)
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:44:50 +03:00
|
|
|
var bot *tgbotapi.BotAPI
|
2019-12-07 06:29:47 +03:00
|
|
|
if len(p.Config.Socks5) > 0 {
|
|
|
|
proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
|
2023-05-26 11:38:36 +03:00
|
|
|
bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, "", proxyClient) // ANCHOR ENDPOINT
|
2019-12-07 06:29:47 +03:00
|
|
|
} else {
|
|
|
|
bot, err = tgbotapi.NewBotAPI(p.Config.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 17:23:13 +03:00
|
|
|
bot.Debug = p.Config.Debug
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2017-09-11 07:34:47 +03:00
|
|
|
ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail)
|
2018-04-06 05:28:55 +03:00
|
|
|
photos := globList(trimElement(p.Config.Photo))
|
|
|
|
documents := globList(trimElement(p.Config.Document))
|
|
|
|
stickers := globList(trimElement(p.Config.Sticker))
|
|
|
|
audios := globList(trimElement(p.Config.Audio))
|
|
|
|
voices := globList(trimElement(p.Config.Voice))
|
|
|
|
videos := globList(trimElement(p.Config.Video))
|
2016-10-14 09:27:34 +03:00
|
|
|
locations := trimElement(p.Config.Location)
|
2016-10-14 11:28:40 +03:00
|
|
|
venues := trimElement(p.Config.Venue)
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2017-07-04 14:03:14 +03:00
|
|
|
message = trimElement(message)
|
|
|
|
|
2019-10-30 14:50:31 +03:00
|
|
|
if p.Config.Format == formatMarkdown {
|
2017-07-04 14:03:14 +03:00
|
|
|
message = escapeMarkdown(message)
|
|
|
|
|
2017-09-11 07:34:47 +03:00
|
|
|
p.Commit.Message = escapeMarkdownOne(p.Commit.Message)
|
|
|
|
p.Commit.Branch = escapeMarkdownOne(p.Commit.Branch)
|
|
|
|
p.Commit.Link = escapeMarkdownOne(p.Commit.Link)
|
|
|
|
p.Commit.Author = escapeMarkdownOne(p.Commit.Author)
|
|
|
|
p.Commit.Email = escapeMarkdownOne(p.Commit.Email)
|
|
|
|
|
|
|
|
p.Build.Tag = escapeMarkdownOne(p.Build.Tag)
|
2017-07-04 14:03:14 +03:00
|
|
|
p.Build.Link = escapeMarkdownOne(p.Build.Link)
|
|
|
|
p.Build.PR = escapeMarkdownOne(p.Build.PR)
|
|
|
|
|
2019-02-17 10:39:47 +03:00
|
|
|
p.Repo.Namespace = escapeMarkdownOne(p.Repo.Namespace)
|
2017-07-04 14:03:14 +03:00
|
|
|
p.Repo.Name = escapeMarkdownOne(p.Repo.Name)
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// send message.
|
2016-11-09 07:00:36 +03:00
|
|
|
for _, user := range ids {
|
2017-07-04 14:03:14 +03:00
|
|
|
for _, value := range message {
|
2021-07-12 10:17:02 +03:00
|
|
|
txt, err := templateMessage(value, p)
|
2016-10-31 04:57:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:50:31 +03:00
|
|
|
txt = html.UnescapeString(txt)
|
|
|
|
|
2016-10-31 04:57:34 +03:00
|
|
|
msg := tgbotapi.NewMessage(user, txt)
|
2016-10-06 19:41:28 +03:00
|
|
|
msg.ParseMode = p.Config.Format
|
2020-10-03 04:22:38 +03:00
|
|
|
msg.DisableWebPagePreview = p.Config.DisableWebPagePreview
|
2021-04-03 01:38:16 +03:00
|
|
|
msg.DisableNotification = p.Config.DisableNotification
|
2023-05-26 11:38:52 +03:00
|
|
|
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
2016-10-07 09:34:31 +03:00
|
|
|
|
|
|
|
for _, value := range photos {
|
2023-05-26 11:38:52 +03:00
|
|
|
msg := tgbotapi.NewPhoto(user, tgbotapi.FilePath(value))
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-07 16:50:42 +03:00
|
|
|
}
|
2016-10-07 09:34:31 +03:00
|
|
|
|
2016-10-07 16:50:42 +03:00
|
|
|
for _, value := range documents {
|
2023-05-26 11:38:52 +03:00
|
|
|
msg := tgbotapi.NewDocument(user, tgbotapi.FilePath(value))
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-07 09:34:31 +03:00
|
|
|
}
|
2016-10-09 04:56:31 +03:00
|
|
|
|
|
|
|
for _, value := range stickers {
|
2023-05-26 11:38:52 +03:00
|
|
|
msg := tgbotapi.NewSticker(user, tgbotapi.FilePath(value))
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-09 04:56:31 +03:00
|
|
|
}
|
2016-10-14 08:36:08 +03:00
|
|
|
|
|
|
|
for _, value := range audios {
|
2023-05-26 11:38:52 +03:00
|
|
|
msg := tgbotapi.NewAudio(user, tgbotapi.FilePath(value))
|
2016-10-14 08:36:08 +03:00
|
|
|
msg.Title = "Audio Message."
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-14 08:36:08 +03:00
|
|
|
}
|
2016-10-14 08:57:31 +03:00
|
|
|
|
|
|
|
for _, value := range voices {
|
2023-05-26 11:38:52 +03:00
|
|
|
msg := tgbotapi.NewVoice(user, tgbotapi.FilePath(value))
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-14 08:57:31 +03:00
|
|
|
}
|
2016-10-14 09:27:34 +03:00
|
|
|
|
2016-10-14 10:36:50 +03:00
|
|
|
for _, value := range videos {
|
2023-05-26 11:38:52 +03:00
|
|
|
msg := tgbotapi.NewVideo(user, tgbotapi.FilePath(value))
|
2016-10-14 10:36:50 +03:00
|
|
|
msg.Caption = "Video Message"
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-14 10:36:50 +03:00
|
|
|
}
|
|
|
|
|
2016-10-14 09:27:34 +03:00
|
|
|
for _, value := range locations {
|
|
|
|
location, empty := convertLocation(value)
|
|
|
|
|
2019-02-16 19:23:46 +03:00
|
|
|
if empty {
|
2016-10-14 09:27:34 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude)
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-14 09:27:34 +03:00
|
|
|
}
|
2016-10-14 11:28:40 +03:00
|
|
|
|
|
|
|
for _, value := range venues {
|
|
|
|
location, empty := convertLocation(value)
|
|
|
|
|
2019-02-16 19:23:46 +03:00
|
|
|
if empty {
|
2016-10-14 11:28:40 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := tgbotapi.NewVenue(user, location.Title, location.Address, location.Latitude, location.Longitude)
|
2017-12-18 10:16:10 +03:00
|
|
|
if err := p.Send(bot, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-14 11:28:40 +03:00
|
|
|
}
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-07 16:50:42 +03:00
|
|
|
// Send bot message.
|
2017-12-18 10:16:10 +03:00
|
|
|
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
|
|
|
|
message, err := bot.Send(msg)
|
2016-10-07 16:50:42 +03:00
|
|
|
|
2019-02-16 19:23:46 +03:00
|
|
|
if p.Config.Debug {
|
2017-12-18 10:16:10 +03:00
|
|
|
log.Println("=====================")
|
|
|
|
log.Printf("Response Message: %#v\n", message)
|
|
|
|
log.Println("=====================")
|
2016-10-07 16:50:42 +03:00
|
|
|
}
|
2017-12-18 10:16:10 +03:00
|
|
|
|
2019-02-17 12:39:15 +03:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(strings.Replace(err.Error(), p.Config.Token, "<token>", -1))
|
2016-10-07 16:50:42 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// Message is plugin default message.
|
2019-02-17 10:44:29 +03:00
|
|
|
func (p Plugin) Message() []string {
|
2019-12-11 02:24:10 +03:00
|
|
|
icon := icons[strings.ToLower(p.Build.Status)]
|
|
|
|
|
2019-02-17 11:03:10 +03:00
|
|
|
if p.Config.GitHub {
|
|
|
|
return []string{fmt.Sprintf("%s/%s triggered by %s (%s)",
|
|
|
|
p.Repo.FullName,
|
|
|
|
p.GitHub.Workflow,
|
|
|
|
p.Repo.Namespace,
|
|
|
|
p.GitHub.EventName,
|
|
|
|
)}
|
|
|
|
}
|
|
|
|
|
2019-12-11 05:34:52 +03:00
|
|
|
// ✅ Build #106 of drone-telegram succeeded.
|
|
|
|
//
|
|
|
|
// 📝 Commit by appleboy on master:
|
|
|
|
// chore: update default template
|
|
|
|
//
|
|
|
|
// 🌐 https://cloud.drone.io/appleboy/drone-telegram/106
|
|
|
|
return []string{fmt.Sprintf("%s Build #%d of `%s` %s.\n\n📝 Commit by %s on `%s`:\n``` %s ```\n\n🌐 %s",
|
2019-12-11 02:24:10 +03:00
|
|
|
icon,
|
2019-12-11 05:34:52 +03:00
|
|
|
p.Build.Number,
|
2019-12-11 02:24:10 +03:00
|
|
|
p.Repo.FullName,
|
2019-12-11 05:34:52 +03:00
|
|
|
p.Build.Status,
|
2019-02-17 10:44:29 +03:00
|
|
|
p.Commit.Author,
|
2019-12-11 05:34:52 +03:00
|
|
|
p.Commit.Branch,
|
|
|
|
p.Commit.Message,
|
|
|
|
p.Build.Link,
|
2016-10-04 16:45:49 +03:00
|
|
|
)}
|
|
|
|
}
|