diff --git a/pkg/api/handlers_data.go b/pkg/api/handlers_data.go index d5ce06bb..645ca625 100644 --- a/pkg/api/handlers_data.go +++ b/pkg/api/handlers_data.go @@ -373,37 +373,37 @@ func (api *API) SearchDiscordProfile() gin.HandlerFunc { // SearchChannelMessages returns a gin.HandlerFunc that processes a request to search for messages in a Discord channel. func (api *API) SearchChannelMessages() gin.HandlerFunc { return func(c *gin.Context) { - var reqBody struct { + var reqParams struct { ChannelID string `json:"channelID"` - Limit int `json:"limit"` + Limit string `json:"limit"` Before string `json:"before"` } - // Extract the required parameter from the URL path - reqBody.ChannelID = c.Param("channelID") - if reqBody.ChannelID == "" { + reqParams.ChannelID = c.Param("channelID") + if reqParams.ChannelID == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "ChannelID must be provided and valid"}) return } - // Extract optional query parameters - if limitStr := c.Query("limit"); limitStr != "" { - if limit, err := strconv.Atoi(limitStr); err == nil { - reqBody.Limit = limit + reqParams.Limit = c.Query("limit") + reqParams.Before = c.Query("before") + + if reqParams.Limit != "" { + if _, err := strconv.Atoi(reqParams.Limit); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit parameter"}) + return } } - reqBody.Before = c.Query("before") - // worker handler implementation - bodyBytes, err := json.Marshal(reqBody) + bodyBytes, err := json.Marshal(reqParams) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Print the reqBody for debugging - logrus.Infof("Request Body: %+v", reqBody) + logrus.Infof("Request Body: %+v", reqParams) requestID := uuid.New().String() responseCh := pubsub2.GetResponseChannelMap().CreateChannel(requestID) diff --git a/pkg/scrapers/discord/getchannelmessages.go b/pkg/scrapers/discord/getchannelmessages.go index 0c889799..a26c6ae8 100644 --- a/pkg/scrapers/discord/getchannelmessages.go +++ b/pkg/scrapers/discord/getchannelmessages.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "os" + "strconv" "github.com/masa-finance/masa-oracle/pkg/llmbridge" ) @@ -26,9 +27,9 @@ type ChannelMessage struct { } // GetChannelMessages fetches messages for a specific channel from the Discord API -func GetChannelMessages(channelID string, limit int, before string) ([]ChannelMessage, error) { +func GetChannelMessages(channelID string, limit string, before string) ([]ChannelMessage, error) { // Print the parameters for debugging - fmt.Printf("Parameters - channelID: %s, limit: %d, before: %s\n", channelID, limit, before) + fmt.Printf("Parameters - channelID: %s, limit: %s, before: %s\n", channelID, limit, before) botToken := os.Getenv("DISCORD_BOT_TOKEN") // Replace with your actual environment variable name if botToken == "" { @@ -41,9 +42,11 @@ func GetChannelMessages(channelID string, limit int, before string) ([]ChannelMe return nil, err } + limitCheck, _ := strconv.Atoi(limit) + // Add query parameters if they are provided q := req.URL.Query() - if limit > 0 && limit <= 100 { + if limitCheck > 0 && limitCheck <= 100 { q.Add("limit", fmt.Sprintf("%d", limit)) } if before != "" { @@ -83,7 +86,7 @@ func GetChannelMessages(channelID string, limit int, before string) ([]ChannelMe // ScrapeDiscordMessagesForSentiment scrapes messages from a Discord channel and analyzes their sentiment. func ScrapeDiscordMessagesForSentiment(channelID string, model string, prompt string) (string, string, error) { // Fetch messages from the Discord channel - messages, err := GetChannelMessages(channelID, 100, "") + messages, err := GetChannelMessages(channelID, "100", "") if err != nil { return "", "", fmt.Errorf("error fetching messages from Discord channel: %v", err) } diff --git a/pkg/workers/handler.go b/pkg/workers/handler.go index 9b108fae..e6e0a60a 100644 --- a/pkg/workers/handler.go +++ b/pkg/workers/handler.go @@ -99,13 +99,7 @@ func (a *Worker) HandleWork(ctx actor.Context, m *messages.Work, node *masa.Orac resp, err = discord.GetUserProfile(userID) case string(WORKER.DiscordChannelMessages): channelID := bodyData["channelID"].(string) - limit := bodyData["limit"].(int) - before := bodyData["before"].(string) - - // Print the values for debugging - logrus.Infof("GetChannelMessages called with channelID: %s, limit: %d, before: %s", channelID, limit, before) - - resp, err = discord.GetChannelMessages(channelID, limit, before) + resp, err = discord.GetChannelMessages(channelID, bodyData["limit"].(string), bodyData["before"].(string)) case string(WORKER.DiscordSentiment): logrus.Infof("[+] Discord Channel Messages %s %s", m.Data, m.Sender) channelID := bodyData["channelID"].(string) diff --git a/pkg/workers/workers.go b/pkg/workers/workers.go index 5bfdd00e..10cd88b6 100644 --- a/pkg/workers/workers.go +++ b/pkg/workers/workers.go @@ -140,7 +140,7 @@ func (a *Worker) Receive(ctx actor.Context) { workerDoneCh <- msg ctx.Poison(ctx.Self()) default: - logrus.Warningf("[+] Received unknown message: %T, message: %+v", m, m) + logrus.Warningf("[+] Received unknown message in workers: %T, message: %+v", m, m) } }