From ef219a94bbeec96bf21099883a3c29ae183bb000 Mon Sep 17 00:00:00 2001 From: "Andrey Ivanov (NetMoose)" Date: Fri, 25 Feb 2022 20:50:18 +0500 Subject: [PATCH] Add telegram send --- go.mod | 1 + go.sum | 2 ++ main.go | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e9dc5c0..af803b9 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/boltdb/bolt v1.3.1 // indirect + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect github.com/jessevdk/go-flags v1.5.0 // indirect golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index e438d95..80853f8 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc= diff --git a/main.go b/main.go index b63ff9c..122b345 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,13 @@ package main import ( "encoding/json" "encoding/xml" + "html" "html/template" "log" "os" "github.com/boltdb/bolt" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/jessevdk/go-flags" "gopkg.in/yaml.v2" ) @@ -15,8 +17,10 @@ import ( type Config struct { Dbpath string `yaml:"dbpath"` Telegram struct { - Send bool `yaml:"send"` - Token string `yaml:"token"` + Send bool `yaml:"send"` + SendDebug bool `yaml:"senddebug"` + ChatId int64 `yaml:"chatid"` + Token string `yaml:"token"` } `yaml:"telegram"` VK struct { Send bool `yaml:"send"` @@ -164,6 +168,21 @@ func InitDb(rss Rss2, dbpath string) { func (config Config) RunSend() { if config.Telegram.Send { log.Println("Send to telegram") + bot, err := tgbotapi.NewBotAPI(config.Telegram.Token) + if err != nil { + log.Panic(err) + } + bot.Debug = config.Telegram.SendDebug + for _, v := range senditems.ItemList { + s := "" + string(v.Title) + "\n" + html.UnescapeString(string(v.Description)) + + "\n" + v.Link + msg := tgbotapi.NewMessage(config.Telegram.ChatId, s) + msg.ParseMode = "Html" + _, err := bot.Send(msg) + if err != nil { + log.Panic(err) + } + } } if config.VK.Send { log.Println("Send to VK")