Skip to content

Instantly share code, notes, and snippets.

@Breci
Last active Feb 28, 2019
Embed
What would you like to do?
Code to reproduce the sendmessage bug in twitch extensions
const request = require('request');
const jwt = require('jsonwebtoken');
const EXTENSION_SECRET = ""; //secret of the extension
const CLIENT_ID = ""; //client id of the extension
const EXTENSION_VERSION = "0.0.1"; //current version of the extension
const EXTENSION_OWNER = ""; //account id of the extension developer
const CONFIGURATION_SERVICE_VERSION="1";//version of the configuration service segment
const TARGETED_CHANNEL = "";
function makeJWTToken(channelId) {
const payload = {
exp: Math.floor(Date.now() / 1000) + 2592000,
user_id: EXTENSION_OWNER, // extension owner ID for the call to Twitch PubSub
channel_id: channelId, // channel where the extension is activated
role: 'external',
};
return jwt.sign(payload, Buffer.from(EXTENSION_SECRET, 'base64'), { algorithm: 'HS256' });
}
function setExtensionConfigurationSegment(channelId, segment, content, version, callback) {
let p = new Promise(function(resolve, reject) {
//TODO : add content size check it should be less than 5KB (let's say 5000 characters)
if (content && typeof content !== "string") {
content = JSON.stringify(content);
}
if (content.length > 5000) {
reject()
}
// Set the HTTP headers required by the Twitch API.
const headers = {
'Client-Id': CLIENT_ID,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + makeJWTToken(channelId)
};
const body = JSON.stringify({
channel_id: channelId,
segment: segment,
content: content,
version: version
});
// Send the broadcast request to the Twitch API.
//const apiHost = ext.local ? 'localhost.rig.twitch.tv:3000' : 'api.twitch.tv';
const apiHost = 'api.twitch.tv';
request(
`https://${apiHost}/extensions/${CLIENT_ID}/configurations`, {
method: 'PUT',
headers,
body,
}, (err, res, body) => {
if (err) {
reject(err);
}
else {
resolve(body);
}
});
});
if (callback) {
p.then(callback.bind(null, null), callback.bind(null))
}
return p;
}
async function sendChatMessage(channelId, message, callback) {
let p = new Promise(function(resolve, reject) {
if (typeof message !== "string") {
message = JSON.stringify(message);
}
// Set the HTTP headers required by the Twitch API.
const headers = {
'Client-ID': CLIENT_ID,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + makeJWTToken(channelId)
};
const body = JSON.stringify({
text: message,
});
//const apiHost = ext.local ? 'localhost.rig.twitch.tv:3000' : 'api.twitch.tv';
const apiHost = 'api.twitch.tv';
request(
`https://${apiHost}/extensions/${CLIENT_ID}/${EXTENSION_VERSION}/channels/${channelId}/chat`, {
method: 'POST',
headers,
body,
}, (err, res, body) => {
if (err) {
reject(err);
}
else {
resolve(body);
}
});
});
if (callback) {
p.then(callback.bind(null, null), callback.bind(null))
}
return p;
}
setExtensionConfigurationSegment(TARGETED_CHANNEL, 'developer', "test", CONFIGURATION_SERVICE_VERSION).then(()=>{
return setExtensionConfigurationSegment(TARGETED_CHANNEL, 'broadcaster', "test", CONFIGURATION_SERVICE_VERSION);
}).then(()=>{
return sendChatMessage(TARGETED_CHANNEL, "test");
}).then((message)=>{
console.log(message)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment