commit 9b48f2ee501406857e36dffa3509e1e57c57845b
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2022-09-14T14:54:51Z |
| subject | Early telegram integrations |
commit 9b48f2ee501406857e36dffa3509e1e57c57845b
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2022-09-14T14:54:51Z
Early telegram integrations
---
.gitignore | 1 +
datasources/mtr/bus/QueryResult.go | 6 +++++-
datasources/mtr/bus/query.go | 35 ++++++++++++++++++++++-------------
go.mod | 2 ++
go.sum | 2 ++
handlers/index.go | 11 -----------
main.go | 35 +++++++++++++++++++++++++++++++----
utils/cache.go | 2 ++
8 files changed, 65 insertions(+), 29 deletions(-)
diff --git a/.gitignore b/.gitignore
index 3c9b48b..dcc712c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+./golifehk
*.csv
*.json
*.swp
diff --git a/datasources/mtr/bus/QueryResult.go b/datasources/mtr/bus/QueryResult.go
index 90b995c..fd45f6f 100644
--- a/datasources/mtr/bus/QueryResult.go
+++ b/datasources/mtr/bus/QueryResult.go
@@ -37,7 +37,11 @@ func ( result QueryResult ) Message() string {
sb.WriteString( "\n" )
for _, bus := range buses.Buses {
sb.WriteString( " * " )
- sb.WriteString( bus.ETAText )
+ if bus.ETAText == "" {
+ sb.WriteString( bus.ETDText )
+ } else {
+ sb.WriteString( bus.ETAText )
+ }
sb.WriteString( "\n" )
}
diff --git a/datasources/mtr/bus/query.go b/datasources/mtr/bus/query.go
index 0d79081..a3a7a8b 100644
--- a/datasources/mtr/bus/query.go
+++ b/datasources/mtr/bus/query.go
@@ -10,6 +10,7 @@ import (
type queryObj struct {
Route string
BusStops *[]BusStop
+ Search string
}
type qTerm struct {
@@ -25,6 +26,11 @@ func Query( lang string, message string ) *QueryResult {
}
if qo.Route != "" {
+
+ if len( *qo.BusStops ) == 0 {
+ return &QueryResult{ Error: errors.New( "Result not found" ) }
+ }
+
schedules, err := getSchedule( lang, qo.Route )
if err != nil {
return &QueryResult{ Error: err }
@@ -54,9 +60,9 @@ func Query( lang string, message string ) *QueryResult {
func test( entry BusStop, val string ) bool {
switch true {
- case strings.Contains( (*entry.Name)["zh"], val ):
+ case strings.Contains( strings.ToUpper( (*entry.Name)["zh"] ), val ):
fallthrough
- case strings.Contains( (*entry.Name)["en"], val ):
+ case strings.Contains( strings.ToUpper( (*entry.Name)["en"] ), val ):
return true
}
return false
@@ -109,21 +115,24 @@ func parse( line string ) ( *queryObj, error ) {
// If route found, filter out all other route
// then search within that route
- if route != "" && 0 < len( searches ) {
- matches_in := []BusStop{}
- for _, entry := range matches {
- if entry.RouteId != route {
- continue
- }
+ if route != "" {
+ if 0 < len( searches ) {
+ matches_in := []BusStop{}
+ for _, entry := range matches {
+ if entry.RouteId != route {
+ continue
+ }
- for _, val := range searches {
- if test( entry, val ) {
- matches_in = append( matches_in, entry )
- break
+ for _, val := range searches {
+ if test( entry, val ) {
+ matches_in = append( matches_in, entry )
+ break
+ }
}
}
+ matches = matches_in
+ } else {
}
- matches = matches_in
}
return &queryObj{ Route: route, BusStops: &matches }, err
diff --git a/go.mod b/go.mod
index 15f974b..e523e83 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module github.com/tgckpg/golifehk
go 1.19
+
+require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..db8e45c
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+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=
diff --git a/handlers/index.go b/handlers/index.go
deleted file mode 100644
index 634ed0f..0000000
--- a/handlers/index.go
+++ /dev/null
@@ -1,11 +0,0 @@
-package handlers
-
-import (
- "fmt"
- "html"
- "net/http"
-)
-
-func Index(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
-}
diff --git a/main.go b/main.go
index 7552782..eaa4b66 100644
--- a/main.go
+++ b/main.go
@@ -2,11 +2,38 @@ package main
import (
"log"
- "net/http"
- "github.com/tgckpg/golifehk/handlers"
+ "os"
+ mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
- http.HandleFunc( "/", handlers.Index )
- log.Fatal(http.ListenAndServe(":8000", nil))
+ bot, err := tgbotapi.NewBotAPI( os.Getenv( "TELEGRAM_API_TOKEN" ) )
+ 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 := bot.GetUpdatesChan(u)
+
+ for update := range updates {
+ if update.Message == nil {
+ continue
+ }
+
+ log.Printf( "[%s] %s", update.Message.From.UserName, update.Message.Text )
+
+ result := mtrbus.Query( "zh", update.Message.Text )
+
+ msg := tgbotapi.NewMessage( update.Message.Chat.ID, result.Message() )
+ msg.ReplyToMessageID = update.Message.MessageID
+
+ bot.Send( msg )
+ }
}
diff --git a/utils/cache.go b/utils/cache.go
index 00540fc..1fea008 100644
--- a/utils/cache.go
+++ b/utils/cache.go
@@ -3,6 +3,7 @@ package utils
import (
"bytes"
"io"
+ "log"
"os"
"path/filepath"
"time"
@@ -20,6 +21,7 @@ func CacheStream( path string, readStream func() ( io.ReadCloser, error ), expir
f, err := os.Open( path )
if err == nil {
defer f.Close()
+ log.Printf( "Using cache: %s", path )
writeBuff := bytes.NewBuffer( []byte{} )
_, err = io.Copy( writeBuff, f )
if err == nil {