so I am creating a EventSub via websockets once the websocket is open then it waits for a notification (“message”) and it will send a message to the chat of the broadcaster that works well but then if you try to do that a second time it doesnt do it. it console logs it but it doesnt appear on the chat on twitch. when i console log the data of the sent message the message id is the exact same one for all the messages
let websocketSessionID;
// Function to start the WebSocket client
function startWebSocketClient() {
const websocketClient = new WebSocket(EVENTSUB_WEBSOCKET_URL);
websocketClient.onopen = () => {
console.log('WebSocket connection opened to ' + EVENTSUB_WEBSOCKET_URL);
};
websocketClient.onerror = (error) => {
console.error('WebSocket error:', error);
};
websocketClient.onmessage = (event) => {
try {
const dataInfo = JSON.parse(event.data);
console.log('Received WebSocket message:', dataInfo);
if (dataInfo.metadata.message_type === 'session_welcome') {
websocketSessionID = dataInfo.payload.session.id;
console.log('WebSocket Session ID:', websocketSessionID);
// Call the function to subscribe to the event once the session ID is received
subscribeToChatMessage();
}
if (dataInfo.metadata.message_type === 'notification') {
console.log('Notification received:', dataInfo.payload.event);
console.log('Message Text:', dataInfo.payload.event.message.text);
// Call the function to send a chat message
sendChatMessage('Hello, world! I am testing this thing out');
} } catch (error) {
console.error('Error processing WebSocket message:', error);
}
};
}
// Function to subscribe to the channel.chat.message event
function subscribeToChatMessage() {
const url = 'https://api.twitch.tv/helix/eventsub/subscriptions';
const accessToken = OAUTH_TOKEN;
const clientId = CLIENT_ID;
const requestBody = {
type: 'channel.chat.message',
version: '1',
condition: {
user_id: BOT_USER_ID,
broadcaster_user_id: CHAT_CHANNEL_USER_ID
},
transport: {
method: 'websocket',
session_id: websocketSessionID,
},
};
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
'Client-Id': clientId,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
};
fetch(url, options)
.then((response) => {
if (!response.ok) {
return response.json().then((errorData) => {
throw new Error(`Request failed: ${errorData.message}`);
});
}
return response.json();
})
.then((data) => {
console.log('Subscription created successfully:', data);
})
.catch((error) => {
console.error('Error:', error);
});
}
function sendChatMessage(message) {
const url = 'https://api.twitch.tv/helix/chat/messages';
const accessToken = OAUTH_TOKEN;
const clientId = CLIENT_ID;
const requestBody = {
broadcaster_id: CHAT_CHANNEL_USER_ID,
sender_id: BOT_USER_ID,
message: message
};
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
'Client-Id': clientId,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
};
fetch(url, options)
.then((response) => {
if (!response.ok) {
return response.json().then((errorData) => {
throw new Error(`Request failed: ${errorData.message}`);
});
}
return response.json();
})
.then((data) => {
console.log('Chat message sent successfully:', data);
})
.catch((error) => {
console.error('Error:', error);
});
}
// Start the WebSocket client
startWebSocketClient();