-
Notifications
You must be signed in to change notification settings - Fork 1
/
SlackWebHook.kt
150 lines (123 loc) · 4.78 KB
/
SlackWebHook.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package dev.yunzai.slackwebhook
import android.content.Context
import com.google.gson.annotations.SerializedName
data class SlackWebHook(
var attachments: ArrayList<Attachment>? = null
) {
fun send(context: Context) {
this.sendMessage(context)
}
companion object {
@JvmStatic
fun builder() = Builder()
class Builder {
private var pretext: String? = null
private var fallback: String? = null
private var title: String? = null
private var text: String? = null
private var authorName: String? = null
private var authorLink: String? = null
private var authorIcon: String? = null
private var fields: ArrayList<Pair<String, String>>? = null
private var color: String? = null
private var isTimeStampEnable = false
private var imageUrl: String? = null
private var thumbUrl: String? = null
private var footer: String? = null
private var footerIcon: String? = null
fun pretext(pretext: String) = apply {
this.pretext = pretext
}
fun fallback(fallback: String) = apply {
this.fallback = fallback
}
fun title(title: String) = apply {
this.title = title
}
fun text(text: String) = apply {
this.text = text
}
fun authorName(authorName: String) = apply {
this.authorName = authorName
}
fun authorLink(authorLink: String) = apply {
this.authorLink = authorLink
}
fun authorIcon(authorIcon: String) = apply {
this.authorIcon = authorIcon
}
fun timeStampEnabled(isTimeStampEnable: Boolean) = apply {
this.isTimeStampEnable = isTimeStampEnable
}
fun fields(vararg fields: Pair<String, String>) = apply {
this.fields = arrayListOf(*fields)
}
fun color(color: String) = apply {
this.color = color
}
fun imageUrl(imageUrl: String) = apply {
this.imageUrl = imageUrl
}
fun thumbUrl(thumbUrl: String) = apply {
this.thumbUrl = thumbUrl
}
fun footer(footer: String) = apply {
this.footer = footer
}
fun footerIcon(footerIcon: String) = apply {
this.footerIcon = footerIcon
}
fun build(): SlackWebHook {
val attachment = Attachment()
attachment.pretext = pretext
attachment.fallback = fallback
attachment.title = title
attachment.text = text
attachment.authorName = authorName
attachment.authorLink = authorLink
attachment.authorIcon = authorIcon
attachment.color = color
attachment.imageUrl = imageUrl
attachment.thumbUrl = thumbUrl
attachment.footer = footer
attachment.footerIcon = footerIcon
if (isTimeStampEnable)
attachment.timeStamp = (System.currentTimeMillis() / 1000).toString()
if (!fields.isNullOrEmpty()) {
attachment.fields.addAll(fields!!.toFieldArrayList())
}
return SlackWebHook(
arrayListOf(attachment)
)
}
}
private fun ArrayList<Pair<String, String>>.toFieldArrayList(): ArrayList<Field> {
val fieldArrayList = arrayListOf<Field>()
this.forEach {
fieldArrayList.add(Field(it.first, it.second, false))
}
return fieldArrayList
}
}
}
data class Attachment(
var title: String? = null,
var text: String? = null,
var fallback: String? = null,
var color: String? = null,
var pretext: String? = null,
@SerializedName("author_name") var authorName: String? = null,
@SerializedName("author_link") var authorLink: String? = null,
@SerializedName("author_icon") var authorIcon: String? = null,
var fields: ArrayList<Field> = arrayListOf(),
@SerializedName("ts") var timeStamp: String? = null,
@SerializedName("image_url") var imageUrl: String? = null,
@SerializedName("thumb_url") var thumbUrl: String? = null,
var footer: String? = null,
@SerializedName("footer_icon") var footerIcon: String? = null
)
data class Field(
var title: String? = null,
var value: String? = null,
var short: Boolean = false
)