Early telegram integrations

This commit is contained in:
斟酌 鵬兄 2022-09-14 22:54:51 +08:00
parent 534bc70f0f
commit 9b48f2ee50
8 changed files with 65 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
./golifehk
*.csv
*.json
*.swp

View File

@ -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" )
}

View File

@ -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

2
go.mod
View File

@ -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

2
go.sum Normal file
View File

@ -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=

View File

@ -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))
}

35
main.go
View File

@ -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 )
}
}

View File

@ -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 {