Skip to content

Commit ebed156

Browse files
Fixed fetch command not working with spaces in paths
Added unmute duration to /info Added option to specify the mute duration as a floating point value Added debug environment variable Changed error report minutes to TimeInterval and used DateComponentsFormatter to print the duration
1 parent 0753783 commit ebed156

File tree

8 files changed

+63
-20
lines changed

8 files changed

+63
-20
lines changed

NotifierBot/Sources/Notifier/Commands/InfoCommand.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ struct InfoCommand: Command {
5656
if !e.waitElement.isEmpty {
5757
lines.append("- Wait Element: ".escaped() + "`\(e.waitElement.escaped())`")
5858
}
59-
lines.append("- Muted: \(e.isMuted ? "Yes" : "No")".escaped())
59+
var durationString = ""
60+
if let restDuration = e.unmuteDate?.timeIntervalSince(Date()) {
61+
durationString = JFUtils.muteDurationFormatter.string(from: restDuration) ?? ""
62+
}
63+
lines.append("- Muted: \(e.isMuted ? "Yes (\(durationString) hours remaining)" : "No")".escaped())
6064
return lines.joined(separator: "\n")
6165
}
6266

NotifierBot/Sources/Notifier/Commands/MuteCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ struct MuteCommand: Command {
2929
let name = args[0].trimmingCharacters(in: .whitespaces)
3030
let hoursStr = args[1].trimmingCharacters(in: .whitespaces)
3131

32-
let hours = Int(hoursStr) ?? 0
32+
let hours = Double(hoursStr) ?? 0.0
3333
guard (hours > 0) else {
3434
// No valid int found
35-
try bot.sendMessage("\(hoursStr) is not a valid integer. Please enter a valid amount of hours > 0.", to: chatID)
35+
try bot.sendMessage("\(hoursStr) is not a valid number. Please enter a valid amount of hours > 0.", to: chatID)
3636
return
3737
}
3838

NotifierBot/Sources/Notifier/JFUtils.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ struct JFUtils {
3232
}
3333

3434
static func takeScreenshot(entry e: URLEntry, filename: String) {
35-
shell("\(kScreenshotScript) \"\(e.url)\" \"\(filename)\" \"\(e.delay)\" \"\(e.captureElement)\" \"\(e.clickElement)\" \"\(e.waitElement)\"")
35+
shell("\"\(kScreenshotScript)\" \"\(e.url)\" \"\(filename)\" \"\(e.delay)\" \"\(e.captureElement)\" \"\(e.clickElement)\" \"\(e.waitElement)\"")
3636
if e.area.width != 0 && e.area.height != 0 {
3737
// Crop the screenshot
38-
shell("\(kConvertPath) \(filename) -crop \(e.area.width)x\(e.area.height)+\(e.area.x)+\(e.area.y) \(filename)")
38+
shell("\"\(kConvertPath)\" \(filename) -crop \(e.area.width)x\(e.area.height)+\(e.area.x)+\(e.area.y) \(filename)")
3939
}
4040
}
4141

@@ -48,7 +48,7 @@ struct JFUtils {
4848
}
4949

5050
static private func sendImageOrFile(path: String, chatID: Int64, isFile: Bool, text: String? = nil) {
51-
var command = "\(kTelegramScript) -t \(token!) -c \(chatID) -\(isFile ? "f" : "i") \"\(path)\""
51+
var command = "\"\(kTelegramScript)\" -t \(token!) -c \(chatID) -\(isFile ? "f" : "i") \"\(path)\""
5252
if let text = text {
5353
// Encase the lines in parantheses
5454
let lines = text.components(separatedBy: .newlines).map({ "\"\($0)\"" })
@@ -74,6 +74,13 @@ struct JFUtils {
7474
return list.joined(separator: "\n")
7575
}
7676

77+
static var muteDurationFormatter: DateComponentsFormatter {
78+
let f = DateComponentsFormatter()
79+
f.unitsStyle = .abbreviated
80+
f.allowedUnits = [.year, .day, .hour, .minute]
81+
return f
82+
}
83+
7784
}
7885

7986
extension Dispatcher {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ For the scripts and the bot to work, you have to put your bot token in a file ca
115115
1. Start the bot
116116
2. Run the command `/myid` to retrieve your ID
117117
3. Stop the bot
118-
4. Add your ID to the permissions file: `echo "YOUR_ID: admin" > /path/to/your/install/directory/permissions.txt`
118+
4. Add your ID to the permissions file: `echo "YOUR_ID: admin" > /path/to/your/install/directory/NotifierBot/permissions.txt`
119119
5. Start the bot again and make sure, it worked by checking your permissions with the bot: `/getpermissions YOUR_ID`
120120
6. If the bot returned your permission level as **admin**, everything worked and you now have admin permissions
121121

Shared/Sources/Shared/JFLiterals.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ public let mainDirectory: String = {
2323
if components?.last == "urlwatcher" {
2424
components?.removeLast()
2525
}
26-
let mainDirectory = components?
26+
var mainDirectory = components?
2727
.joined(separator: "/")
28+
if !(ProcessInfo.processInfo.environment["NOTIFIER_BOT_DEBUG"]?.isEmpty ?? true) {
29+
// If the debug variable is set, we use the working directory as mainDirectory
30+
// (e.g. when debugging in Xcode)
31+
mainDirectory = FileManager.default.currentDirectoryPath
32+
print("Using debug directory as main: \(mainDirectory ?? "nil")")
33+
}
2834
print("Installation Directory: \(mainDirectory ?? "nil")")
2935

3036
// If no install directory could be constructed:
@@ -67,4 +73,4 @@ public let kDiffFile = "diff.png"
6773
/// The threshold value the bot uses. If the NCC value is below this, a notification is triggered
6874
public let kNccThreshold = 0.999
6975
/// The duration for which a screenshot capture error has to persist for the user to be notified. Set to 0 to immediately notify on errors
70-
public let kErrorReportMinutes = 120
76+
public let kErrorReportDuration: TimeInterval = 1

urlwatcher/.swiftpm/xcode/xcshareddata/xcschemes/urlwatcher.xcscheme

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
3737
launchStyle = "0"
3838
useCustomWorkingDirectory = "YES"
39-
customWorkingDirectory = "/Users/jonasfrey/Documents/_Developing/Xcode Projects/Bots/NotifierBot/urlwatcher"
39+
customWorkingDirectory = "/Users/jonasfrey/Documents/_Developing/Xcode Projects/Bots/NotifierBot"
4040
ignoresPersistentStateOnLaunch = "NO"
4141
debugDocumentVersioning = "YES"
4242
debugServiceExtension = "internal"
@@ -53,8 +53,13 @@
5353
</BuildableProductRunnable>
5454
<EnvironmentVariables>
5555
<EnvironmentVariable
56-
key = "PATH"
57-
value = "/Users/jonasfrey/tools:/usr/local/opt/gnu-getopt/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/opt/local/bin:/Users/jonasfrey/scripts:/Users/jonasfrey/flutter/bin"
56+
key = "NOTIFIER_BOT_DEBUG"
57+
value = "true"
58+
isEnabled = "YES">
59+
</EnvironmentVariable>
60+
<EnvironmentVariable
61+
key = "TELEGRAM_BOT_TOKEN"
62+
value = "5221316043:AAF-_gfAxbHVrW2HDfplfSYz3psPtseLVKM"
5863
isEnabled = "YES">
5964
</EnvironmentVariable>
6065
</EnvironmentVariables>

urlwatcher/Sources/urlwatcher/Functions.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import Foundation
99
import Shared
1010

11-
func takeScreenshot(ofURL url: String, outputPath: String, delay: Int? = nil, captureElement: String? = nil, clickElement: String? = nil, waitElement: String? = nil) throws -> BashResult {
11+
func takeScreenshot(ofURL url: String, outputPath: String, delay: Int? = nil, captureElement: String? = nil,
12+
clickElement: String? = nil, waitElement: String? = nil) throws -> BashResult {
1213
let screenshotCommand = "capture-website"
1314
var arguments = [
1415
url,
@@ -144,12 +145,13 @@ func sendTelegramMessage(_ message: String, to chatID: Int, image: String? = nil
144145
func handleScreenshotError(entry: URLEntry) throws {
145146
let errorFile = "\(directory(for: entry))/error"
146147
// If the errorReportMinutes are not set, we immediately notify the user
147-
guard kErrorReportMinutes > 0 else {
148+
guard kErrorReportDuration > 0 else {
148149
try notifyError(entry: entry)
149150
return
150151
}
151152

152-
// Before we notify the user, we check if the error hast persisted for the last few minutes to avoid notifying the user at single screenshot errors
153+
// Before we notify the user, we check if the error hast persisted for the last few minutes to avoid notifying the
154+
// user at single screenshot errors
153155
// We do this, by checking for a file 'error' and its creation date to see when the error first appeared.
154156
// This file should be deleted on the next successful capture
155157
if !fileManager.fileExists(atPath: errorFile) {
@@ -162,7 +164,7 @@ func handleScreenshotError(entry: URLEntry) throws {
162164
// If an error file already exists, get the attributes and check the creation date
163165
if fileManager.fileExists(atPath: errorFile),
164166
let creationDate = try fileManager.attributesOfItem(atPath: errorFile)[.creationDate] as? Date,
165-
creationDate.distance(to: Date()) >= Double(kErrorReportMinutes * 60) {
167+
creationDate.distance(to: Date()) >= kErrorReportDuration {
166168
// Notify the user that an error persisted for the last `errorReportTime` seconds
167169
try notifyError(entry: entry)
168170
}
@@ -174,7 +176,12 @@ func notifyError(entry: URLEntry) throws {
174176
return
175177
}
176178
print("Error taking screenshot. Notifying user...")
177-
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for the last \(kErrorReportMinutes) minutes.", to: Int(entry.chatID))
179+
let f = DateComponentsFormatter()
180+
f.allowedUnits = [.year, .day, .hour, .minute, .second]
181+
f.unitsStyle = .full
182+
let durationString = f.string(from: kErrorReportDuration) ?? "some time"
183+
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for at least \(durationString).",
184+
to: Int(entry.chatID))
178185
}
179186

180187
func notifyUnmuted(entry: URLEntry) throws {

urlwatcher/Sources/urlwatcher/main.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ import Shared
66
let silentMode: Bool = false
77

88
let fileManager = FileManager.default
9+
910
/// The telegram bot token is read from a file
10-
let telegramBotToken = try String(contentsOfFile: "\(mainDirectory)/BOT_TOKEN", encoding: .utf8).components(separatedBy: .newlines).first
11-
guard telegramBotToken != nil else {
12-
print("Unable to read bot token. Please place it into the file \(mainDirectory.components(separatedBy: "/").dropLast().joined(separator: "/"))/BOT_TOKEN")
11+
var telegramBotToken: String! = nil
12+
if let t = try? String(contentsOfFile: "\(mainDirectory)/BOT_TOKEN", encoding: .utf8)
13+
.components(separatedBy: .newlines)
14+
.first {
15+
// Read the token from a the file
16+
print("Reading token from file.")
17+
telegramBotToken = t
18+
} else if let t = ProcessInfo.processInfo.environment["TELEGRAM_BOT_TOKEN"] {
19+
// Read the token from the environment, if it exists
20+
print("Reading token from environment.")
21+
telegramBotToken = t
22+
} else {
23+
print("Unable to read bot token. Please place it into the file " +
24+
"\(mainDirectory.components(separatedBy: "/").dropLast().joined(separator: "/"))/BOT_TOKEN " +
25+
"or provide the environment variable TELEGRAM_BOT_TOKEN")
1326
exit(1)
1427
}
28+
1529
/// The script used to send the telegram messages
1630
let telegramScript = "\(mainDirectory)/tools/telegram.sh"
1731

0 commit comments

Comments
 (0)