Skip to content

Commit

Permalink
Merge pull request #7 from codescalers/fix_timezone
Browse files Browse the repository at this point in the history
Fix timezone
  • Loading branch information
Eslam-Nawara authored Nov 12, 2023
2 parents 970b81d + 40bb178 commit 1620e13
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ jobs:
- name: staticcheck
uses: dominikh/[email protected]
with:
version: "2022.1.3"
install-go : false
version: "2023.1.6"
min-go-version: "1.21.x"

- name: gofmt
uses: Jerome1337/[email protected]
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ WORKDIR /app

COPY --from=builder /src/statusbot .

CMD ./statusbot -t ${STATUSBOT_TOKEN}

CMD ./statusbot -b ${STATUSBOT_TOKEN} -t ${NOTFICATION_TIME} -z ${TIMEZONE}
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,59 @@

## Overview

The Status Bot is a tool designed to send a reminder at 5 O'clock
The Status Bot is a tool designed to send a reminder at specific time

## Getting Started

### Prerequisites

Ensure that you have installed:

- Go programming language (version 1.21 or higher if not using docker)
- Git
- Go programming language (version 1.21 or higher if not using docker)
- Git

### How to start

1. Clone this repository to your local machine:
1. Clone this repository to your local machine:

```bash
git clone https://github.com/codescalers/statusbot
cd statusbot
```
```bash
git clone https://github.com/codescalers/statusbot
cd statusbot
```

2. Setup your telegram bot and your env
2. Setup your telegram bot and your env

- Create a new [telegram bot](README.md#create-a-bot) if you don't have.
- Create a new [telegram bot](README.md#create-a-bot) if you don't have.

3. Run the bot:
3. Run the bot:

- Using go
- Using go

```bash
go run main.go -t <your bot token>
```
```bash
go run main.go -b <your bot token> -t <notfication time (default to 17:00)> -z <timezone (default Africa/Cairo)>
```

- Using Docker
- Using Docker

```bash
docker build -t statusbot .
docker run -e <your bot token> -it statusbot
```
```bash
docker build -t statusbot .
docker run -e STATUSBOT_TOKEN=<your bot token> -e NOTFICATION_TIME=<notfication time> -e TIMEZONE=<timezone> -it statusbot
```

## Create a bot

- Open telegram app
- Create a new bot
- Open telegram app
- Create a new bot

```ordered
1. Find telegram bot named "@botfarther"
2. Type /newbot
```
```ordered
1. Find telegram bot named "@botfarther"
2. Type /newbot
```

- Get the bot token
- Get the bot token

```ordered
1. In the same bot named "@botfarther"
2. Type /token
3. Choose your bot
```
```ordered
1. In the same bot named "@botfarther"
2. Type /token
3. Choose your bot
```
24 changes: 19 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"os"

"github.com/codescalers/statusbot/internal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"os"
)

var rootCmd = &cobra.Command{
Expand All @@ -17,13 +18,25 @@ var rootCmd = &cobra.Command{

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

token, err := cmd.Flags().GetString("token")
token, err := cmd.Flags().GetString("bot-token")
if err != nil || token == "" {
log.Error().Err(err).Msg("error in token")
return
}

bot, err := internal.NewBot(token)
time, err := cmd.Flags().GetString("time")
if err != nil || time == "" {
log.Error().Err(err).Msg("error in time")
return
}

timezone, err := cmd.Flags().GetString("timezone")
if err != nil || timezone == "" {
log.Error().Err(err).Msg("error in timezone")
return
}

bot, err := internal.NewBot(token, time, timezone)
if err != nil {
log.Error().Err(err).Msg("failed to create bot")
return
Expand All @@ -40,7 +53,8 @@ func Execute() {
}

func init() {

cobra.OnInitialize()
rootCmd.Flags().StringP("token", "t", "", "Enter a valid telegram bot token")
rootCmd.Flags().StringP("bot-token", "b", "", "Enter a valid telegram bot token")
rootCmd.Flags().StringP("time", "t", "17:00", "Enter a valid time")
rootCmd.Flags().StringP("timezone", "z", "Africa/Cairo", "Enter a valid timezone")
}
42 changes: 25 additions & 17 deletions internal/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package internal

import (
"fmt"
"time"
_ "time/tzdata"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log"
Expand All @@ -13,10 +15,11 @@ type Bot struct {
botAPI tgbotapi.BotAPI
addChan chan int64
removeChan chan int64
time time.Time
}

// NewBot creates new bot with a valid bot api and communication channels
func NewBot(token string) (Bot, error) {
func NewBot(token string, inputTime string, timezone string) (Bot, error) {
bot := Bot{}

botAPI, err := tgbotapi.NewBotAPI(token)
Expand All @@ -26,9 +29,23 @@ func NewBot(token string) (Bot, error) {

log.Printf("Authorized on account %s", botAPI.Self.UserName)

loc, err := time.LoadLocation(timezone)
if err != nil {
return bot, err
}

inputTime = fmt.Sprintf("%s %s:00", time.Now().Format(time.DateOnly), inputTime)
parsedTime, err := time.ParseInLocation(time.DateTime, inputTime, loc)
if err != nil {
return bot, err
}

log.Printf("Notfications is set to %s", parsedTime)

bot.botAPI = *botAPI
bot.addChan = make(chan int64)
bot.removeChan = make(chan int64)
bot.time = parsedTime

return bot, nil
}
Expand Down Expand Up @@ -70,7 +87,7 @@ func (bot Bot) Start() {
func (bot Bot) runBot() {
chatIDs := make(map[int64]bool)

ticker := time.NewTicker(durationUntil5())
ticker := time.NewTicker(bot.getDuration())

for {
select {
Expand All @@ -89,23 +106,14 @@ func (bot Bot) runBot() {
}
}

func durationUntil5() time.Duration {
currentTime := time.Now()
next5 := time.Date(
currentTime.Year(),
currentTime.Month(),
currentTime.Day(),
17,
0,
0,
0,
currentTime.Location(),
)
if currentTime.After(next5) {
next5 = next5.AddDate(0, 0, 1)
func (bot Bot) getDuration() time.Duration {
targetTime := bot.time

if time.Now().After(targetTime) {
targetTime = targetTime.AddDate(0, 0, 1)
}

return time.Until(next5)
return time.Until(targetTime)
}

func (bot Bot) sendReminder(chatID int64) {
Expand Down

0 comments on commit 1620e13

Please sign in to comment.