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

Commit b2ee32f

Browse files
committed
first commit, version 1.0.0
1 parent 1925eb5 commit b2ee32f

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

lib/slack_html.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library slack_html;
2+
3+
import 'dart:html';
4+
import 'package:slack/src/slacksrc.dart';
5+
export 'package:slack/src/slacksrc.dart';
6+
7+
8+
/**
9+
* Posts a Slack message to the properly authenticated Slack token.
10+
* The messages will go to whatever channel the token was set up for.
11+
*/
12+
send(Message m) {
13+
String outurl = 'https://' + team + '.slack.com/services/hooks/incoming-webhook?token=' + token;
14+
String payload = m.toString();
15+
HttpRequest.postFormData(outurl,{'payload' : payload});
16+
}
17+
18+
19+
20+
21+
22+
23+

lib/slack_io.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library slack_io;
2+
3+
import 'package:http/http.dart' as http;
4+
import 'package:slack/src/slacksrc.dart';
5+
export 'package:slack/src/slacksrc.dart';
6+
7+
8+
/**
9+
* Posts a Slack message to the properly authenticated Slack token.
10+
* The messages will go to whatever channel the token was set up for.
11+
*/
12+
send(Message m) {
13+
String outurl = 'https://' + team + '.slack.com/services/hooks/incoming-webhook?token=' + token;
14+
String payload = m.toString();
15+
http.post(outurl, body: {'payload' : payload});
16+
}

lib/src/slacksrc.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import 'dart:convert';
2+
3+
String token, team;
4+
5+
// A message passed between your app and slack.
6+
class Message {
7+
// Basic message vars
8+
String text;
9+
String username;
10+
String icon_url;
11+
String icon_emoji;
12+
13+
/*
14+
* Any number of attachments
15+
* can be added to the message
16+
*/
17+
List <Attachment> attachments;
18+
19+
String toString() {
20+
Map message = new Map();
21+
22+
if (text != null)
23+
message['text'] = text;
24+
25+
if (username != null)
26+
message['username'] = username;
27+
28+
if (icon_url != null)
29+
message['icon_url'] = icon_url;
30+
31+
if (icon_emoji != null)
32+
message['icon_emoji'] = icon_emoji;
33+
34+
if (attachments != null) {
35+
List attached_maps = [];
36+
for (Attachment a in attachments)
37+
attached_maps.add(a.toMap());
38+
message['attachments'] = attached_maps;
39+
}
40+
return JSON.encoder.convert(message);
41+
}
42+
}
43+
44+
class Attachment {
45+
String fallback; // Required
46+
String pretext;
47+
String text;
48+
String color; // 'good', 'warning', 'danger' or hex.
49+
50+
Map<String,String> fields;
51+
52+
String toString() {
53+
return JSON.encoder.convert(this.toMap());
54+
}
55+
56+
Map toMap() {
57+
Map attachment = new Map()
58+
..['fallback'] = fallback;
59+
60+
if (pretext != null)
61+
attachment['pretext'] = pretext;
62+
if (text != null)
63+
attachment['text'] = text;
64+
if (color != null)
65+
attachment['color'] = color;
66+
67+
if (fields != null)
68+
attachment['fields'] = fields;
69+
70+
return attachment;
71+
}
72+
73+
}

pubspec.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: slack
2+
version: 1.0.0
3+
author: Paul VanKeuren <paul.vankeuren@gmail.com>
4+
description: |-
5+
Package for accessing and utilizing the Slack webhook API.
6+
Currently only works for 'incoming webhooks'
7+
homepage: www.google.com/+PaulVanKeuren
8+
dependencies:
9+
http: any
10+
dev_dependencies:
11+
browser: any

test/test_html.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:slack/slack_html.dart';
2+
3+
4+
main() {
5+
token = 'oaV4CYX5OLlfwO3gciyhBxB8';
6+
team = 'cou';
7+
8+
Message m = new Message()
9+
..username = 'Street Spirit'
10+
..text = 'I am a spirit!'
11+
..icon_url = 'http://childrenofur.com/assets/street_spirit.png';
12+
13+
14+
send(m);
15+
print(m);
16+
}

test/test_html.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>test_html</title>
8+
</head>
9+
10+
<body>
11+
<p id="text"></p>
12+
<script type="application/dart" src="test_html.dart"></script>
13+
<!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
14+
<script src="packages/browser/dart.js"></script>
15+
</body>
16+
</html>

test/test_io.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:slack/slack_io.dart';
2+
3+
main() {
4+
token = 'oaV4CYX5OLlfwO3gciyhBxB8';
5+
team = 'cou';
6+
7+
Message m = new Message()
8+
..username = 'Street Spirit'
9+
..text = 'I am a spirit!'
10+
..icon_url = 'http://childrenofur.com/assets/street_spirit.png';
11+
print(send(m));
12+
}

0 commit comments

Comments
 (0)