From 6014ccedc3fdc7918b7e519c5575670f6c828a35 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 28 Apr 2023 17:34:26 +0200 Subject: [PATCH] Implement SendTextToTelegram function --- .env.template | 2 ++ .gitignore | 1 + go.mod | 8 ++++++++ go.sum | 4 ++++ main.go | 4 ++++ tg.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+) create mode 100644 .env.template create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 tg.go diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..b60b650 --- /dev/null +++ b/.env.template @@ -0,0 +1,2 @@ +TELEGRAM_BOT_TOKEN= +TELEGRAM_CHAT_ID= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..345b1d6 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module gitlab.com/ekzyis/stackernews_rss-to-tg + +go 1.20 + +require ( + github.com/joho/godotenv v1.5.1 // indirect + github.com/namsral/flag v1.7.4-pre // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4c16f8b --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs= +github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo= diff --git a/main.go b/main.go new file mode 100644 index 0000000..da29a2c --- /dev/null +++ b/main.go @@ -0,0 +1,4 @@ +package main + +func main() { +} diff --git a/tg.go b/tg.go new file mode 100644 index 0000000..5d0e173 --- /dev/null +++ b/tg.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/joho/godotenv" + "github.com/namsral/flag" +) + +var ( + ChatId int + BotToken string +) + +func init() { + err := godotenv.Load() + if err != nil { + log.Fatal("error loading .env file") + } + flag.IntVar(&ChatId, "TELEGRAM_CHAT_ID", 0, "Chat id of telegram channel") + flag.StringVar(&BotToken, "TELEGRAM_BOT_TOKEN", "", "Telegram bot token") + flag.Parse() + if ChatId == 0 { + log.Fatal("TELEGRAM_CHAT_ID not set") + } + if BotToken == "" { + log.Fatal("TELEGRAM_BOT_TOKEN not set") + } +} + +func SendTextToTelegram(text string) { + url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%d&text=%s", BotToken, ChatId, text) + resp, err := http.Get(url) + if err != nil { + err = fmt.Errorf("error during GET %s: %w", url, err) + log.Println(err) + return + } + defer resp.Body.Close() + log.Printf("GET %s: %d\n", url, resp.StatusCode) +}