Skip to content

Commit

Permalink
Login sessions persist; start/stop all chat bot messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tylertravisty committed Feb 9, 2024
1 parent c090dc8 commit 5c3fa66
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 61 deletions.
2 changes: 2 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Doing

Reset session information in config on logout

Show error when choosing file "chooseFile"
Show filename in chat bot list
Add styling to choose file button
Expand Down
198 changes: 188 additions & 10 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,76 @@ func (a *App) UpdateChatMessage(cid string, cm config.ChatMessage) (map[string]c
return a.cfg.Channels[cid].ChatBot.Messages, nil
}

func (a *App) NewChatBot(cid string, username string, password string, streamUrl string) error {
type NewChatBotResponse struct {
LoggedIn bool `json:"logged_in"`
StreamUrl string `json:"stream_url"`
Username string `json:"username"`
}

func (a *App) GetChatBot(cid string) (NewChatBotResponse, error) {
if a.cb == nil {
return NewChatBotResponse{}, fmt.Errorf("Chat bot not initalized.")
}

loggedIn, err := a.cb.LoggedIn()
if err != nil {
a.logError.Println("error checking if chat bot is logged in:", err)
return NewChatBotResponse{}, fmt.Errorf("Error checking if chat bot is logged in. Try again.")
}

return NewChatBotResponse{loggedIn, a.cb.Cfg.Session.Client.StreamUrl, a.cb.Cfg.Session.Username}, nil
}

func (a *App) NewChatBot(cid string) (NewChatBotResponse, error) {
a.cbMu.Lock()
defer a.cbMu.Unlock()

if a.cb != nil {
err := a.resetChatBot()
if err != nil {
a.logError.Println("error resetting chat bot:", err)
return NewChatBotResponse{}, fmt.Errorf("Error creating chat bot. Try Again.")
}
}
channel, exists := a.cfg.Channels[cid]
if !exists {
a.logError.Println("channel does not exist:", cid)
return NewChatBotResponse{}, fmt.Errorf("Channel does not exist.")
}

if channel.ChatBot.Session.Client.StreamUrl == "" {
return NewChatBotResponse{}, nil
}

var err error
a.cb, err = chatbot.NewChatBot(a.ctx, channel.ChatBot, a.logError)
if err != nil {
a.logError.Println("error creating new chat bot:", err)
return NewChatBotResponse{}, fmt.Errorf("Error creating new chat bot. Try again.")
}

loggedIn, err := a.cb.LoggedIn()
if err != nil {
a.logError.Println("error checking if chat bot is logged in:", err)
return NewChatBotResponse{}, fmt.Errorf("Error checking if chat bot is logged in. Try again.")
}

if loggedIn {
err = a.cb.StartChatStream()
if err != nil {
a.logError.Println("error starting chat stream:", err)
return NewChatBotResponse{}, fmt.Errorf("Error connecting to chat. Try again.")
}
}

return NewChatBotResponse{loggedIn, channel.ChatBot.Session.Client.StreamUrl, channel.ChatBot.Session.Username}, nil
}

func (a *App) LoginChatBot(cid string, username string, password string, streamUrl string) error {
a.cbMu.Lock()
defer a.cbMu.Unlock()
a.cfgMu.Lock()
defer a.cfgMu.Unlock()

if a.cb != nil {
err := a.resetChatBot()
Expand All @@ -231,35 +298,151 @@ func (a *App) NewChatBot(cid string, username string, password string, streamUrl
a.logError.Println("channel does not exist:", cid)
return fmt.Errorf("Channel does not exist.")
}
channel.ChatBot.Session.Client.StreamUrl = streamUrl

var err error
a.cb, err = chatbot.NewChatBot(a.ctx, streamUrl, channel.ChatBot, a.logError)
a.cb, err = chatbot.NewChatBot(a.ctx, channel.ChatBot, a.logError)
if err != nil {
a.logError.Println("error creating new chat bot:", err)
return fmt.Errorf("Error creating new chat bot. Try again.")
}

err = a.cb.Login(username, password)
cookies, err := a.cb.Login(username, password)
if err != nil {
a.logError.Println("error logging into chat bot:", err)
return fmt.Errorf("Error logging in. Try again.")
}

channel.ChatBot.Session = config.ChatBotSession{
Client: rumblelivestreamlib.NewClientOptions{
Cookies: cookies,
StreamUrl: streamUrl,
},
Username: username,
}
a.cfg.Channels[cid] = channel
err = a.cfg.Save()
if err != nil {
a.logError.Println("error saving config:", err)
return fmt.Errorf("Error saving session information. Try again.")
}

a.cb.Cfg.Session = channel.ChatBot.Session

err = a.cb.StartChatStream()
if err != nil {
a.logError.Println("error starting chat stream:", err)
return fmt.Errorf("Error connecting to chat. Try again.")
}

// a.cb = cb
return nil
}

func (a *App) ResetChatBot() error {
func (a *App) StopAllChatBot(cid string) error {
err := a.cb.StopAllMessages()
if err != nil {
a.logError.Println("error stopping all chat bot messages:", err)
return fmt.Errorf("Error stopping messages.")
}

return nil
}

func (a *App) StartAllChatBot(cid string) error {
err := a.cb.StartAllMessages()
if err != nil {
a.logError.Println("error starting all chat bot messages:", err)
return fmt.Errorf("Error starting messages.")
}

return nil
}

func (a *App) UpdateChatBotUrl(cid string, streamUrl string) error {
a.cbMu.Lock()
defer a.cbMu.Unlock()
a.cfgMu.Lock()
defer a.cfgMu.Unlock()

if a.cb == nil {
return fmt.Errorf("Chat bot not initalized.")
}

err := a.resetChatBot()
if err != nil {
a.logError.Println("error resetting chat bot:", err)
return fmt.Errorf("Error creating chat bot. Try Again.")
}

channel, exists := a.cfg.Channels[cid]
if !exists {
a.logError.Println("channel does not exist:", cid)
return fmt.Errorf("Channel does not exist.")
}
channel.ChatBot.Session.Client.StreamUrl = streamUrl

a.cb, err = chatbot.NewChatBot(a.ctx, channel.ChatBot, a.logError)
if err != nil {
a.logError.Println("error creating new chat bot:", err)
return fmt.Errorf("Error creating new chat bot. Try again.")
}

a.cfg.Channels[cid] = channel
err = a.cfg.Save()
if err != nil {
a.logError.Println("error saving config:", err)
return fmt.Errorf("Error saving session information. Try again.")
}

a.cb.Cfg.Session.Client.StreamUrl = streamUrl

err = a.cb.StartChatStream()
if err != nil {
a.logError.Println("error starting chat stream:", err)
return fmt.Errorf("Error connecting to chat. Try again.")
}

return nil
}

func (a *App) ResetChatBot(cid string, logout bool) error {
a.cbMu.Lock()
defer a.cbMu.Unlock()
a.cfgMu.Lock()
defer a.cfgMu.Unlock()

if a.cb == nil {
return nil
}

err := a.cb.StopAllMessages()
if err != nil {
return fmt.Errorf("error stopping all chat bot messages: %v", err)
}

if logout {
err := a.cb.Logout()
if err != nil {
return fmt.Errorf("error logging out of chat bot: %v", err)
}

//TODO: reset session in config
channel, exists := a.cfg.Channels[cid]
if !exists {
a.logError.Println("channel does not exist:", cid)
return fmt.Errorf("Channel does not exist.")
}

channel.ChatBot.Session = config.ChatBotSession{}
a.cfg.Channels[cid] = channel
err = a.cfg.Save()
if err != nil {
a.logError.Println("error saving config:", err)
return fmt.Errorf("Error saving session information. Try again.")
}
}

err = a.resetChatBot()
if err != nil {
a.logError.Println("error resetting chat bot:", err)
return fmt.Errorf("Error resetting chat bot. Try Again.")
Expand All @@ -284,11 +467,6 @@ func (a *App) resetChatBot() error {
return fmt.Errorf("error stopping chat stream: %v", err)
}

err = a.cb.Logout()
if err != nil {
return fmt.Errorf("error logging out of chat bot: %v", err)
}

a.cb = nil

return nil
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/assets/icons/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import heart from './heart-fill.png';
import house from './house.png';
import pause from './pause-fill.png';
import play from './play-fill.png';
import play_green from './play-fill-green.png';
import plus_circle from './plus-circle-fill.png';
import star from './star-fill.png';
import stop from './stop-fill.png';
import thumbs_down from './hand-thumbs-down.png';
import thumbs_up from './hand-thumbs-up.png';
import x_lg from './x-lg.png';
Expand All @@ -20,8 +22,10 @@ export const Heart = heart;
export const House = house;
export const Pause = pause;
export const Play = play;
export const PlayGreen = play_green;
export const PlusCircle = plus_circle;
export const Star = star;
export const Stop = stop;
export const ThumbsDown = thumbs_down;
export const ThumbsUp = thumbs_up;
export const XLg = x_lg;
Binary file added frontend/src/assets/icons/play-fill-green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/src/assets/icons/stop-fill.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions frontend/src/components/ChatBot.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@
padding: 10px;
resize: none;
width: 100%;
}

.chat-bot-description {
align-items: center;
display: flex;
flex-direction: row;
justify-content: start;
padding-top: 10px;
width: 100%;
}

.chat-bot-description-label {
color: white;
font-family: sans-serif;
font-size: 20px;
padding-bottom: 5px;
padding-right: 5px;
}

0 comments on commit 5c3fa66

Please sign in to comment.