Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Call Telegram API
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Feb 8, 2023
1 parent b68e411 commit b00461b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source "https://rubygems.org"
gem "async-process"
gem "aws-sdk-s3"
gem "falcon"
gem "faraday"
gem "memery"
gem "ox"
gem "sentry-ruby"
gem "sinatra"
Expand All @@ -15,6 +17,7 @@ group :development, :test do
gem "rspec"
gem "simplecov"
gem "timecop"
gem "webmock"
end

group :development do
Expand Down
19 changes: 19 additions & 0 deletions base/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
async (2.3.1)
console (~> 1.10)
Expand Down Expand Up @@ -50,6 +52,8 @@ GEM
concurrent-ruby (1.2.0)
console (1.16.2)
fiber-local
crack (0.4.5)
rexml
diff-lcs (1.5.0)
docile (1.4.0)
falcon (0.42.3)
Expand All @@ -65,12 +69,19 @@ GEM
process-metrics (~> 0.2.0)
protocol-rack (~> 0.1)
samovar (~> 2.1)
faraday (2.7.4)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
fiber-local (1.0.0)
hashdiff (1.0.1)
io-event (1.1.6)
jmespath (1.6.2)
json (2.6.3)
localhost (1.1.9)
mapping (1.1.1)
memery (1.4.1)
ruby2_keywords (~> 0.0.2)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
openssl (3.1.0)
Expand All @@ -91,6 +102,7 @@ GEM
protocol-rack (0.2.4)
protocol-http (~> 0.23)
rack (>= 1.0)
public_suffix (5.0.1)
rack (2.2.6.2)
rack-protection (3.0.5)
rack
Expand Down Expand Up @@ -157,6 +169,10 @@ GEM
timers (4.3.5)
traces (0.8.0)
unicode-display_width (2.4.2)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.6.6)

PLATFORMS
Expand All @@ -167,6 +183,8 @@ DEPENDENCIES
async-rspec
aws-sdk-s3
falcon
faraday
memery
ox
rspec
rubocop
Expand All @@ -176,6 +194,7 @@ DEPENDENCIES
simplecov
sinatra
timecop
webmock
zeitwerk

BUNDLED WITH
Expand Down
2 changes: 2 additions & 0 deletions base/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

require "async/process"
require "aws-sdk-s3"
require "faraday"
require "memery"

loader = Zeitwerk::Loader.for_gem
loader.setup
Expand Down
9 changes: 9 additions & 0 deletions notifier/function/lib/function/handler.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# frozen_string_literal: true

class Function::Handler
TELEGRAM_TOKEN = ENV.fetch("TELEGRAM_TOKEN", "")

TELEGRAM_CHATS = ENV.fetch("TELEGRAM_CHATS", "")
.split(",")
.map(&:strip)

TELEGRAM = Function::Telegram.new(TELEGRAM_TOKEN, TELEGRAM_CHATS)

def initialize(env)
@env = env
end

def call
pp(@env.to_json)
@env.to_json
end
end
23 changes: 23 additions & 0 deletions notifier/function/lib/function/telegram.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

class Function::Telegram
include Memery

def initialize(token, chats)
@token = token
@chats = chats
end

def notify(text)
@chats.map do |chat_id|
Async { connection.post("/bot#{@token}/sendMessage", chat_id:, text:, parse_mode: "Markdown") }
end.map(&:wait)
end

memoize def connection
Faraday.new("https://api.telegram.org") do |f|
f.request :json
f.response :raise_error
end
end
end
20 changes: 20 additions & 0 deletions notifier/function/spec/function/telegram_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

RSpec.describe Function::Telegram do
let(:telegram) { described_class.new("token", ["123"]) }

describe "#notify" do
subject { telegram.notify("text") }

it "send a request to Telegram API" do
stub = stub_request(:post, "https://api.telegram.org/bottoken/sendMessage")
.with(
body: "{\"chat_id\":\"123\",\"text\":\"text\",\"parse_mode\":\"Markdown\"}"
)
.to_return(status: 200, body: "", headers: {})

subject
expect(stub).to have_been_requested
end
end
end
1 change: 1 addition & 0 deletions notifier/function/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ENV["AUTH_TOKEN"] ||= "token"

require "async/rspec"
require "webmock/rspec"
require "simplecov"

SimpleCov.start do
Expand Down

0 comments on commit b00461b

Please sign in to comment.