Skip to content

Commit

Permalink
Change messages to user
Browse files Browse the repository at this point in the history
  • Loading branch information
craftlion committed Sep 27, 2023
1 parent 1dbbb0c commit bcb968f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.20

require github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible

require github.com/joho/godotenv v1.5.1 // indirect
require github.com/joho/godotenv v1.5.1

require (
github.com/craftlion/communautofinder v1.1.0
github.com/craftlion/communautofinder v1.2.0
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/craftlion/communautofinder v1.1.0 h1:XLLZlDr6nzHmStzIk3rCetRobiym/Z84wVChngYTeX8=
github.com/craftlion/communautofinder v1.1.0/go.mod h1:aUPA9bhCTDcsHax45j1149Hql6d1hNMLwI9TAAYEfso=
github.com/craftlion/communautofinder v1.2.0 h1:eblDFzB36erPjZLRQC4lR5va2AP9Bh8OYB8MqfjRSyA=
github.com/craftlion/communautofinder v1.2.0/go.mod h1:aUPA9bhCTDcsHax45j1149Hql6d1hNMLwI9TAAYEfso=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand Down
55 changes: 32 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"
"time"

"github.com/joho/godotenv"

"github.com/craftlion/communautofinder"
Expand Down Expand Up @@ -51,19 +52,20 @@ var cancelSearchingMethod = make(map[int64]context.CancelFunc)

const layoutDate = "2006-01-02 15:04"

const dateExample = "2023-11-21 20:12"

var bot *tgbotapi.BotAPI

var mutex = sync.Mutex{}

func main() {

// Find .env file
err1 := godotenv.Load(".env")
if err1!= nil{
log.Fatalf("Error loading .env file: %s", err1)
// Find .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file: %s", err)
}

var err error
bot, err = tgbotapi.NewBotAPI(os.Getenv("TOKEN_COMMUNAUTOSEARCH_BOT"))
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -121,17 +123,17 @@ func generateResponse(userCtx *UserContext, message *tgbotapi.Message) string {
}

userCtx.state = AskingType
return "Hello! Type:\n- station to search for a communauto station\n- flex to search for a communauto flex vehicle?"
return "Hello! Type:\n- station to search for a communauto station\n- flex to search for a communauto flex vehicle ?"
} else if userCtx.state == AskingType {
if strings.ToLower(messageText) == "station" {
userCtx.searchType = Station
userCtx.state = AskingMargin
return "What is your search radius in Km?"
return "What is your search radius in Km ?"

} else if strings.ToLower(messageText) == "flex" {
userCtx.searchType = Flex
userCtx.state = AskingMargin
return "What is your search radius in Km?"
return "What is your search radius in Km ?"
}

} else if userCtx.state == AskingMargin {
Expand Down Expand Up @@ -161,7 +163,7 @@ func generateResponse(userCtx *UserContext, message *tgbotapi.Message) string {
} else if userCtx.searchType == Station {
userCtx.state = AskingDateStart

return fmt.Sprintf("What is the start date and time for the rental in the format %s?", layoutDate)
return fmt.Sprintf("What is the start date and time for the rental in the format %s ?", dateExample)
}
}
} else if userCtx.state == AskingDateStart {
Expand All @@ -171,7 +173,7 @@ func generateResponse(userCtx *UserContext, message *tgbotapi.Message) string {
if err == nil {
userCtx.dateStart = t
userCtx.state = AskingDateEnd
return fmt.Sprintf("What is the end date and time for the rental in the format %s?", layoutDate)
return fmt.Sprintf("What is the end date and time for the rental in the format %s ?", dateExample)
}

} else if userCtx.state == AskingDateEnd {
Expand All @@ -198,7 +200,7 @@ func generateResponse(userCtx *UserContext, message *tgbotapi.Message) string {

}

return "I didn't quite understand."
return "I didn't quite understand 😕"
}

func generateMessageResearch(userCtx UserContext) string {
Expand All @@ -211,10 +213,12 @@ func generateMessageResearch(userCtx UserContext) string {
typeSearch = "station"
}

message := fmt.Sprintf("Searching for a %s vehicle within %fkm of GPS(%f,%f)", typeSearch, userCtx.kmMargin, userCtx.latitude, userCtx.longitude)
roundedKmMargin := int(userCtx.kmMargin)

message := fmt.Sprintf("🔍 Searching for a %s vehicle within %dkm of the position you entered... you will receive a message when one is found", typeSearch, roundedKmMargin)

if userCtx.searchType == Station {
message += fmt.Sprintf(" from %s to %s", userCtx.dateStart, userCtx.dateEnd)
message += fmt.Sprintf(" from %s to %s", userCtx.dateStart.Format(layoutDate), userCtx.dateEnd.Format(layoutDate))
}

return message
Expand All @@ -229,25 +233,30 @@ func launchSearch(userCtx UserContext) {
cancelSearchingMethod[userCtx.chatId] = cancel

if userCtx.searchType == Flex {
go communautofinder.SearchFlexCarForGoRoutine(cityId, currentCoordinate, userCtx.kmMargin, resultChannel[userCtx.chatId], ctx)
go communautofinder.SearchFlexCarForGoRoutine(cityId, currentCoordinate, userCtx.kmMargin, resultChannel[userCtx.chatId], ctx, cancel)
} else if userCtx.searchType == Station {
go communautofinder.SearchStationCarForGoRoutine(cityId, currentCoordinate, userCtx.kmMargin, userCtx.dateStart, userCtx.dateEnd, resultChannel[userCtx.chatId], ctx)
go communautofinder.SearchStationCarForGoRoutine(cityId, currentCoordinate, userCtx.kmMargin, userCtx.dateStart, userCtx.dateEnd, resultChannel[userCtx.chatId], ctx, cancel)
}

nbCarFound := <-resultChannel[userCtx.chatId]

var msg tgbotapi.MessageConfig

if nbCarFound != -1 {
msg := tgbotapi.NewMessage(userCtx.chatId, fmt.Sprintf("%d vehicle(s) available according to your search criteria", nbCarFound))
bot.Send(msg)
msg = tgbotapi.NewMessage(userCtx.chatId, fmt.Sprintf("💡 Found ! %d vehicle(s) available according to your search criteria", nbCarFound))
} else {
msg = tgbotapi.NewMessage(userCtx.chatId, "😞 An error occurred in your search criteria. Please launch a new search")
}

mutex.Lock()
bot.Send(msg)

newUserCtx := userContexts[userCtx.chatId]
newUserCtx.state = EndSearch
userContexts[newUserCtx.chatId] = newUserCtx
mutex.Lock()

mutex.Unlock()
}
newUserCtx := userContexts[userCtx.chatId]
newUserCtx.state = EndSearch
userContexts[newUserCtx.chatId] = newUserCtx

mutex.Unlock()

delete(cancelSearchingMethod, userCtx.chatId)
}

0 comments on commit bcb968f

Please sign in to comment.