all repos — cameraman @ e9af61f938b31b2bb9589e5bb079370941294021

minor tweaks, ditch resty
BiRabittoh andronacomarco@gmail.com
Fri, 07 Jun 2024 01:43:01 +0200
commit

e9af61f938b31b2bb9589e5bb079370941294021

parent

585f171daa720446f46b066ded55d5da8d56c93c

5 files changed, 103 insertions(+), 74 deletions(-)

jump to
M go.modgo.mod

@@ -3,7 +3,6 @@

go 1.22.3 require ( - github.com/go-resty/resty/v2 v2.13.1 github.com/joho/godotenv v1.5.1 gorm.io/driver/sqlite v1.5.5 gorm.io/gorm v1.25.10

@@ -13,5 +12,4 @@ require (

github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect - golang.org/x/net v0.25.0 // indirect )
M go.sumgo.sum

@@ -1,5 +1,3 @@

-github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g= -github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=

@@ -8,53 +6,6 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=

github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= -golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= -golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E= gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE= gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
M main.gomain.go

@@ -34,7 +34,7 @@ dataDir = "data"

dbFile = "occurrences.db" defaultNotificationWindow = 3 defaultSleepDuration = 1 - defaultPort = 3000 + defaultPort = "3000" ) var (

@@ -80,6 +80,9 @@ }

log.Println("Sleep duration:", SleepDuration) port = os.Getenv("PORT") + if port == "" { + port = defaultPort + } } func main() {
M notify.gonotify.go

@@ -1,51 +1,133 @@

package main import ( + "bytes" + "encoding/json" "errors" "fmt" + "io" "log" + "net/http" "os" "time" +) - "github.com/go-resty/resty/v2" -) +const msgFormat = "*Giorno %02d/%02d*\n\n_%s_\n%s" +const msgFormatYear = "*Giorno %02d/%02d/%04d*\nšŸŽ‚ %d anni\n\n_%s_\n%s" +const baseUrl = "https://api.telegram.org/bot" + +type Response struct { + Ok bool `json:"ok"` + Result Result `json:"result"` +} + +type Result struct { + MessageID int `json:"message_id"` +} var ( NotificationWindow int SleepDuration time.Duration - - notificationClient *resty.Client telegramToken string chatID string threadID string ) +func sendPostRequest(url string, payload map[string]interface{}) (*http.Response, error) { + jsonData, err := json.Marshal(payload) + if err != nil { + return nil, fmt.Errorf("failed to marshal payload: %v", err) + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, fmt.Errorf("failed to create new request: %v", err) + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to send request: %v", err) + } + + return resp, nil +} + func notifyTelegram(occurrence Occurrence) error { log.Println("Sending notification for occurrence", occurrence.ID) - message := fmt.Sprintf("*Giorno %02d/%02d*.\n\n_%s_\n%s", - occurrence.Day, occurrence.Month, occurrence.Name, occurrence.Description) + var message string + if occurrence.Year != nil { + years := time.Now().Year() - int(*occurrence.Year) + message = fmt.Sprintf(msgFormatYear, occurrence.Day, occurrence.Month, *occurrence.Year, years, occurrence.Name, occurrence.Description) + } else { + message = fmt.Sprintf(msgFormat, occurrence.Day, occurrence.Month, occurrence.Name, occurrence.Description) + } - url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramToken) + url := fmt.Sprintf("%s%s/sendMessage", baseUrl, telegramToken) // Create the payload payload := map[string]interface{}{ - "chat_id": chatID, - "text": message, - "parse_mode": "markdown", - "message_thread_id": threadID, + "chat_id": chatID, + "text": message, + "parse_mode": "markdown", + "message_thread_id": threadID, + "disable_notification": true, } // Send the POST request - resp, err := notificationClient.R(). - SetHeader("Content-Type", "application/json"). - SetBody(payload). - Post(url) + resp, err := sendPostRequest(url, payload) + if err != nil { + log.Printf("Failed to send notification: %v", err) + return err + } + defer resp.Body.Close() + + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + log.Printf("Failed to read response body: %v", err) + return err + } + + log.Printf("Notification sent: %s, Response: %s", message, string(bodyBytes)) + + // Decode the JSON response + var r Response + if err := json.Unmarshal(bodyBytes, &r); err != nil { + log.Printf("Failed to decode response: %v", err) + return err + } + + if !r.Ok { + log.Printf("Telegram API returned an error: %v", r) + return fmt.Errorf("telegram API error: %v", r) + } + + msgId := r.Result.MessageID + + // Prepare the request to pin the message + url = fmt.Sprintf("%s%s/pinChatMessage", baseUrl, telegramToken) + payload = map[string]interface{}{ + "chat_id": chatID, + "message_id": msgId, + "disable_notification": false, + } + + // Send the POST request to pin the message + resp, err = sendPostRequest(url, payload) + if err != nil { + log.Printf("Failed to pin message: %v", err) + return err + } + defer resp.Body.Close() + bodyBytes, err = io.ReadAll(resp.Body) if err != nil { - log.Printf("Failed to send notification: %v", err) + log.Printf("Failed to read response body: %v", err) return err } - log.Printf("Notification sent: %s, Response: %s", message, resp) + + log.Printf("Message pinned: %s, Response: %s", message, string(bodyBytes)) return nil }

@@ -58,8 +140,6 @@ }

} func initNotifications() error { - notificationClient = resty.New() - telegramToken = os.Getenv("TELEGRAM_BOT_TOKEN") chatID = os.Getenv("TELEGRAM_CHAT_ID") threadID = os.Getenv("TELEGRAM_THREAD_ID")
M templates/index.htmltemplates/index.html

@@ -223,7 +223,6 @@

function padMax(input, max = 31, min=1) { const n = Number(input); if (isNaN(n)) { - console.log(max) return max;} return padNumber(Math.max(min, Math.min(n | 0, max))) }

@@ -262,8 +261,6 @@ const myDescription = description || '';

const myDay = day || '01'; const myMonth = month || '01'; const myYear = year || ''; - console.log(notified); - console.log(typeof notified); return ` <td data-field="name" data-value="${myName}"><input class="big-input" type="text" value="${myName}" id="name-${id}" onkeydown="handleInputKeyDown(event, ${id})" autocomplete="off"></td> <td data-field="description" data-value="${myDescription}"><input class="big-input" type="text" value="${myDescription}" id="description-${id}" onkeydown="handleInputKeyDown(event, ${id})" autocomplete="off"></td>