Skip to content

Commit

Permalink
Add user filtering capability and make more parameters configurable o…
Browse files Browse the repository at this point in the history
…utside.
  • Loading branch information
mguaylam committed Jul 14, 2024
1 parent ce0cd4d commit b049008
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,27 @@ The goal of this Telegram bot is to provide an interface for launching a Communa

## Usage

To use this bot, you need to define an system environment variable named _TOKEN_COMMUNAUTOSEARCH_BOT_ and set its value to your Telegram token.
### Using the Bot

To use this bot, you need to define the following system environment variables:

1. **Telegram Token**:
- Variable Name: `TOKEN_COMMUNAUTOSEARCH_BOT`
- Description: This is the token value for Telegram.
- Example: `TOKEN_COMMUNAUTOSEARCH_BOT=your_telegram_token_value`

2. **Authorized Users**:
- Variable Name: `AUTHORIZED_USERS_ID`
- Description: This is a list of user IDs that you want to authorize to chat with the bot. The IDs should be separated by a semicolon (`;`).
- Example: `AUTHORIZED_USERS_ID=id1;id2;id3`

3. **City ID**:
- Variable Name: `CITY_ID`
- Description: This is the ID of the city you want to query from. You can obtain the list of city IDs from this [link](https://restapifrontoffice.reservauto.net/ReservautoFrontOffice/index.html?urls.primaryName=Branch%20version%202%20(6.93.1)#/).
- Example: `CITY_ID=your_city_id`

Please replace `your_telegram_token_value`, `id1;id2;id3`, and `your_city_id` with your actual values.

After that, run `go run main.go` to launch the bot.
Then, search for the bot on Telegram to start a discussion with it.

Expand Down
37 changes: 33 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type UserContext struct {
dateEnd time.Time
}

const cityId = 59 // see available cities -> https://restapifrontoffice.reservauto.net/ReservautoFrontOffice/index.html?urls.primaryName=Branch%20version%202%20(6.93.1)#/
var cityId int

var userContexts = make(map[int64]UserContext)
var resultChannel = make(map[int64]chan int)
Expand All @@ -54,23 +54,35 @@ const layoutDate = "2006-01-02 15:04"

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

var authorizedUserIdSlice []string

var authorizedUserId string

var bot *tgbotapi.BotAPI

var mutex = sync.Mutex{}

func main() {

var err error

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
})

go http.ListenAndServe(":8444", nil)

// Find TOKEN in .env file if exist
// Find TOKEN env if exist
godotenv.Load()
var err error

bot, err = tgbotapi.NewBotAPI(os.Getenv("TOKEN_COMMUNAUTOSEARCH_BOT"))
authorizedUserId = os.Getenv("AUTHORIZED_USERS_ID")
cityId, err = strconv.Atoi(os.Getenv("CITY_ID"))
authorizedUserIdSlice = strings.Split(authorizedUserId, ";")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -117,6 +129,23 @@ func main() {
func generateResponse(userCtx *UserContext, message *tgbotapi.Message) string {

messageText := message.Text
userAuthorized := false

// check if user is authorized with to chat from AUTHORIZED_USERS_ID
for _, element := range authorizedUserIdSlice {
userIdInt64, err := strconv.ParseInt(element, 10, 64)
if err != nil {
log.Fatal(err)
}
if userCtx.chatId == userIdInt64 {
userAuthorized = true
}
}

if userAuthorized != true {
log.Printf("User not authorized : " + strconv.FormatInt(userCtx.chatId, 10))
return "Vous n'êtes pas autorisé à utiliser ce rebot."
}

if strings.ToLower(messageText) == "/aide" {
return "Écrire:\n/chercher pour initier une nouvelle recherche.\n/recommencer pour redémarrer une recherche avec les mêmes paramètres que la recherche précédente."
Expand Down

0 comments on commit b049008

Please sign in to comment.