Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 58a6d99

Browse files
committed
Make notification_tag an optional parameter
1 parent 534980d commit 58a6d99

File tree

1 file changed

+163
-163
lines changed

1 file changed

+163
-163
lines changed

Noti/Ephemerals.swift

Lines changed: 163 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,163 @@
1-
//
2-
// Ephemerals.swift
3-
// Noti
4-
//
5-
// Created by Jari on 06/07/16.
6-
// Copyright © 2016 Jari Zwarts. All rights reserved.
7-
//
8-
// Ephemerals is in charge of sending out a range of short JSON messages which could be dismissals, quick replys, etc.
9-
// https://docs.pushbullet.com/#ephemerals
10-
//
11-
12-
import Foundation
13-
import Cocoa
14-
import Alamofire
15-
import SwiftyJSON
16-
17-
class Ephemerals: NSObject {
18-
var token:String
19-
var crypt:Crypt?
20-
21-
init(token:String) {
22-
self.token = token
23-
}
24-
25-
internal func sendEphemeral(_ body: JSON) {
26-
var body = body
27-
let headers: HTTPHeaders = [
28-
"Access-Token": token
29-
];
30-
31-
if crypt != nil && body["type"].exists() && body["type"].stringValue == "push" {
32-
print("Encrypting ephemeral...")
33-
let json = JSON.init(body)
34-
35-
let cipher = crypt!.encryptMessage(message: json["push"].rawString()!)
36-
body["push"] = [
37-
"encrypted": true,
38-
"ciphertext": cipher!
39-
]
40-
}
41-
42-
print("Sending ephemeral...")
43-
print("-------- BODY --------")
44-
debugPrint(body)
45-
print("----------------------")
46-
47-
Alamofire.request("https://api.pushbullet.com/v2/ephemerals", method: .post, parameters: body.dictionaryObject!, encoding: JSONEncoding.default, headers: headers)
48-
.responseString { response in
49-
var result = JSON(parseJSON: response.result.value!)
50-
if(response.response?.statusCode != 200) {
51-
52-
let alert = NSAlert()
53-
alert.messageText = "Unable to send ephemeral!"
54-
if result["error"].exists() {
55-
alert.informativeText = result["error"]["type"].string! + ": " + result["error"]["message"].string!
56-
}
57-
58-
alert.runModal()
59-
}
60-
debugPrint(response)
61-
}
62-
}
63-
64-
func respondToSMS(_ message: String!, thread_id: String!, source_device_iden: String!, source_user_iden: String!) {
65-
print("respondToSMS", "message", message, "thread_id", thread_id, "source_device_iden", source_device_iden)
66-
67-
//get api key from cookies
68-
var APIkey:String = ""
69-
for cookie in HTTPCookieStorage.shared.cookies! {
70-
if(cookie.domain == "www.pushbullet.com" && cookie.isSecure && cookie.name == "api_key" && cookie.path == "/") {
71-
APIkey = cookie.value
72-
}
73-
}
74-
print("Grabbed API key", APIkey)
75-
76-
if(APIkey == "") {
77-
let alert = NSAlert()
78-
alert.messageText = "Unable to retrieve API key from cookies"
79-
alert.informativeText = "Making Noti reauthorize with PushBullet will probably solve this. (click it's icon in your menu and choose 'Reauthorize')"
80-
alert.runModal()
81-
return
82-
}
83-
84-
let headers = [
85-
"Authorization": "Bearer " + APIkey
86-
];
87-
88-
//get thread recipients & send reply to them
89-
let body:JSON = [
90-
"key": source_device_iden + "_threads"
91-
]
92-
93-
Alamofire.request("https://api.pushbullet.com/v3/get-permanent", method: .post, parameters: body.dictionaryObject!, encoding: JSONEncoding.default, headers: headers)
94-
.responseString { response in
95-
debugPrint(response)
96-
var parsed = JSON(parseJSON: response.result.value!)
97-
98-
//decrypt if needed....
99-
if self.crypt != nil && parsed["data"]["encrypted"].exists() {
100-
parsed["data"] = JSON(parseJSON: (self.crypt!.decryptMessage(parsed["data"]["ciphertext"].string!))!)
101-
}
102-
103-
if let threads = parsed["data"]["threads"].array {
104-
for thread in threads {
105-
if thread["id"].string == thread_id {
106-
for recipient in thread["recipients"].array! {
107-
let body:JSON = [
108-
"push": [
109-
"conversation_iden": recipient["address"].string!,
110-
"message": message,
111-
"package_name": "com.pushbullet.android",
112-
"source_user_iden": source_user_iden,
113-
"target_device_iden": source_device_iden,
114-
"type": "messaging_extension_reply"
115-
],
116-
"type": "push"
117-
]
118-
self.sendEphemeral(body)
119-
}
120-
}
121-
}
122-
}
123-
}
124-
}
125-
126-
func quickReply(_ push: JSON, reply: String) {
127-
let body:JSON = [
128-
"type": "push",
129-
"push": [
130-
"type": "messaging_extension_reply",
131-
"source_user_iden": push["source_user_iden"].string!,
132-
"target_device_iden": push["source_device_iden"].string!,
133-
"package_name": push["package_name"].string!,
134-
"conversation_iden": push["conversation_iden"].string!,
135-
"message": reply
136-
]
137-
];
138-
139-
sendEphemeral(body)
140-
}
141-
142-
func dismissPush(_ push: JSON, trigger_key: String?) {
143-
var body:JSON = [
144-
"type": "push",
145-
"push": [
146-
"notification_id": push["notification_id"].string!,
147-
"notification_tag": push["notification_tag"].string!,
148-
"package_name": push["package_name"].string!,
149-
"source_user_iden": push["source_user_iden"].string!,
150-
"type": "dismissal"
151-
]
152-
];
153-
154-
if (trigger_key != nil) {
155-
var push = body["push"];
156-
push["trigger_action"] = JSON(trigger_key!)
157-
body["push"] = push
158-
}
159-
160-
sendEphemeral(body)
161-
}
162-
163-
}
1+
//
2+
// Ephemerals.swift
3+
// Noti
4+
//
5+
// Created by Jari on 06/07/16.
6+
// Copyright © 2016 Jari Zwarts. All rights reserved.
7+
//
8+
// Ephemerals is in charge of sending out a range of short JSON messages which could be dismissals, quick replys, etc.
9+
// https://docs.pushbullet.com/#ephemerals
10+
//
11+
12+
import Foundation
13+
import Cocoa
14+
import Alamofire
15+
import SwiftyJSON
16+
17+
class Ephemerals: NSObject {
18+
var token:String
19+
var crypt:Crypt?
20+
21+
init(token:String) {
22+
self.token = token
23+
}
24+
25+
internal func sendEphemeral(_ body: JSON) {
26+
var body = body
27+
let headers: HTTPHeaders = [
28+
"Access-Token": token
29+
];
30+
31+
if crypt != nil && body["type"].exists() && body["type"].stringValue == "push" {
32+
print("Encrypting ephemeral...")
33+
let json = JSON.init(body)
34+
35+
let cipher = crypt!.encryptMessage(message: json["push"].rawString()!)
36+
body["push"] = [
37+
"encrypted": true,
38+
"ciphertext": cipher!
39+
]
40+
}
41+
42+
print("Sending ephemeral...")
43+
print("-------- BODY --------")
44+
debugPrint(body)
45+
print("----------------------")
46+
47+
Alamofire.request("https://api.pushbullet.com/v2/ephemerals", method: .post, parameters: body.dictionaryObject!, encoding: JSONEncoding.default, headers: headers)
48+
.responseString { response in
49+
var result = JSON(parseJSON: response.result.value!)
50+
if(response.response?.statusCode != 200) {
51+
52+
let alert = NSAlert()
53+
alert.messageText = "Unable to send ephemeral!"
54+
if result["error"].exists() {
55+
alert.informativeText = result["error"]["type"].string! + ": " + result["error"]["message"].string!
56+
}
57+
58+
alert.runModal()
59+
}
60+
debugPrint(response)
61+
}
62+
}
63+
64+
func respondToSMS(_ message: String!, thread_id: String!, source_device_iden: String!, source_user_iden: String!) {
65+
print("respondToSMS", "message", message, "thread_id", thread_id, "source_device_iden", source_device_iden)
66+
67+
//get api key from cookies
68+
var APIkey:String = ""
69+
for cookie in HTTPCookieStorage.shared.cookies! {
70+
if(cookie.domain == "www.pushbullet.com" && cookie.isSecure && cookie.name == "api_key" && cookie.path == "/") {
71+
APIkey = cookie.value
72+
}
73+
}
74+
print("Grabbed API key", APIkey)
75+
76+
if(APIkey == "") {
77+
let alert = NSAlert()
78+
alert.messageText = "Unable to retrieve API key from cookies"
79+
alert.informativeText = "Making Noti reauthorize with PushBullet will probably solve this. (click it's icon in your menu and choose 'Reauthorize')"
80+
alert.runModal()
81+
return
82+
}
83+
84+
let headers = [
85+
"Authorization": "Bearer " + APIkey
86+
];
87+
88+
//get thread recipients & send reply to them
89+
let body:JSON = [
90+
"key": source_device_iden + "_threads"
91+
]
92+
93+
Alamofire.request("https://api.pushbullet.com/v3/get-permanent", method: .post, parameters: body.dictionaryObject!, encoding: JSONEncoding.default, headers: headers)
94+
.responseString { response in
95+
debugPrint(response)
96+
var parsed = JSON(parseJSON: response.result.value!)
97+
98+
//decrypt if needed....
99+
if self.crypt != nil && parsed["data"]["encrypted"].exists() {
100+
parsed["data"] = JSON(parseJSON: (self.crypt!.decryptMessage(parsed["data"]["ciphertext"].string!))!)
101+
}
102+
103+
if let threads = parsed["data"]["threads"].array {
104+
for thread in threads {
105+
if thread["id"].string == thread_id {
106+
for recipient in thread["recipients"].array! {
107+
let body:JSON = [
108+
"push": [
109+
"conversation_iden": recipient["address"].string!,
110+
"message": message,
111+
"package_name": "com.pushbullet.android",
112+
"source_user_iden": source_user_iden,
113+
"target_device_iden": source_device_iden,
114+
"type": "messaging_extension_reply"
115+
],
116+
"type": "push"
117+
]
118+
self.sendEphemeral(body)
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}
125+
126+
func quickReply(_ push: JSON, reply: String) {
127+
let body:JSON = [
128+
"type": "push",
129+
"push": [
130+
"type": "messaging_extension_reply",
131+
"source_user_iden": push["source_user_iden"].string!,
132+
"target_device_iden": push["source_device_iden"].string!,
133+
"package_name": push["package_name"].string!,
134+
"conversation_iden": push["conversation_iden"].string!,
135+
"message": reply
136+
]
137+
];
138+
139+
sendEphemeral(body)
140+
}
141+
142+
func dismissPush(_ push: JSON, trigger_key: String?) {
143+
var body:JSON = [
144+
"type": "push",
145+
"push": [
146+
"notification_id": push["notification_id"].string!,
147+
"notification_tag": push["notification_tag"].string,
148+
"package_name": push["package_name"].string!,
149+
"source_user_iden": push["source_user_iden"].string!,
150+
"type": "dismissal"
151+
]
152+
];
153+
154+
if (trigger_key != nil) {
155+
var push = body["push"];
156+
push["trigger_action"] = JSON(trigger_key!)
157+
body["push"] = push
158+
}
159+
160+
sendEphemeral(body)
161+
}
162+
163+
}

0 commit comments

Comments
 (0)