drone-telegram/plugin.go

473 lines
9.6 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"html"
"io/ioutil"
"log"
2019-12-07 06:29:47 +03:00
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
2019-10-11 15:51:40 +03:00
"github.com/drone/drone-template-lib/template"
2019-10-11 15:54:47 +03:00
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
const (
formatMarkdown = "markdown"
)
type (
2019-02-17 11:03:10 +03:00
// GitHub information.
GitHub struct {
Workflow string
Workspace string
Action string
EventName string
EventPath string
}
// Repo information.
Repo struct {
2019-02-17 10:39:47 +03:00
FullName string
Namespace string
Name string
}
// Commit information.
Commit struct {
Sha string
2019-02-17 10:30:24 +03:00
Ref string
Branch string
Link string
Author string
Avatar string
Email string
Message string
}
// Build information.
Build struct {
Tag string
Event string
Number int
Status string
Link string
Started float64
Finished float64
PR string
DeployTo string
}
// Config for the plugin.
Config struct {
Token string
Debug bool
MatchEmail bool
WebPreview bool
To []string
2020-06-20 08:51:48 +03:00
Message string
MessageFile string
TemplateVars string
Photo []string
Document []string
Sticker []string
Audio []string
Voice []string
Location []string
Video []string
Venue []string
Format string
GitHub bool
2019-12-07 06:29:47 +03:00
Socks5 string
}
// Plugin values.
Plugin struct {
2019-02-17 11:03:10 +03:00
GitHub GitHub
Repo Repo
Commit Commit
Build Build
Config Config
Tpl map[string]string
}
// Location format
Location struct {
Title string
Address string
Latitude float64
Longitude float64
}
)
var icons = map[string]string{
"failure": "❌",
"cancelled": "❕",
"success": "✅",
}
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
}
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
}
func globList(keys []string) []string {
var newKeys []string
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)
continue
}
newKeys = append(newKeys, matches...)
}
return newKeys
}
func convertLocation(value string) (Location, bool) {
var latitude, longitude float64
var title, address string
var err error
2019-09-25 16:37:18 +03:00
values := trimElement(strings.Split(value, " "))
if len(values) < 2 {
return Location{}, true
}
if len(values) > 2 {
title = values[2]
}
if len(values) > 3 {
title = values[2]
address = values[3]
}
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{
Title: title,
Address: address,
Latitude: latitude,
Longitude: longitude,
}, false
}
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
}
func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
var emails []int64
var ids []int64
attachEmail := true
for _, value := range trimElement(to) {
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)
attachEmail = false
continue
}
ids = append(ids, id)
}
2019-02-16 19:23:46 +03:00
if matchEmail && !attachEmail {
return emails
}
2019-02-16 19:23:46 +03:00
ids = append(ids, emails...)
return ids
}
// Exec executes the plugin.
func (p Plugin) Exec() (err error) {
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
return errors.New("missing telegram token or user list")
}
var message []string
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}
default:
p.Config.Format = formatMarkdown
message = p.Message()
}
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)
}
}
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)
}
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)}}
bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, proxyClient)
} else {
bot, err = tgbotapi.NewBotAPI(p.Config.Token)
}
if err != nil {
return err
}
bot.Debug = p.Config.Debug
ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail)
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))
locations := trimElement(p.Config.Location)
venues := trimElement(p.Config.Venue)
message = trimElement(message)
if p.Config.Format == formatMarkdown {
message = escapeMarkdown(message)
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)
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)
p.Repo.Name = escapeMarkdownOne(p.Repo.Name)
}
// send message.
for _, user := range ids {
for _, value := range message {
txt, err := template.RenderTrim(value, p)
if err != nil {
return err
}
txt = html.UnescapeString(txt)
msg := tgbotapi.NewMessage(user, txt)
msg.ParseMode = p.Config.Format
msg.DisableWebPagePreview = !p.Config.WebPreview
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range photos {
msg := tgbotapi.NewPhotoUpload(user, value)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range documents {
msg := tgbotapi.NewDocumentUpload(user, value)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range stickers {
msg := tgbotapi.NewStickerUpload(user, value)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range audios {
msg := tgbotapi.NewAudioUpload(user, value)
msg.Title = "Audio Message."
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range voices {
msg := tgbotapi.NewVoiceUpload(user, value)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range videos {
msg := tgbotapi.NewVideoUpload(user, value)
msg.Caption = "Video Message"
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range locations {
location, empty := convertLocation(value)
2019-02-16 19:23:46 +03:00
if empty {
continue
}
msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude)
if err := p.Send(bot, msg); err != nil {
return err
}
}
for _, value := range venues {
location, empty := convertLocation(value)
2019-02-16 19:23:46 +03:00
if empty {
continue
}
msg := tgbotapi.NewVenue(user, location.Title, location.Address, location.Latitude, location.Longitude)
if err := p.Send(bot, msg); err != nil {
return err
}
}
}
return nil
}
// Send bot message.
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
message, err := bot.Send(msg)
2019-02-16 19:23:46 +03:00
if p.Config.Debug {
log.Println("=====================")
log.Printf("Response Message: %#v\n", message)
log.Println("=====================")
}
if err == nil {
return nil
}
return errors.New(strings.Replace(err.Error(), p.Config.Token, "<token>", -1))
}
// Message is plugin default message.
func (p Plugin) Message() []string {
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,
)}
}
// ✅ 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",
icon,
p.Build.Number,
p.Repo.FullName,
p.Build.Status,
p.Commit.Author,
p.Commit.Branch,
p.Commit.Message,
p.Build.Link,
)}
}