class Tadabot::Bot
Public Class Methods
new(team_uid, bot_token)
click to toggle source
# File lib/tadabot.rb, line 7 def initialize(team_uid, bot_token) @team_uid = team_uid @bot_token = bot_token end
Public Instance Methods
bot_name()
click to toggle source
# File lib/tadabot.rb, line 102 def bot_name _res = HTTParty.get("https://web.tada.team/api/v4/teams/#{@team_uid}/contacts/", :headers => { "Authorization" => "Bearer " + @bot_token }); _res["result"].each do |contact| if contact["short_name"] == "\u0412\u044B" || contact["short_name"] == "You" return contact["display_name"] end end end
init()
click to toggle source
# File lib/tadabot.rb, line 12 def init bn = bot_name EM.run { @ws = Faye::WebSocket::Client.new("wss://web.tada.team/messaging/" + @team_uid, [], { :headers => { "token" => @bot_token }, :ping => 1 } ) @ws.on :open do |event| p [:open, "Bot @#{bn} Start", Time.new.strftime("%I:%M %p")] end @ws.on :message do |event| response = JSON.parse(event.data) if response.key?('event') && response["event"] == "server.message.updated" p "messages" if response.key?('confirm_id') p "confirm", response["confirm_id"] read_by_bot = { "event": "client.confirm", "params": { "confirm_id": response["confirm_id"] } } @ws.send(JSON(read_by_bot)) end if response.fetch("params").fetch("messages")[0].fetch("markup")[0].key?("repl") && response.fetch("params").fetch("messages")[0].fetch("markup")[0].fetch("repl") == "@" + bn p "BOT ANSWER" p "______________________________________" _reply_to = response["params"]["messages"][0]["from"] _chat_uid = response["params"]["messages"][0]["chat"] _res = HTTParty.get("https://web.tada.team/api/v4/teams/#{@team_uid}/contacts/" + _reply_to, :headers => { "Authorization" => "Bearer " + @bot_token }); markdown = "[@#{_res.parsed_response["result"]["display_name"]}](tadateam://#{_res.parsed_response["result"]["jid"]})" hash = { "event": "client.message.updated", "params": { "to": _chat_uid, "content": { "text":"#{markdown} REPLY_FROM_BOT @#{bn}", "type":"plain" } } } @ws.send(JSON(hash)) end else p "listen" end end @ws.on :error do |event| p [:error, Time.new.strftime("%I:%M %p")] end @ws.on :close do |event| p [:close, event.code, event.reason, Time.new.strftime("%I:%M %p")] @ws = nil init end } end
send_message(chat_uid, text)
click to toggle source
# File lib/tadabot.rb, line 88 def send_message(chat_uid, text) hash = { "event": "client.message.updated", "params": { "to": chat_uid, "content": { "text":"#{markdown} MESSAGE_FROM @#{bn} \n #{text}", "type":"plain" } } } @ws.send(JSON(hash)) end