penguin/golifehk

@golifehk bot in Telegram

commit 292665c49b2c9c8749c431eb8c5bb353e08efc48

author斟酌 鵬兄 <tgckpg@gmail.com>
date2022-09-15T13:38:04Z
subjectUsing markdown v2
commit 292665c49b2c9c8749c431eb8c5bb353e08efc48
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2022-09-15T13:38:04Z

    Using markdown v2
---
 datasources/mtr/bus/BusStop.go     |  6 ++++
 datasources/mtr/bus/QueryResult.go | 62 +++++++++++++++++++++-----------------
 datasources/mtr/bus/query.go       |  2 +-
 main.go                            |  7 ++---
 4 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/datasources/mtr/bus/BusStop.go b/datasources/mtr/bus/BusStop.go
index abec4a0..ad7f0a6 100644
--- a/datasources/mtr/bus/BusStop.go
+++ b/datasources/mtr/bus/BusStop.go
@@ -25,3 +25,9 @@ func ( busStop BusStop ) NextStop() *BusStop {
     }
     return nil
 }
+
+type ByRouteId []BusStop
+
+func (a ByRouteId) Len() int           { return len(a) }
+func (a ByRouteId) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
+func (a ByRouteId) Less(i, j int) bool { return a[i].RouteId < a[j].RouteId }
diff --git a/datasources/mtr/bus/QueryResult.go b/datasources/mtr/bus/QueryResult.go
index a6286d5..ba59b4c 100644
--- a/datasources/mtr/bus/QueryResult.go
+++ b/datasources/mtr/bus/QueryResult.go
@@ -13,30 +13,32 @@ type QueryResult struct {
     Query *QueryObject
 }
 
+var MARKDOWN_ESC []string = []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
+func _escape( t string ) string {
+    for _, c := range MARKDOWN_ESC {
+        t = strings.Replace( t, c, "\\" + c, -1 )
+    }
+    return t
+}
+
 func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
     if b.PrevStop() != nil {
-        sb.WriteString( (*b.PrevStop().Name)[ *lang ] )
-        sb.WriteString( " > " )
+        sb.WriteString( _escape( (*b.PrevStop().Name)[ *lang ] ) )
+        sb.WriteString( " \\> " )
     }
 
-    sb.WriteString( "**" )
-    sb.WriteString( (*b.Name)[ *lang ] )
-    sb.WriteString( "**" )
+    sb.WriteString( "*" )
+    sb.WriteString( _escape( (*b.Name)[ *lang ] ) )
+    sb.WriteString( "*" )
 
     if b.NextStop() != nil {
-        sb.WriteString( " > " )
-        sb.WriteString( (*b.NextStop().Name)[ *lang ] )
+        sb.WriteString( " \\> " )
+        sb.WriteString( _escape( (*b.NextStop().Name)[ *lang ] ) )
     }
 
     sb.WriteString( "\n" )
 }
 
-type ByRouteId []BusStop
-
-func (a ByRouteId) Len() int           { return len(a) }
-func (a ByRouteId) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
-func (a ByRouteId) Less(i, j int) bool { return a[i].RouteId < a[j].RouteId }
-
 func ( result QueryResult ) Message() string {
 
     if result.Error != nil {
@@ -51,44 +53,48 @@ func ( result QueryResult ) Message() string {
         if query.Route == "" {
             sort.Sort( ByRouteId( *query.BusStops ) )
             for _, busStop := range *query.BusStops {
-                sb.WriteString( busStop.RouteId )
+                sb.WriteString( _escape( busStop.RouteId ) )
                 sb.WriteString( " " )
                 writeShortRoute( &result.Lang, &sb, &busStop )
             }
         } else if query.BusStops == nil && query.RouteStarts != nil {
             for d, b := range *query.RouteStarts {
                 sb.WriteString( query.Route )
-                sb.WriteString( "-" )
-                sb.WriteString( d )
+                sb.WriteString( "\\-" )
+                sb.WriteString( _escape( d ) )
                 sb.WriteString( "\n " )
 
                 for {
-                    sb.WriteString( (*b.Name)[ result.Lang ] )
+                    sb.WriteString( _escape( (*b.Name)[ result.Lang ] ) )
                     b = b.NextStop()
                     if b == nil {
                         break
                     }
 
-                    sb.WriteString( " > " )
+                    sb.WriteString( " \\> " )
                 }
                 sb.WriteString( "\n" )
             }
         } else {
-            sb.WriteString( "Unreachable condition occured!?" )
+            sb.WriteString( _escape( "Unreachable condition occured!?" ) )
         }
     } else {
-        for busStop, buses := range *result.Schedules {
-            writeShortRoute( &result.Lang, &sb, &busStop )
-            for _, bus := range buses.Buses {
-                sb.WriteString( "  * " )
-                if bus.ETAText == "" {
-                    sb.WriteString( bus.ETDText )
-                } else {
-                    sb.WriteString( bus.ETAText )
+        if 0 < len(*result.Schedules) {
+            for busStop, buses := range *result.Schedules {
+                writeShortRoute( &result.Lang, &sb, &busStop )
+                for _, bus := range buses.Buses {
+                    sb.WriteString( "  \\* " )
+                    if bus.ETAText == "" {
+                        sb.WriteString( _escape( bus.ETDText ) )
+                    } else {
+                        sb.WriteString( _escape( bus.ETAText ) )
+                    }
+                    sb.WriteString( "\n" )
                 }
                 sb.WriteString( "\n" )
             }
-            sb.WriteString( "\n" )
+        } else {
+            sb.WriteString( _escape( "Schedules are empty...perhaps Out of Service Time?" ) )
         }
     }
 
diff --git a/datasources/mtr/bus/query.go b/datasources/mtr/bus/query.go
index ccb4941..4741e65 100644
--- a/datasources/mtr/bus/query.go
+++ b/datasources/mtr/bus/query.go
@@ -65,7 +65,7 @@ func Query( lang string, message string ) *QueryResult {
         }
 
         if len( schedules.BusStops ) == 0 {
-            qr.Error = errors.New( "Schedules are empty...perhaps Out of Service Time?" )
+            qr.Schedules = &map[BusStop] *BusStopBuses{}
             return &qr
         }
 
diff --git a/main.go b/main.go
index c5b8616..eb32d7a 100644
--- a/main.go
+++ b/main.go
@@ -3,7 +3,6 @@ package main
 import (
     "log"
     "os"
-    "strings"
     mtrbus "github.com/tgckpg/golifehk/datasources/mtr/bus"
     tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
 )
@@ -30,11 +29,11 @@ func main() {
 
         log.Printf( "[%s] %s", update.Message.From.UserName, update.Message.Text )
 
-        if strings.Contains( update.Message.Text, "@golifehkbot" ){
-            result := mtrbus.Query( "zh", strings.Replace( update.Message.Text, "@golifehkbot", "", -1 ) )
+        result := mtrbus.Query( "zh", update.Message.Text )
 
+        if result.Error == nil || !update.Message.Chat.IsGroup() {
             msg := tgbotapi.NewMessage( update.Message.Chat.ID, result.Message() )
-            msg.ParseMode = "Markdown"
+            msg.ParseMode = "MarkdownV2"
             msg.ReplyToMessageID = update.Message.MessageID
 
             bot.Send( msg )