slack-ruby / slack-ruby-bot

The easiest way to write a Slack bot in Ruby.
MIT License
1.12k stars 187 forks source link

When a command is posted in thread, its again posted as direct message automatically #274

Open ReshmaUmapathy opened 3 years ago

ReshmaUmapathy commented 3 years ago

Hi Team, Issue: when i post a command as a thread message, its automatically getting reposted as a direct message also...

payload sent from slack after posting the command - {"type":"message","subtype":"message_replied","hidden":true,"message":{"client_msg_id":"","type":"message","text":"<> create ticket","user":"","ts":"1607538628.001600","team":"","blocks":[{"type":"rich_text","block_id":"0cSo","elements":[{"type":"rich_text_section","elements":[{"type":"user","user_id":""},{"type":"text","text":" create ticket"}]}]}],"thread_ts":"1607538628.001600","reply_count":1,"reply_users_count":1,"latest_reply":"1607538692.001700","reply_users":[""]},"channel":"","event_ts":"1607538692.001800","ts":"1607538692.001800"} payload pushed to slack after performing few actions - {"type":"message","id":1,"text":"Ticket created - https://abcd.abcd.com","channel":"","thread_ts":"1607626974.008800"} ps: i've removed the user id, client id and channel id..

What i've done -> using conversations.history i've retrieved the text of the parent message. And performing some actions and again posting the result of that action to that channel as a reply thread. Here only problem am facing is, when i post a command as thread message in slack channel, its automatically getting posted as a direct message also... Kindly help me in resolving this.

image image

Thanks, Reshma.

dblock commented 3 years ago

This is really a Slack API question, but looking at chat_postMessage it looks like this is controlled by the value of broadcast? Try setting it to false?

https://api.slack.com/methods/chat.postMessage#arg_reply_broadcast

ReshmaUmapathy commented 3 years ago

Hi, NO, not using chat_postMessage... using client.say method to post it in slack channel. Issue is not happening when message is sent to slack channel from my app... facing this issue only when a user post a command in slack channel... immediately it is posted as direct message in the channel...

My code:

require "uri"
require "net/http"
module SlackMathbot
  module Commands
    class Calculate < SlackRubyBot::Commands::Base
      command 'Create ticket' do |client, data, _match|
        url = URI("https://slack.com/api/conversations.history?channel=#{data.channel}&latest=#{data.thread_ts}&inclusive=true&limit=1")
        https = Net::HTTP.new(url.host, url.port);
        https.use_ssl = true
        request = Net::HTTP::Get.new(url)
        request["Authorization"] = ""
        response1 = https.request(request)
        resp1 = response1.read_body
        puts resp1
        ui_name = resp1.split("Transcation Name")[1].split("\\n")[0].split(" ")[2]
        error_class = resp1.split("Error class")[1].split("\\n")[0].split(" ")[2]
        error_path = (resp1.split("Path")[1].split("\\n")[0].split(" ")[2]).delete('\\')
        title = "#{ui_name} #{error_class} - #{error_path}"

        url = URI("https://freshworks.freshrelease.com")
        https = Net::HTTP.new(url.host, url.port);
        https.use_ssl = true
        request = Net::HTTP::Post.new(url)
        request["Authorization"] = "Token token="
        request["Content-Type"] = "application/json"
        request.body = "{\"title\":\"#{title}\",\"issue_type_id\":,\"project_id\":12111,\"tags\": [\"test\"]}"
        response = https.request(request)
        resp = response.read_body
        # get_key(resp,data)
        key = resp.split("key")[1].split(",")[0].split(/\s*-\s*/)[1].split(/\"/)[0]
        fr_url = "https://freshworks.freshrelease.com"
        client.say(channel: data.channel,thread_ts: data.event_ts, text: "Ticket created - #{fr_url}")
      end
    end
  end
end
ReshmaUmapathy commented 3 years ago

This is the response i got when i posted a command in slack channel: {"ok":true,"channel":"channel_id","ts":"1607755792.000700","message":{"client_msg_id":"client_id","type":"message","text":"<@user_id> create ticket","user":"user_id","ts":"1607755792.000700","team":"team_id","blocks":[{"type":"rich_text","block_id":"tDr","elements":[{"type":"rich_text_section","elements":[{"type":"user","user_id":"user_id"},{"type":"text","text":" create ticket"}]}]}],"thread_ts":"1604574561.000500"}}

Here, thread_ts and ts are different... is it because of that? If so, how to solve it... where we need to change...

dblock commented 3 years ago

This is really a Slack API question, I don't know more than the documentation here. The say implementation is a dumb wrapper.

I would switch to chat_postMessage and call it a day.

Please note that real-time bots are not even accepted in the slack directory any more, you should be using a bot with granular permissions. read this