-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added postToTopic() for MessagingService needs (#793)
* Added postToTopic() * Update publishToTopic.js * Updated errors and doc example * Switched error handling to if else Avoid fall through and use of break in each case * Updated error message * Updated error handling * Add publishToTopic into typings
- Loading branch information
1 parent
c2dd3d3
commit e1c01e1
Showing
2 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const http = require('../util/http.js').func | ||
|
||
exports.required = ['universeId', 'topic', 'data'] | ||
exports.optional = ['jar'] | ||
|
||
// Docs | ||
/** | ||
* ☁️ Publish a message to a subscribed topic. | ||
* @category Game | ||
* @alias postToTopic | ||
* @param {number} universeId - The id of the universe. | ||
* @param {string} topic - The name of the topic. | ||
* @param {Object | string} data - The data to post. | ||
* @returns {Promise<boolean>} | ||
* @example const noblox = require("noblox.js") | ||
* const data = { targetUser: 123456789, staffMember: 1210019099, action: "Kick" } | ||
* | ||
* await noblox.publishToTopic(2152417643, "ModerateUser", data) | ||
**/ | ||
|
||
function publishToTopic (universeId, topic, data, jar) { | ||
return new Promise((resolve, reject) => { | ||
|
||
const httpOpt = { | ||
url: `//apis.roblox.com/messaging-service/v1/universes/${universeId}/topics/${topic}`, | ||
options: { | ||
json: true, | ||
resolveWithFullResponse: true, | ||
jar, | ||
method: 'POST', | ||
body: { message: JSON.stringify(data) }, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
} | ||
|
||
return http(httpOpt) | ||
.then(function (res) { | ||
if (res.statusCode === 200) { | ||
resolve(true) | ||
} else { | ||
if (typeof(res.body) === "string") { | ||
reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)) | ||
} else { | ||
const data = Object.assign(res.body) | ||
reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)) | ||
} | ||
} | ||
}) | ||
.catch(reject) | ||
}) | ||
} | ||
|
||
exports.func = function (args) { | ||
return publishToTopic(args.universeId, args.topic, args.data, args.jar) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters