Using markdown v2
This commit is contained in:
		@@ -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 }
 | 
			
		||||
 
 | 
			
		||||
@@ -13,30 +13,32 @@ type QueryResult struct {
 | 
			
		||||
    Query *QueryObject
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
 | 
			
		||||
    if b.PrevStop() != nil {
 | 
			
		||||
        sb.WriteString( (*b.PrevStop().Name)[ *lang ] )
 | 
			
		||||
        sb.WriteString( " > " )
 | 
			
		||||
var MARKDOWN_ESC []string = []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
 | 
			
		||||
func _escape( t string ) string {
 | 
			
		||||
    for _, c := range MARKDOWN_ESC {
 | 
			
		||||
        t = strings.Replace( t, c, "\\" + c, -1 )
 | 
			
		||||
    }
 | 
			
		||||
    return t
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    sb.WriteString( "**" )
 | 
			
		||||
    sb.WriteString( (*b.Name)[ *lang ] )
 | 
			
		||||
    sb.WriteString( "**" )
 | 
			
		||||
func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
 | 
			
		||||
    if b.PrevStop() != nil {
 | 
			
		||||
        sb.WriteString( _escape( (*b.PrevStop().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,45 +53,49 @@ 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 {
 | 
			
		||||
        if 0 < len(*result.Schedules) {
 | 
			
		||||
            for busStop, buses := range *result.Schedules {
 | 
			
		||||
                writeShortRoute( &result.Lang, &sb, &busStop )
 | 
			
		||||
                for _, bus := range buses.Buses {
 | 
			
		||||
                sb.WriteString( "  * " )
 | 
			
		||||
                    sb.WriteString( "  \\* " )
 | 
			
		||||
                    if bus.ETAText == "" {
 | 
			
		||||
                    sb.WriteString( bus.ETDText )
 | 
			
		||||
                        sb.WriteString( _escape( bus.ETDText ) )
 | 
			
		||||
                    } else {
 | 
			
		||||
                    sb.WriteString( bus.ETAText )
 | 
			
		||||
                        sb.WriteString( _escape( bus.ETAText ) )
 | 
			
		||||
                    }
 | 
			
		||||
                    sb.WriteString( "\n" )
 | 
			
		||||
                }
 | 
			
		||||
                sb.WriteString( "\n" )
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            sb.WriteString( _escape( "Schedules are empty...perhaps Out of Service Time?" ) )
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return sb.String()
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								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 )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user