text-message in command handler

to and from props in context in suggestions function
ability to send request using text-message property in handler and via status.sendMessage
show request's text if it exists
This commit is contained in:
Roman Volosovskyi 2017-06-06 15:13:08 +03:00 committed by Roman Volosovskyi
parent 33d7146b25
commit 4f6ebb53d3
6 changed files with 103 additions and 27 deletions

View File

@ -71,10 +71,9 @@ function demoSuggestions(params, context) {
});
return {markup: view};
};
}
status.addListener("on-message-input-change", demoSuggestions);
status.addListener("init", demoSuggestions);
status.addListener("on-message-send", function (params, context) {
var cnt = localStorage.getItem("cnt");
if (!cnt) {
@ -107,3 +106,41 @@ status.addListener("on-message-send", function (params, context) {
}
});
});
status.command({
name: "init-request",
description: "send-request",
color: "#a187d5",
sequentialParams: true,
params: [{
name: "address",
type: status.types.TEXT,
placeholder: "address"
}],
handler: function (params) {
return {
"text-message": {
type: "request",
content: {
command: "response",
params: {first: "123"},
text: "That's request's content! It works!"
}
}
};
}
});
status.response({
name: "response",
color: "#a187d5",
sequentialParams: true,
params: [{
name: "first",
type: status.types.TEXT,
placeholder: "first"
}],
handler: function (params) {
return {"text-message": "ok"};
}
});

View File

@ -242,8 +242,11 @@ var status = {
updateDb: function (db) {
addContext("update-db", db)
},
sendMessage: function (text) {
statusSignals.sendMessage(text);
sendMessage: function (message) {
if(typeof message !== "string") {
message = JSON.stringify(message);
}
statusSignals.sendMessage(message);
},
addLogMessage: function (type, message) {
var message = {

View File

@ -115,7 +115,8 @@
(handlers/register-handler
:load-chat-parameter-box
(handlers/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ {:keys [name type bot] :as command}]]
(fn [{:keys [current-chat-id current-account-id] :as db}
[_ {:keys [name type bot] :as command}]]
(let [parameter-index (input-model/argument-position db current-chat-id)]
(when (and command (> parameter-index -1))
(let [jail-id (or bot current-chat-id)
@ -128,8 +129,12 @@
args (-> (get-in db [:chats current-chat-id :input-text])
(input-model/split-command-args)
(rest))
to (get-in db [:contacts current-chat-id :address])
from (get-in db [])
params {:parameters {:args args}
:context (merge {:data data}
:context (merge {:data data
:from current-account-id
:to to}
(input-model/command-dependent-context-params command))}]
(status/call-jail
{:jail-id jail-id

View File

@ -70,16 +70,17 @@
(register-handler :prepare-command!
(u/side-effect!
(fn [{:keys [current-public-key network-status] :as db}
[_ add-to-chat-id {:keys [chat-id command-message command handler-data] :as params}]]
[_ add-to-chat-id {:keys [chat-id command-message command] :as params}]]
(let [clock-value (messages/get-last-clock-value chat-id)
request (:request (:handler-data command))
handler-data (:handler-data command)
hidden-params (->> (:params (:command command))
(filter #(= (:hidden %) true))
(map :name))
command' (->> (assoc command-message :handler-data handler-data)
(prepare-command current-public-key chat-id clock-value request)
(cu/check-author-direction db chat-id))]
(log/debug "Handler data: " request handler-data (dissoc params :commands :command-message))
(log/debug "Handler data: " request (:handler-data command) (dissoc params :commands :command-message))
(dispatch [:update-message-overhead! chat-id network-status])
(dispatch [:set-chat-ui-props {:sending-in-progress? false}])
(dispatch [::send-command! add-to-chat-id (assoc params :command command') hidden-params])
@ -238,17 +239,29 @@
:from chat-id
:to "me"}]))))))
(register-handler :send-message-from-jail
(u/side-effect!
(fn [_ [_ {:keys [chat_id message]}]]
(defn handle-message-from-bot [{:keys [message chat-id]}]
(cond
(string? message)
(dispatch [:received-message
{:message-id (random/id)
:content (str message)
:content-type text-content-type
:outgoing false
:chat-id chat_id
:from chat_id
:to "me"}]))))
:chat-id chat-id
:from chat-id
:to "me"}])
(= "request" (:type message))
(dispatch [:add-request-message!
{:content (:content message)
:chat-id chat-id}])))
(register-handler :send-message-from-jail
(u/side-effect!
(fn [_ [_ {:keys [chat_id message]}]]
(let [parsed-message (types/json->clj message)]
(handle-message-from-bot {:message parsed-message
:chat-id chat_id})))))
(register-handler :show-suggestions-from-jail
(u/side-effect!
@ -301,7 +314,10 @@
current-account-id accounts contacts] :as db}
[_ {:keys [chat-id command]}]]
(log/debug "sending command: " command)
(when (not (get-in contacts [chat-id :dapp?]))
(if (get-in contacts [chat-id :dapp?])
(when-let [text-message (get-in command [:content :handler-data :text-message])]
(handle-message-from-bot {:message text-message
:chat-id chat-id}))
(let [{:keys [public-key private-key]} (chats chat-id)
{:keys [group-chat public?]} (get-in db [:chats chat-id])
@ -332,3 +348,15 @@
:else
(protocol/send-message! (assoc-in options
[:message :to] chat-id))))))))
(register-handler :add-request-message!
(u/side-effect!
(fn [_ [_ {:keys [content chat-id]}]]
(dispatch [:received-message
{:message-id (random/id)
:content (assoc content :bot chat-id)
:content-type content-type-command-request
:outgoing false
:chat-id chat-id
:from chat-id
:to "me"}]))))

View File

@ -78,6 +78,7 @@
(fn [{:keys [message-id content from incoming-group]}]
(let [commands @commands-atom
params (:params content)
text-content (:text content)
{:keys [command content]} (parse-command-request commands content)
command (if (and params command)
(merge command {:prefill (vals params)})
@ -92,7 +93,7 @@
[view @markup]
[text {:style st/style-message-text
:font :default}
(or @markup content)])]]
(or text-content @markup content)])]]
(when (:request-text command)
[view st/command-request-text-view
[text {:style st/style-sub-text

View File

@ -13,4 +13,6 @@
(defn json->clj [json]
(when-not (= json "undefined")
(js->clj (.parse js/JSON json) :keywordize-keys true)))
(try
(js->clj (.parse js/JSON json) :keywordize-keys true)
(catch js/Error _ (when (string? json) json)))))