-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.clj
144 lines (121 loc) · 4.81 KB
/
handlers.clj
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
(ns lemme-know-bot.handlers
"Functions that take action on various message text contents."
(:gen-class)
(:require [clojure.string :as string]
[lemme-know-bot.config :as cfg]
[lemme-know-bot.fields :as fields]
[lemme-know-bot.notify :as notify]
[taoensso.timbre :as log]
[telegrambot-lib.core :as tbot]))
(defn send-msg
"Send a message to the chat."
[bot chat-id text]
(try
(tbot/send-message bot chat-id text)
(catch Exception e
(log/error "tbot/send-message exception:" e))))
(defn help
"Send info about this bot and available commands."
[bot msg]
(log/debug "/help or /start command received.")
(let [chat-id (fields/chat-id msg)]
(send-msg bot chat-id
(str "-- Lemme-Know-Bot: Help/Getting Started --\n"
"This bot can watch the current chat for keywords/phrases "
"and mention you when one has been seen.\n\n"
"The following commands are supported:\n"
"/add keyword - add a new keyword to watch for in this chat.\n"
"/list - list all the watched keywords.\n"
"/remove keyword - remove a watched keyword.\n"
"/help - show this help message with available commands.\n"
"/settings - show the bot configuration."))))
(defn settings
"Implements the /settings command."
[bot msg]
(log/debug "/settings command received.")
(let [chat-id (fields/chat-id msg)
cfg-timeout (:timeout cfg/config)
cfg-sleep (:sleep cfg/config)]
(send-msg bot chat-id
(str "-- Lemme-Know-Bot: Settings --\n"
"Timeout (secs) to wait during a long poll: " cfg-timeout "\n"
"Sleep (ms) between polls: " cfg-sleep))))
(defn list-search
"List the current searches setup for the user."
[bot msg]
(log/debug "/list command received.")
(let [chat-id (fields/chat-id msg)
username (fields/from-user msg)]
(send-msg bot chat-id
(str username ", your searches are: "
(into [] (notify/list-searches chat-id username))))))
(defn extract-keyword
"Extract only the keyword(s) from a text message.
Convert to lowercase to avoid case sensitive searches."
[msg]
(-> msg
(fields/text)
(string/replace-first #"/(\w+)@*(\w+)" "")
(string/trim)
(string/lower-case)))
(comment
(extract-keyword {:message {:text "/add"}}) ; no text
(extract-keyword {:message {:text "/add "}}) ; spaces only
(extract-keyword {:message {:text "/add hello"}}) ; text
(extract-keyword {:message {:text "/add hello there"}}) ; two words
)
(defn add-search
"Add a new search for the user."
[bot msg]
(log/debug "/add command received.")
(let [add-text (extract-keyword msg)
chat-id (fields/chat-id msg)
username (fields/from-user msg)]
(if (not (string/blank? add-text))
;; not blank - add the text and message back
(do
(notify/add-search! chat-id username add-text)
(notify/clean-searches!)
(send-msg bot chat-id
(str username ", search added for: '" add-text "'")))
;; blank text - do not add
(send-msg bot chat-id
(str username ", search NOT added. "
"Passed parameter text was blank.\n"
"Try sending something like: /add cheeseburger")))))
(defn remove-search
"Remove a search for the user."
[bot msg]
(log/debug "/remove command received.")
(let [rm-text (extract-keyword msg)
chat-id (fields/chat-id msg)
username (fields/from-user msg)]
(if (not (string/blank? rm-text))
;; not blank - remove and message back
(do
(notify/remove-search! chat-id username rm-text)
(send-msg bot chat-id
(str username ", search removed for: '" rm-text "'")))
;; blank text - do not remove
(send-msg bot chat-id
(str username ", search NOT removed. "
"Passed parameter text was blank.\n"
"Try sending someting like: /remove cheeseburger")))))
(defn search
"Search for a match in the text.
Mention the user if found."
[bot msg]
(let [msg-text (string/lower-case (fields/text msg))
chat-id (fields/chat-id msg)
chat-entries (filter #(= (str chat-id)
(:chat-id %))
@notify/searches)]
(log/debug "searching for keywords in:" msg-text)
(doseq [entry chat-entries]
(when (string/includes? msg-text (:text entry))
(send-msg bot chat-id
(str "@" (:user entry)
" search string '"
(:text entry)
"' was found in message:\n"
"'" msg-text "'"))))))