aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2021-02-21 21:14:09 +0000
committerterminaldweller <thabogre@gmail.com>2021-02-21 21:14:09 +0000
commitc9876c40703b6723f94eea5638a0779c0550b2d7 (patch)
tree47e8946c2b7b47a35a96ba9b0d4341e3d5c83ede
parentInitial commit (diff)
downloadhived-c9876c40703b6723f94eea5638a0779c0550b2d7.tar.gz
hived-c9876c40703b6723f94eea5638a0779c0550b2d7.zip
the price end point works, the telegram bot is only echo though
-rw-r--r--Dockerfile11
-rw-r--r--docker-compose.yaml18
-rw-r--r--hived.go150
-rwxr-xr-xrun.sh2
4 files changed, 181 insertions, 0 deletions
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..2848f6c
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,11 @@
+FROM alpine:3.13 as builder
+
+RUN apk update && apk upgrade
+RUN apk add go git
+RUN go get -u github.com/go-telegram-bot-api/telegram-bot-api
+COPY *.go /hived/
+RUN cd /hived && go build
+
+FROM alpine:3.13
+COPY --from=builder /hived/hived /hived/
+ENTRYPOINT ["/hived/hived"]
diff --git a/docker-compose.yaml b/docker-compose.yaml
new file mode 100644
index 0000000..8ac47b4
--- /dev/null
+++ b/docker-compose.yaml
@@ -0,0 +1,18 @@
+version: "3.8"
+services:
+ hived:
+ image: hived
+ build:
+ context: ./
+ secrets:
+ - tg_bot_token
+ networks:
+ - hivednet
+ restart: unless-stopped
+ ports:
+ - "8008:8008"
+networks:
+ hivednet:
+secrets:
+ tg_bot_token:
+ file: ./tgtoken.json
diff --git a/hived.go b/hived.go
new file mode 100644
index 0000000..14fa696
--- /dev/null
+++ b/hived.go
@@ -0,0 +1,150 @@
+package main
+
+import (
+ "encoding/json"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "net/url"
+
+ "github.com/go-telegram-bot-api/telegram-bot-api"
+)
+
+var flagPort = flag.String("port", "8008", "determined the port the sercice runs on")
+var flagTgTokenFile = flag.String("tgtokenfile", "/run/secrets/tg_bot_token", "determines the location of the telegram bot token file")
+
+const cryptocomparePriceURL = "https://min-api.cryptocompare.com/data/price?"
+
+func getTgToken() string {
+ tgTokenJsonBytes, err := ioutil.ReadFile(*flagTgTokenFile)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ type TgToken struct {
+ Token string `json:"token"`
+ }
+
+ var tgToken TgToken
+
+ err = json.Unmarshal(tgTokenJsonBytes, &tgToken)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ return tgToken.Token
+}
+
+func runTgBot() {
+ botToken := getTgToken()
+ bot, err := tgbotapi.NewBotAPI(botToken)
+ if err != nil {
+ log.Panic(err)
+ }
+
+ bot.Debug = true
+
+ log.Printf("Authorized on account %s", bot.Self.UserName)
+
+ u := tgbotapi.NewUpdate(0)
+ u.Timeout = 60
+
+ updates, err := bot.GetUpdatesChan(u)
+ if err != nil {
+ log.Panic(err)
+ }
+
+ for update := range updates {
+ if update.Message == nil {
+ continue
+ }
+
+ log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
+
+ msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
+ msg.ReplyToMessageID = update.Message.MessageID
+
+ bot.Send(msg)
+ }
+}
+
+func sendGetToCryptoCompare(ccurl, name, unit string) (float64, string, error) {
+ params := "fsym=" + url.QueryEscape(name) + "&" +
+ "tsyms=" + url.QueryEscape(unit)
+ path := ccurl + params
+ fmt.Println(path)
+ resp, err := http.Get(path)
+ if err != nil {
+ log.Fatal(err)
+ return 0., "", err
+ }
+ defer resp.Body.Close()
+
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ log.Fatal(err)
+ return 0., "", err
+ }
+
+ jsonBody := make(map[string]float64)
+ err = json.Unmarshal(body, &jsonBody)
+ if err != nil {
+ log.Fatal(err)
+ return 0., "", err
+ }
+
+ fmt.Println(string(body))
+
+ return jsonBody[unit], unit, nil
+}
+
+//TODO
+func healthHandler(w http.ResponseWriter, r *http.Request) {
+}
+
+func cryptoHandler(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/crypto" {
+ http.Error(w, "404 not found.", http.StatusNotFound)
+ return
+ }
+
+ if r.Method != "GET" {
+ http.Error(w, "Method is not supported.", http.StatusNotFound)
+ }
+
+ var name string
+ var unit string
+ params := r.URL.Query()
+ for key, value := range params {
+ switch key {
+ case "name":
+ name = value[0]
+ case "unit":
+ unit = value[0]
+ default:
+ }
+ }
+
+ price, unit, err := sendGetToCryptoCompare(cryptocomparePriceURL, name, unit)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ json.NewEncoder(w).Encode(map[string]interface{}{"name": price, "unit": unit})
+}
+
+func startServer() {
+ http.HandleFunc("/crypto", cryptoHandler)
+ http.HandleFunc("/health", healthHandler)
+
+ if err := http.ListenAndServe(":"+*flagPort, nil); err != nil {
+ log.Fatal(err)
+ }
+}
+
+func main() {
+ go runTgBot()
+ startServer()
+}
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..4031990
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,2 @@
+#!/usr/bin/env sh
+docker build -t hived ./ && docker-compose up