Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Last active October 27, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenfeather/037a43f7790e46c0039936dcfbbaaeb2 to your computer and use it in GitHub Desktop.
Save stephenfeather/037a43f7790e46c0039936dcfbbaaeb2 to your computer and use it in GitHub Desktop.
Small library for Titanium to send info to a Slack webhook for 'remote logging' during development/testing
// Copyright 2016 Stephen Feather
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Usage:
//
// var slackLogger = require('slackLogger');
//
// slackLogger.init({webhookUrl: 'https://hooks.slack.com/services/fooBar/baz'});
//
// slackLogger.log({
// text: 'test text',
// username: 'logging-bot',
// icon_emoji: ':ghost:',
// channel: '#mobile_logging'
// });
var webhookUrl;
exports.init = function(args) {
if (!args) {
console.error('slackLogger.init() requires webhookUrl');
return;
}
if (args.webhookUrl) {
webhookUrl = args.webhookUrl;
}
}
exports.log = function(args) {
var payload = {};
if (!webhookUrl){
console.error('slackLogger requires initialization');
return;
}
if (!args || !args.text) {
console.error('slackLogger requires minimum {text}. Available params {text, channel, username, icon_emoji, callback}');
return;
} else {
payload.text = args.text
}
if (!args.channel) {
channel = "#general";
} else {
payload.channel = args.channel;
}
if (args.username) {
payload.username = args.username;
}
if (args.icon_emoji) {
payload.icon_emoji = args.icon_emoji
}
var client = Ti.Network.createHTTPClient({
onload: function(result) {
if (args.callback) {
callback(result);
}
},
onerror: function(error) {
if (args.callback) {
callback(error);
}
}
});
client.open('POST', webhookUrl);
client.setRequestHeader('Content-Type', 'application/json');
client.send(JSON.stringify(payload));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment