summaryrefslogtreecommitdiff
path: root/app/internal/create_bot.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/internal/create_bot.go')
-rw-r--r--app/internal/create_bot.go74
1 files changed, 15 insertions, 59 deletions
diff --git a/app/internal/create_bot.go b/app/internal/create_bot.go
index 667ac0a..ce70cdd 100644
--- a/app/internal/create_bot.go
+++ b/app/internal/create_bot.go
@@ -3,45 +3,22 @@ package internal
import (
"fmt"
"log"
+ "net/http"
"os"
"os/exec"
+ "strings"
"syscall"
-
- zmq "github.com/pebbe/zmq4"
)
type Bot struct {
BotToken string `json:"bot_token"`
Cmd *exec.Cmd // Ajouter une référence à la commande
ProcessID string
- dealer *zmq.Socket // Stocker le PGID (Process Group ID)
- read bool
- readyChan chan bool // Canal pour indiquer que le bot est prêt
+ client *http.Client
}
func Start(b *Bot) (*Bot, error) {
- if b.readyChan == nil {
- b.readyChan = make(chan bool)
- }
-
// Create a new ZeroMQ socket specifically for this bot
- ctx, err := zmq.NewContext()
- if err != nil {
- return nil, fmt.Errorf("[SERVER] failed to create context: %w", err)
- }
-
- // Each bot gets its own dealer socket
- dealer, err := ctx.NewSocket(zmq.REP)
- if err != nil {
- return nil, fmt.Errorf("[SERVER] failed to create dealer: %w", err)
- }
-
- // Binding the socket to a specific address (may need to adjust the address based on your needs)
- err = dealer.Bind(fmt.Sprintf("tcp://localhost:%s", b.ProcessID))
- if err != nil {
- return nil, fmt.Errorf("[SERVER] failed to bind dealer: %w", err)
- }
-
// Configuration du bot
cmd := exec.Command("./discord-bot", b.BotToken, b.ProcessID) // Passer le port unique
cmd.SysProcAttr = &syscall.SysProcAttr{
@@ -56,20 +33,11 @@ func Start(b *Bot) (*Bot, error) {
return nil, fmt.Errorf("failed to start bot: %w", err)
}
b.Cmd = cmd
- b.dealer = dealer
- // Here we will receive messages from the bot in a separate goroutine
- for {
- msg, err := dealer.Recv(0)
- if err != nil {
- return nil, fmt.Errorf("[SERVER] failed to receive message: %w", err)
- }
- if msg == "ready" {
- log.Printf("[SERVER] Bot is ready")
- b.read = true
- break
- }
- }
+ client := http.Client{}
+ // Send data to the bot
+ b.client = &client
+ log.Printf("[SERVER] Bot %s started successfully with PID %d", b.BotToken, cmd.Process.Pid)
return b, nil
}
@@ -84,22 +52,6 @@ func (b *Bot) Stop() error {
}
func (b *Bot) SendMessage(message string) error {
- if b.dealer == nil {
- return fmt.Errorf("[SERVER] sender socket is not initialized")
- }
-
- // Wait for the bot to be ready if it's not already
- if !b.read {
- log.Printf("[SERVER] Waiting for bot %s to be ready...", b.BotToken)
- // this mean we should read the recv channel
- msg, err := b.dealer.Recv(0)
- if err != nil {
- return fmt.Errorf("[SERVER] failed to receive message: %w", err)
- }
- log.Printf("[SERVER] Received message from bot %s: %s", b.BotToken, msg)
- b.read = true
- }
-
// Check if the bot process is still running
if err := b.Cmd.Process.Signal(syscall.Signal(0)); err != nil {
return fmt.Errorf("[SERVER] bot process is not running: %w", err)
@@ -111,12 +63,16 @@ func (b *Bot) SendMessage(message string) error {
}
// Send the message
- _, err := b.dealer.Send(message, 0)
+ resp, err := b.client.Post("http://localhost:"+b.ProcessID, "application/json", strings.NewReader(message))
if err != nil {
- return fmt.Errorf("[SERVER] failed to send message to bot %s: %w", b.BotToken, err)
+ return fmt.Errorf("[SERVER] failed to send message: %w", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("[SERVER] failed to send message: %s", resp.Status)
}
- log.Printf("[SERVER] Sent message to bot %s: %s", b.BotToken, message)
+ // Log the message sent
+ log.Printf("[SERVER] Message sent to bot %s: %s", b.BotToken, message)
- b.read = false // Reset read state after sending the message
return nil
}