Class: StoatWebhooks::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/stoatrb/webhooks.rb

Constant Summary collapse

DEFAULT_API_URL =
'https://stoat.chat/api'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(webhook_id, webhook_token, api_url: DEFAULT_API_URL) ⇒ Webhook

Returns a new instance of Webhook.



68
69
70
71
72
73
# File 'lib/stoatrb/webhooks.rb', line 68

def initialize(webhook_id, webhook_token, api_url: DEFAULT_API_URL)
	@webhook_id = webhook_id
	@webhook_token = webhook_token
	@api_url = api_url
	@uri = URI("#{@api_url}/webhooks/#{@webhook_id}/#{@webhook_token}")
end

Instance Method Details

#send_message(content:, embeds: nil) ⇒ Object



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
# File 'lib/stoatrb/webhooks.rb', line 75

def send_message(content:, embeds: nil)
	payload = {
		content: content
	}

	if embeds
		payload[:embeds] = Array(embeds)
	end

	http = Net::HTTP.new(@uri.host, @uri.port)
	http.use_ssl = true
	request = Net::HTTP::Post.new(@uri.path, 'Content-Type' => 'application/json')
	request.body = payload.to_json

	begin
		response = http.request(request)
		if response.code.to_i == 204 || response.code.to_i == 200
			return true
		else
			puts "[Stoatrb Webhooks] AN ERROR HAS OCCURED: Status code: #{response.code}"
			puts "[Stoatrb Webhooks] Response body: #{response.body}"
			return false
		end
	rescue StandardError => e
		puts "[Stoatrb Webhooks] AN ERROR HAS OCCURED: #{e.message}"
		return false
	end
end