Class: SlackProcessor

Inherits:
Processor show all
Defined in:
lib/Processors/SlackProcessor.rb

Defined Under Namespace

Classes: Payload

Instance Attribute Summary collapse

Attributes inherited from Processor

#baseExecutePath, #config, #configFilePath

Instance Method Summary collapse

Constructor Details

#initialize(config, configFilePath, baseExecutePath) ⇒ SlackProcessor

Returns a new instance of SlackProcessor.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/Processors/SlackProcessor.rb', line 15

def initialize(config, configFilePath, baseExecutePath)
    @config = config
    @configFilePath = configFilePath
    @baseExecutePath = baseExecutePath
    @logger = ZLogger.new(baseExecutePath)

    @botToken = config["slackBotToken"]
    @inCommingWebHookURL = config["slackInCommingWebHookURL"]
    @targetChannel = config["slackBotTargetChannel"]
    @timeZoneOffset = Helper.unwrapRequiredParameter(config, "slackTimeZoneOffset")
    @attachmentGroupByNumber = 1

    if !config['slackAttachmentGroupByNumber'].nil? && config['slackAttachmentGroupByNumber'] != "" && config['slackAttachmentGroupByNumber'].to_i > 0  && config['slackAttachmentGroupByNumber'].to_i < 100
        @attachmentGroupByNumber = config['slackAttachmentGroupByNumber'].to_i
    end

    if (botToken.nil? && inCommingWebHookURL.nil?) || (botToken == "" && inCommingWebHookURL == "")
        raise "must specify slackBotToken or slackInCommingWebHookURL in SlackProcessor."
    elsif !botToken.nil? && botToken != "" && (targetChannel.nil? || targetChannel == "")
        raise "must specify slackBotTargetChannel in SlackProcessor."
    end

    puts "[SlackProcessor] Init Success."
end

Instance Attribute Details

#attachmentGroupByNumberObject

Returns the value of attribute attachmentGroupByNumber.



13
14
15
# File 'lib/Processors/SlackProcessor.rb', line 13

def attachmentGroupByNumber
  @attachmentGroupByNumber
end

#botTokenObject

Returns the value of attribute botToken.



13
14
15
# File 'lib/Processors/SlackProcessor.rb', line 13

def botToken
  @botToken
end

#inCommingWebHookURLObject

Returns the value of attribute inCommingWebHookURL.



13
14
15
# File 'lib/Processors/SlackProcessor.rb', line 13

def inCommingWebHookURL
  @inCommingWebHookURL
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/Processors/SlackProcessor.rb', line 13

def logger
  @logger
end

#targetChannelObject

Returns the value of attribute targetChannel.



13
14
15
# File 'lib/Processors/SlackProcessor.rb', line 13

def targetChannel
  @targetChannel
end

#timeZoneOffsetObject

Returns the value of attribute timeZoneOffset.



13
14
15
# File 'lib/Processors/SlackProcessor.rb', line 13

def timeZoneOffset
  @timeZoneOffset
end

Instance Method Details

#processReviews(reviews, platform) ⇒ Object



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
# File 'lib/Processors/SlackProcessor.rb', line 40

def processReviews(reviews, platform)
    if reviews.length < 1
        return reviews
    end

    pendingPayloads = []

    # Slack Message Limit: posting one message per second per channel
    reviews.each_slice(attachmentGroupByNumber) do |reviewGroup|
        payload = Payload.new()
        payload.attachments = []

        reviewGroup.each do |review|
            attachment = Payload::Attachment.new()

            stars = "" * review.rating + "" * (5 - review.rating)
            color = review.rating >= 4 ? "good" : (review.rating > 2 ? "warning" : "danger")
            date = Time.at(review.createdDateTimestamp).getlocal(timeZoneOffset)
            
            title = "#{stars}"
            if !review.title.nil?
                title = "#{review.title} - #{stars}"
            end
                
            attachment.color = color
            attachment.author_name = review.userName
            attachment.fallback = title
            attachment.title = title
            attachment.text = review.body
            attachment.footer = "#{platform} - #{review.platform} - #{review.appVersion} - #{review.territory} - <#{review.url}|#{date}>"
            
            payload.attachments.append(attachment)
        end

        pendingPayloads.append(payload)
    end

    loop do
        payload = pendingPayloads.shift
        
        result = request(payload)
        if !result[:ok]
            logger.logError(payload)
            logger.logError(result)
            if result[:message] == "ratelimited"
                puts "[SlackProcessor] Reached Rate Limited, sleep 1 sec..."
                sleep(1)
                pendingPayloads.insert(0, payload)
            end
        end

        puts "[SlackProcessor] Send new Review messages, rest: #{pendingPayloads.length}"
        break if pendingPayloads.length < 1
    end

    return reviews
end

#sendWelcomMessage(platform) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/Processors/SlackProcessor.rb', line 98

def sendWelcomMessage(platform)
    payload = Payload.new()
    payload.attachments = []

    attachment = Payload::Attachment.new()
        
    title = "ZReviewTender Standing By :astronaut:"
    body = "#{platform} Init Success!, will resend App Review to this channel automatically."
    
    attachment.color = "good"
    attachment.author_name = "ZhgChgLi"
    attachment.fallback = title
    attachment.title = title
    attachment.text = body
    attachment.footer = "<https://github.com/ZhgChgLi/ZReviewTender|ZReviewTender>, <https://github.com/ZhgChgLi/ZReviewTender/issues|Report an issue>, Powered by <https://zhgchg.li|ZhgChgLi>."
    
    payload.attachments.append(attachment)

    puts "[SlackProcessor] sendWelcomMessage(#{title})"
    request(payload)
end