Skip to content
This repository has been archived by the owner on Mar 19, 2019. It is now read-only.

Commit

Permalink
first commit, version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ppvk committed May 29, 2014
1 parent 1925eb5 commit b2ee32f
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/slack_html.dart
@@ -0,0 +1,23 @@
library slack_html;

import 'dart:html';
import 'package:slack/src/slacksrc.dart';
export 'package:slack/src/slacksrc.dart';


/**
* Posts a Slack message to the properly authenticated Slack token.
* The messages will go to whatever channel the token was set up for.
*/
send(Message m) {
String outurl = 'https://' + team + '.slack.com/services/hooks/incoming-webhook?token=' + token;
String payload = m.toString();
HttpRequest.postFormData(outurl,{'payload' : payload});
}







16 changes: 16 additions & 0 deletions lib/slack_io.dart
@@ -0,0 +1,16 @@
library slack_io;

import 'package:http/http.dart' as http;
import 'package:slack/src/slacksrc.dart';
export 'package:slack/src/slacksrc.dart';


/**
* Posts a Slack message to the properly authenticated Slack token.
* The messages will go to whatever channel the token was set up for.
*/
send(Message m) {
String outurl = 'https://' + team + '.slack.com/services/hooks/incoming-webhook?token=' + token;
String payload = m.toString();
http.post(outurl, body: {'payload' : payload});
}
73 changes: 73 additions & 0 deletions lib/src/slacksrc.dart
@@ -0,0 +1,73 @@
import 'dart:convert';

String token, team;

// A message passed between your app and slack.
class Message {
// Basic message vars
String text;
String username;
String icon_url;
String icon_emoji;

/*
* Any number of attachments
* can be added to the message
*/
List <Attachment> attachments;

String toString() {
Map message = new Map();

if (text != null)
message['text'] = text;

if (username != null)
message['username'] = username;

if (icon_url != null)
message['icon_url'] = icon_url;

if (icon_emoji != null)
message['icon_emoji'] = icon_emoji;

if (attachments != null) {
List attached_maps = [];
for (Attachment a in attachments)
attached_maps.add(a.toMap());
message['attachments'] = attached_maps;
}
return JSON.encoder.convert(message);
}
}

class Attachment {
String fallback; // Required
String pretext;
String text;
String color; // 'good', 'warning', 'danger' or hex.

Map<String,String> fields;

String toString() {
return JSON.encoder.convert(this.toMap());
}

Map toMap() {
Map attachment = new Map()
..['fallback'] = fallback;

if (pretext != null)
attachment['pretext'] = pretext;
if (text != null)
attachment['text'] = text;
if (color != null)
attachment['color'] = color;

if (fields != null)
attachment['fields'] = fields;

return attachment;
}

}
11 changes: 11 additions & 0 deletions pubspec.yaml
@@ -0,0 +1,11 @@
name: slack
version: 1.0.0
author: Paul VanKeuren <paul.vankeuren@gmail.com>
description: |-
Package for accessing and utilizing the Slack webhook API.
Currently only works for 'incoming webhooks'
homepage: www.google.com/+PaulVanKeuren
dependencies:
http: any
dev_dependencies:
browser: any
16 changes: 16 additions & 0 deletions test/test_html.dart
@@ -0,0 +1,16 @@
import 'package:slack/slack_html.dart';


main() {
token = 'oaV4CYX5OLlfwO3gciyhBxB8';
team = 'cou';

Message m = new Message()
..username = 'Street Spirit'
..text = 'I am a spirit!'
..icon_url = 'http://childrenofur.com/assets/street_spirit.png';


send(m);
print(m);
}
16 changes: 16 additions & 0 deletions test/test_html.html
@@ -0,0 +1,16 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test_html</title>
</head>

<body>
<p id="text"></p>
<script type="application/dart" src="test_html.dart"></script>
<!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
<script src="packages/browser/dart.js"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions test/test_io.dart
@@ -0,0 +1,12 @@
import 'package:slack/slack_io.dart';

main() {
token = 'oaV4CYX5OLlfwO3gciyhBxB8';
team = 'cou';

Message m = new Message()
..username = 'Street Spirit'
..text = 'I am a spirit!'
..icon_url = 'http://childrenofur.com/assets/street_spirit.png';
print(send(m));
}

0 comments on commit b2ee32f

Please sign in to comment.