From a17b8da9ff248d05309555c74ce819c7a23fcd6f Mon Sep 17 00:00:00 2001 From: gempir Date: Sat, 17 Feb 2024 22:48:31 +0100 Subject: [PATCH] support sending messages --- internal/helixclient/chat.go | 31 ++++++++++++++++++++++++++++--- internal/helixclient/client.go | 4 ++-- internal/helixclient/testing.go | 2 +- web/src/components/Bot/Bot.tsx | 13 ------------- web/src/factory/createLoginUrl.ts | 2 +- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/internal/helixclient/chat.go b/internal/helixclient/chat.go index a31c119..e60a060 100644 --- a/internal/helixclient/chat.go +++ b/internal/helixclient/chat.go @@ -1,7 +1,32 @@ package helixclient -import "github.com/nicklaw5/helix/v2" +import ( + "context" -func (c *HelixClient) SendChatMessage(params *helix.SendChatMessageParams) (*helix.ChatMessageResponse, error) { - return c.Client.SendChatMessage(params) + "github.com/carlmjohnson/requests" + "github.com/nicklaw5/helix/v2" +) + +type SendChatMessageResponse struct { + Data []struct { + MessageID string `json:"message_id"` + IsSent bool `json:"is_sent"` + } `json:"data"` +} + +func (c *HelixClient) SendChatMessage(params *helix.SendChatMessageParams) (*SendChatMessageResponse, error) { + var resp SendChatMessageResponse + + err := requests. + URL(TWITCH_API). + BodyJSON(params). + Bearer(c.AppAccessToken.AccessToken). + Header("Client-Id", c.clientID). + ContentType("application/json"). + Path("/helix/chat/messages"). + ToJSON(&resp). + Post(). + Fetch(context.Background()) + + return &resp, err } diff --git a/internal/helixclient/client.go b/internal/helixclient/client.go index bcddef6..7ebbc2b 100644 --- a/internal/helixclient/client.go +++ b/internal/helixclient/client.go @@ -36,7 +36,7 @@ type Client interface { SetUserAccessToken(token string) ValidateToken(accessToken string) (bool, *helix.ValidateTokenResponse, error) RequestUserAccessToken(code string) (*helix.UserAccessTokenResponse, error) - SendChatMessage(params *helix.SendChatMessageParams) (*helix.ChatMessageResponse, error) + SendChatMessage(params *helix.SendChatMessageParams) (*SendChatMessageResponse, error) } // Client wrapper for helix @@ -65,7 +65,7 @@ func init() { const TWITCH_API = "https://api.twitch.tv/" -var scopes = []string{"channel:read:redemptions", "channel:manage:redemptions", "channel:read:predictions", "channel:manage:predictions moderation:read channel:bot user:write:chat"} +var scopes = []string{"channel:read:redemptions", "channel:manage:redemptions", "channel:read:predictions", "channel:manage:predictions moderation:read channel:bot user:write:chat user:bot"} // NewClient Create helix client func NewClient(cfg *config.Config, db store.Store) *HelixClient { diff --git a/internal/helixclient/testing.go b/internal/helixclient/testing.go index e9f0c07..cebc479 100644 --- a/internal/helixclient/testing.go +++ b/internal/helixclient/testing.go @@ -90,6 +90,6 @@ func (m *MockHelixClient) RequestUserAccessToken(code string) (*helix.UserAccess return nil, nil } -func (m *MockHelixClient) SendChatMessage(params *helix.SendChatMessageParams) (*helix.ChatMessageResponse, error) { +func (m *MockHelixClient) SendChatMessage(params *helix.SendChatMessageParams) (*SendChatMessageResponse, error) { return nil, nil } diff --git a/web/src/components/Bot/Bot.tsx b/web/src/components/Bot/Bot.tsx index 2c84fcd..03efe86 100644 --- a/web/src/components/Bot/Bot.tsx +++ b/web/src/components/Bot/Bot.tsx @@ -77,19 +77,6 @@ export function Bot() { - {isDev &&
-
-
-

Media Commands

-
-
    -
  • !sr {""}
  • -
-
-
- -
-
} ; } diff --git a/web/src/factory/createLoginUrl.ts b/web/src/factory/createLoginUrl.ts index 4c264e7..f146cce 100644 --- a/web/src/factory/createLoginUrl.ts +++ b/web/src/factory/createLoginUrl.ts @@ -3,7 +3,7 @@ export function createLoginUrl(apiBaseUrl: string, twitchClientId: string): URL url.searchParams.set("client_id", twitchClientId); url.searchParams.set("redirect_uri", apiBaseUrl + "/api/callback"); url.searchParams.set("response_type", "code"); - url.searchParams.set("scope", "channel:read:redemptions channel:manage:redemptions channel:read:predictions channel:manage:predictions moderation:read channel:bot user:write:chat"); + url.searchParams.set("scope", "channel:read:redemptions channel:manage:redemptions channel:read:predictions channel:manage:predictions moderation:read channel:bot user:write:chat user:bot"); return url; } \ No newline at end of file