Skip to content

Commit

Permalink
add restarted message after update command
Browse files Browse the repository at this point in the history
  • Loading branch information
akshettrj committed Jan 14, 2023
1 parent 7e49f6e commit ea6c158
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/exec"
"strconv"
"time"

"watgbridge/database"
Expand All @@ -13,6 +14,7 @@ import (
"watgbridge/utils"
"watgbridge/whatsapp"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/go-co-op/gocron"
)

Expand Down Expand Up @@ -102,5 +104,38 @@ func main() {
}
})

{
isRestarted, found := os.LookupEnv("WATG_IS_RESTARTED")
if !found || isRestarted != "1" {
goto SKIP_RESTART
}

chatIdString, chatIdFound := os.LookupEnv("WATG_CHAT_ID")
msgIdString, msgIdFound := os.LookupEnv("WATG_MESSAGE_ID")
threadIdString, threadIdFound := os.LookupEnv("WATG_THREAD_ID")
if !chatIdFound || !msgIdFound {
goto SKIP_RESTART
}

chatId, chatIdSuccess := strconv.ParseInt(chatIdString, 10, 64)
msgId, msgIdSuccess := strconv.ParseInt(msgIdString, 10, 64)
if chatIdSuccess != nil || msgIdSuccess != nil {
goto SKIP_RESTART
}

opts := gotgbot.SendMessageOpts{
ReplyToMessageId: msgId,
}
if threadIdFound {
threadId, threadIdSuccess := strconv.ParseInt(threadIdString, 10, 64)
if threadIdSuccess == nil {
opts.MessageThreadId = threadId
}
}

state.State.TelegramBot.SendMessage(chatId, "Successfully restarted", &opts)
}
SKIP_RESTART:

state.State.TelegramUpdater.Idle()
}
7 changes: 7 additions & 0 deletions telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ func UpdateAndRestartHandler(b *gotgbot.Bot, c *ext.Context) error {
}
utils.TgReplyTextByContext(b, c, "Successfully built the binary, now restarting...", nil)

os.Setenv("WATG_IS_RESTARTED", "1")
os.Setenv("WATG_CHAT_ID", fmt.Sprint(c.EffectiveChat.Id))
os.Setenv("WATG_MESSAGE_ID", fmt.Sprint(c.EffectiveMessage.MessageId))
if c.EffectiveMessage.IsTopicMessage {
os.Setenv("WATG_THREAD_ID", fmt.Sprint(c.EffectiveMessage.MessageThreadId))
}

err = syscall.Exec(path.Join(".", "watgbridge"), []string{}, os.Environ())
if err != nil {
return utils.TgReplyWithErrorByContext(b, c, "Failed to run exec syscall to restart the bot", err)
Expand Down

0 comments on commit ea6c158

Please sign in to comment.