Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 1147969

Browse files
committed
Merge branch 'master' of github.com:R-unic/cosmo
2 parents add1055 + 53b97fe commit 1147969

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/cosmo/runtime/interpreter.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Cosmo::Interpreter
5454
importable("system", Intrinsic::SystemLib.new(self))
5555
importable("file", Intrinsic::FileLib.new(self))
5656
importable("socket", Intrinsic::SocketLib.new(self))
57+
importable("webhook", Intrinsic::WebhookLib.new(self))
5758
end
5859

5960
private def importable(name : String, library : Intrinsic::Lib)

src/cosmo/runtime/intrinsic.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ require "./intrinsic/lib/json"
5353
require "./intrinsic/lib/system"
5454
require "./intrinsic/lib/socket"
5555
require "./intrinsic/lib/file"
56-
56+
require "./intrinsic/lib/webhook"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "http/client"
2+
require "uri"
3+
require "json"
4+
5+
module Cosmo
6+
module Intrinsic
7+
class WebhookLib < Lib
8+
def inject : Nil
9+
discord_webhook = {} of String => IFunction
10+
discord_webhook["send"] = Send.new(@i)
11+
12+
@i.declare_intrinsic("string->Function", "DiscordWebhook", discord_webhook)
13+
end
14+
15+
class Send < IFunction
16+
def arity : Range(UInt32, UInt32)
17+
2.to_u .. 2.to_u
18+
end
19+
20+
def call(args : Array(ValueType)) : String
21+
webhook_url = args[0].to_s
22+
payload = { "content" => args[1].to_s }
23+
24+
headers = HTTP::Headers{"Content-Type" => "application/json"}
25+
response = HTTP::Client.post(url: webhook_url, form: payload, headers: headers, tls: true)
26+
27+
if response.status_code == 301
28+
new_location = response.headers["Location"]
29+
return call([new_location, args[1]])
30+
elsif response.status_code == 204
31+
return "Message sent successfully!"
32+
else
33+
Logger.report_error("Failed to send webhook message", "Status code #{response.status_code}", token("Webhook->send"))
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)