Skip to content

Commit 856798b

Browse files
Added Info, SetWaitElement and SetClickElement commands
/diff command now uses the new folder names ("entry_name.chat_id")
1 parent 56afc29 commit 856798b

File tree

11 files changed

+221
-36
lines changed

11 files changed

+221
-36
lines changed

NotifierBot/Sources/Notifier/Commands/AddCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct AddCommand: Command {
3131
// Check if there is a valid URL
3232
guard url.starts(with: "http") else {
3333
// No valid URL found
34-
try bot.sendMessage("Please enter a valid URL, starting with 'http://' or 'https://'", to: chatID)
34+
try bot.sendMessage("\(url) is not a valid URL. Please enter a valid URL, starting with 'http://' or 'https://'", to: chatID)
3535
return
3636
}
3737

NotifierBot/Sources/Notifier/Commands/DiffCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ struct DiffCommand: Command {
3434
// Send the diff file and the contents of the ncc file
3535
let nccInfo = try? String(contentsOfFile: "\(mainDirectory!)/urlwatcher/images/\(realName)/\(nccFile)")
3636
print("Sending diff file and NCC information:\n\(nccInfo ?? "nil")")
37-
JFUtils.sendImage(path: "\(mainDirectory!)/urlwatcher/images/\(realName)/\(diffFile)", chatID: chatID, text: nccInfo)
37+
JFUtils.sendImage(path: "\(mainDirectory!)/urlwatcher/images/\(realName).\(chatID)/\(diffFile)", chatID: chatID, text: nccInfo)
3838
}
3939
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// InfoCommand.swift
3+
//
4+
//
5+
// Created by Jonas Frey on 01.05.21.
6+
//
7+
8+
import Foundation
9+
import Telegrammer
10+
11+
struct InfoCommand: Command {
12+
13+
let name = "Info"
14+
let commands = ["/info"]
15+
let syntax = "/info <name>"
16+
let description = "Shows more information about a specific entry"
17+
let permission = BotPermission.user
18+
19+
func run(update: Update, context: BotContext?) throws {
20+
let chatID = try update.chatID()
21+
let args = try update.args()
22+
guard args.count == 1 else {
23+
try showUsage(chatID)
24+
return
25+
}
26+
let name = args[0]
27+
// Get the settings
28+
let config = try ConfigParser.getConfig()
29+
let entryIndex = config.firstIndex(where: { $0.name.lowercased() == name.lowercased() && $0.chatID == chatID })
30+
guard entryIndex != nil else {
31+
try bot.sendMessage("There is no entry with the name '\(name)'", to: chatID)
32+
return
33+
}
34+
35+
// Show information about the entry
36+
try bot.sendMessage(infoText(for: config[entryIndex!]), to: chatID, parseMode: .markdownV2)
37+
}
38+
39+
private func infoText(for e: URLEntry) -> String {
40+
var lines = ["*\(e.name.escaped()):*"]
41+
lines.append("- URL: \(e.url.escaped())")
42+
if e.area.height != 0 && e.area.width != 0 {
43+
lines.append("- Area: (\(e.area.x), \(e.area.y), \(e.area.width), \(e.area.height))")
44+
}
45+
if e.delay > 0 {
46+
lines.append("- Delay: \(e.delay)")
47+
}
48+
if !e.captureElement.isEmpty {
49+
lines.append("- Capture Element: `\(e.captureElement.escaped())`")
50+
}
51+
if !e.clickElement.isEmpty {
52+
lines.append("- Click Element: `\(e.clickElement.escaped())`")
53+
}
54+
if !e.waitElement.isEmpty {
55+
lines.append("- Wait Element: `\(e.waitElement.escaped())`")
56+
}
57+
return lines.joined(separator: "\n")
58+
}
59+
60+
}

NotifierBot/Sources/Notifier/Commands/ListCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct ListCommand: Command {
1919
func run(update: Update, context: BotContext?) throws {
2020
let chatID = try update.chatID()
2121
let entries = try ConfigParser.getConfig().filter({ $0.chatID == chatID })
22-
let list = JFUtils.entryList(entries, listArea: true, listURLs: false)
22+
let list = JFUtils.entryList(entries)
2323
try bot.sendMessage(list, to: chatID, parseMode: .markdownV2)
2424
}
2525

NotifierBot/Sources/Notifier/Commands/ListURLsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct ListURLsCommand: Command {
1919
func run(update: Update, context: BotContext?) throws {
2020
let chatID = try update.chatID()
2121
let entries = try ConfigParser.getConfig().filter({ $0.chatID == chatID })
22-
let list = JFUtils.entryList(entries, listArea: false, listURLs: true)
22+
let list = JFUtils.entryList(entries, listURLs: true)
2323
try bot.sendMessage(list, to: chatID, parseMode: .markdownV2)
2424
}
2525

NotifierBot/Sources/Notifier/Commands/SetElementCommand.swift renamed to NotifierBot/Sources/Notifier/Commands/SetCaptureElementCommand.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// SetElementCommand.swift
2+
// SetCaptureElementCommand.swift
33
//
44
//
55
// Created by Jonas Frey on 03.06.21.
@@ -8,11 +8,11 @@
88
import Foundation
99
import Telegrammer
1010

11-
struct SetElementCommand: Command {
11+
struct SetCaptureElementCommand: Command {
1212

13-
let name = "Set Element"
14-
let commands = ["/setelement"]
15-
let syntax = "/setelement <name> [html element]"
13+
let name = "Set Capture Element"
14+
let commands = ["/setcaptureelement"]
15+
let syntax = "/setcaptureelement <name> [html element]"
1616
let description = "Specifies which HTML element to capture"
1717
let permission = BotPermission.mod
1818

@@ -32,15 +32,15 @@ struct SetElementCommand: Command {
3232
return
3333
}
3434
// Reset the element property
35-
config[entryIndex!].element = ""
35+
config[entryIndex!].captureElement = ""
3636
if args.count > 1 {
3737
let element = args[1...].joined(separator: " ")
3838
guard !element.contains(",") else {
3939
try bot.sendMessage("Please specify a HTML element that does not contain ','", to: chatID)
4040
return
4141
}
4242
// If there was a new element supplied, set it
43-
config[entryIndex!].element = element
43+
config[entryIndex!].captureElement = element
4444
}
4545
// Save the entry
4646
try ConfigParser.saveConfig(config)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// SetClickElementCommand.swift
3+
//
4+
//
5+
// Created by Jonas Frey on 03.06.21.
6+
//
7+
8+
import Foundation
9+
import Telegrammer
10+
11+
struct SetClickElementCommand: Command {
12+
13+
let name = "Set Click Element"
14+
let commands = ["/setclickelement"]
15+
let syntax = "/setclickelement <name> [html element]"
16+
let description = "Specifies which HTML element to click before taking the screenshot"
17+
let permission = BotPermission.mod
18+
19+
func run(update: Update, context: BotContext?) throws {
20+
let chatID = try update.chatID()
21+
let args = try update.args()
22+
guard args.count >= 1 else {
23+
try showUsage(chatID)
24+
return
25+
}
26+
let name = args[0]
27+
// Get the settings
28+
var config = try ConfigParser.getConfig()
29+
let entryIndex = config.firstIndex(where: { $0.name.lowercased() == name.lowercased() && $0.chatID == chatID })
30+
guard entryIndex != nil else {
31+
try bot.sendMessage("There is no entry with the name '\(name)'", to: chatID)
32+
return
33+
}
34+
// Reset the element property
35+
config[entryIndex!].clickElement = ""
36+
if args.count > 1 {
37+
let element = args[1...].joined(separator: " ")
38+
guard !element.contains(",") else {
39+
try bot.sendMessage("Please specify a HTML element that does not contain ','", to: chatID)
40+
return
41+
}
42+
// If there was a new element supplied, set it
43+
config[entryIndex!].clickElement = element
44+
}
45+
// Save the entry
46+
try ConfigParser.saveConfig(config)
47+
48+
if args.count == 1 {
49+
try bot.sendMessage("Successfully removed click element of \(name)", to: chatID)
50+
} else {
51+
try bot.sendMessage("Successfully updated click element of \(name)", to: chatID)
52+
}
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// SetWaitElementCommand.swift
3+
//
4+
//
5+
// Created by Jonas Frey on 03.06.21.
6+
//
7+
8+
import Foundation
9+
import Telegrammer
10+
11+
struct SetWaitElementCommand: Command {
12+
13+
let name = "Set Wait Element"
14+
let commands = ["/setwaitelement"]
15+
let syntax = "/setwaitelement <name> [html element]"
16+
let description = "Specifies which HTML element to wait for before taking the screenshot"
17+
let permission = BotPermission.mod
18+
19+
func run(update: Update, context: BotContext?) throws {
20+
let chatID = try update.chatID()
21+
let args = try update.args()
22+
guard args.count >= 1 else {
23+
try showUsage(chatID)
24+
return
25+
}
26+
let name = args[0]
27+
// Get the settings
28+
var config = try ConfigParser.getConfig()
29+
let entryIndex = config.firstIndex(where: { $0.name.lowercased() == name.lowercased() && $0.chatID == chatID })
30+
guard entryIndex != nil else {
31+
try bot.sendMessage("There is no entry with the name '\(name)'", to: chatID)
32+
return
33+
}
34+
// Reset the element property
35+
config[entryIndex!].waitElement = ""
36+
if args.count > 1 {
37+
let element = args[1...].joined(separator: " ")
38+
guard !element.contains(",") else {
39+
try bot.sendMessage("Please specify a HTML element that does not contain ','", to: chatID)
40+
return
41+
}
42+
// If there was a new element supplied, set it
43+
config[entryIndex!].waitElement = element
44+
}
45+
// Save the entry
46+
try ConfigParser.saveConfig(config)
47+
48+
if args.count == 1 {
49+
try bot.sendMessage("Successfully removed wait element of \(name)", to: chatID)
50+
} else {
51+
try bot.sendMessage("Successfully updated wait element of \(name)", to: chatID)
52+
}
53+
}
54+
}

NotifierBot/Sources/Notifier/ConfigParser.swift

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,36 @@ class ConfigParser {
3030
guard components.count >= 9 else {
3131
throw JFBotError.malformedLineSegments(line)
3232
}
33-
let name = components[0].trimmingCharacters(in: .whitespaces)
34-
let x = Int(components[1].trimmingCharacters(in: .whitespaces))
35-
let y = Int(components[2].trimmingCharacters(in: .whitespaces))
36-
let width = Int(components[3].trimmingCharacters(in: .whitespaces))
37-
let height = Int(components[4].trimmingCharacters(in: .whitespaces))
38-
let delay = Int(components[5].trimmingCharacters(in: .whitespaces))
39-
let element = components[6].trimmingCharacters(in: .whitespaces)
40-
let chatID = Int64(components[7].trimmingCharacters(in: .whitespaces))
41-
let url = components[8...].joined(separator: ",").trimmingCharacters(in: .whitespaces)
33+
var args = components
34+
35+
func nextArg() -> String { args.removeFirst().trimmingCharacters(in: .whitespaces) }
36+
37+
let name = nextArg()
38+
let x = Int(nextArg())
39+
let y = Int(nextArg())
40+
let width = Int(nextArg())
41+
let height = Int(nextArg())
42+
let delay = Int(nextArg())
43+
let captureElement = nextArg()
44+
let clickElement = nextArg()
45+
let waitElement = nextArg()
46+
let chatID = Int64(nextArg())
47+
// URL is the rest
48+
let url = args.joined(separator: ",").trimmingCharacters(in: .whitespaces)
4249

4350
guard x != nil && y != nil && width != nil && height != nil && chatID != nil, delay != nil else {
4451
throw JFBotError.malformedIntegers(line)
4552
}
4653

47-
entries.append(URLEntry(name: name, url: url, area: Rectangle(x: x!, y: y!, width: width!, height: height!), chatID: chatID!, delay: delay!, element: element))
54+
entries.append(URLEntry(
55+
name: name,
56+
url: url,
57+
area: Rectangle(x: x!, y: y!, width: width!, height: height!),
58+
chatID: chatID!,
59+
delay: delay!,
60+
captureElement: captureElement,
61+
clickElement: clickElement,
62+
waitElement: waitElement))
4863
}
4964

5065
return entries
@@ -54,7 +69,7 @@ class ConfigParser {
5469
static func saveConfig(_ config: Config) throws {
5570
var configString = ""
5671
for l in config {
57-
configString += "\(l.name),\(l.area.x),\(l.area.y),\(l.area.width),\(l.area.height),\(l.delay),\(l.element),\(l.chatID),\(l.url)\n"
72+
configString += "\(l.name),\(l.area.x),\(l.area.y),\(l.area.width),\(l.area.height),\(l.delay),\(l.captureElement),\(l.clickElement),\(l.waitElement),\(l.chatID),\(l.url)\n"
5873
}
5974
// Remove the trailing line break
6075
configString.removeLast()
@@ -111,7 +126,9 @@ struct URLEntry {
111126
var area: Rectangle
112127
var chatID: Int64
113128
var delay: Int = 0
114-
var element: String = ""
129+
var captureElement: String = ""
130+
var clickElement: String = ""
131+
var waitElement: String = ""
115132

116133
}
117134

NotifierBot/Sources/Notifier/JFUtils.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ struct JFUtils {
5858
shell(command)
5959
}
6060

61-
static func entryList(_ entries: [URLEntry], listArea: Bool, listURLs: Bool, listAll: Bool = false) -> String {
62-
var list = "*Monitored Websites:*\n"
61+
static func entryList(_ entries: [URLEntry], listURLs: Bool = false) -> String {
62+
var list = ["*Monitored Websites:*"]
6363
if entries.isEmpty {
64-
list += "_None_"
65-
} else if listArea && listURLs {
66-
list += entries.map({ "- \($0.name) (Delay: \($0.delay), Offset: \($0.area.x)/\($0.area.y), Size: \($0.area.width)x\($0.area.height)\($0.element != "" ? ", Element: '\($0.element)'" : "")\n \($0.url)" }).joined(separator: "\n").escaped()
67-
} else if listURLs {
68-
list += entries.map({ "- \($0.name): \($0.url)" }).joined(separator: "\n").escaped()
69-
} else if listArea {
70-
list += entries.map({ "- \($0.name) (Delay: \($0.delay), Offset: \($0.area.x)/\($0.area.y), Size: \($0.area.width)x\($0.area.height)\($0.element != "" ? ", Element: '\($0.element)'" : "")" }).joined(separator: "\n").escaped()
64+
list.append("_None_")
65+
} else if listURLs {§§
66+
for entry in entries {
67+
list.append("- \(entry.name): \(entry.url)".escaped())
68+
}
7169
} else {
72-
list += entries.map({ "- \($0.name)"}).joined(separator: "\n").escaped()
70+
for entry in entries {
71+
list.append("- \(entry.name.escaped())")
72+
}
7373
}
74-
return list
74+
return list.joined(separator: "\n")
7575
}
7676

7777
}

0 commit comments

Comments
 (0)