pechkin 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/pechkin +2 -0
- data/lib/pechkin.rb +17 -0
- data/lib/pechkin/api.rb +67 -0
- data/lib/pechkin/cli.rb +70 -0
- data/lib/pechkin/config.rb +10 -0
- data/lib/pechkin/telegram.rb +40 -0
- data/lib/pechkin/version.rb +11 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2fd4c9f29fc1cdab78ad9ed5d17bc91d639aaf3a07f1c8f1b2a5bd05ab5caed9
|
4
|
+
data.tar.gz: 14355bfc12a1115b9fb7d233f91c974fd6d0ac698a344668dc24a9e6d7b83306
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca47776e42575d2c4bd78a106f5e83de71f70f373de6585937b9d02329e95fb9795e8375a8582b5795ee74a328363fc0abcf44205ada5a8b85bd02ca17866d45
|
7
|
+
data.tar.gz: 81af7a2be13345042b6e3a8846f170317b3b373f59bb49450ebaa648f2e3afe26382c913d4ae29f31c5529582344f302b28129478901e6437d587e5d6eee09d3
|
data/bin/pechkin
ADDED
data/lib/pechkin.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
require_relative 'pechkin/cli'
|
4
|
+
require_relative 'pechkin/api'
|
5
|
+
require_relative 'pechkin/config'
|
6
|
+
|
7
|
+
module Pechkin # :nodoc:
|
8
|
+
class << self
|
9
|
+
def run
|
10
|
+
options = CLI.parse(ARGV)
|
11
|
+
configuration = Config.new(options.config_file)
|
12
|
+
Rack::Server.start(app: Pechkin.create(configuration),
|
13
|
+
Port: options.port || configuration.port,
|
14
|
+
pid: options.pid_file)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/pechkin/api.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'grape'
|
2
|
+
require_relative 'telegram'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Pechkin # :nodoc:
|
6
|
+
# Generates all routes based on configuration
|
7
|
+
module Generator
|
8
|
+
def configure(config)
|
9
|
+
base_path = config['base_path']
|
10
|
+
resource base_path do
|
11
|
+
create_chanels(config['chanels'], config['bots'])
|
12
|
+
end
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_chanels(chanels, bots)
|
17
|
+
chanels.each do |chanel_name, chanel_desc|
|
18
|
+
bot_token = bots[chanel_desc['bot']]
|
19
|
+
chat_ids = chanel_desc['chat_ids']
|
20
|
+
bot = Telegram::Chanel.new(bot_token, chat_ids)
|
21
|
+
resource chanel_name do
|
22
|
+
create_chanel(bot, chanel_desc)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_chanel(bot, chanel_desc)
|
28
|
+
chanel_desc['messages'].each do |message_name, message_desc|
|
29
|
+
post message_name do
|
30
|
+
template = message_desc['template']
|
31
|
+
opts = { markup: 'HTML' }.update(message_desc['options'] || {})
|
32
|
+
# Some services will send json, but without correct content-type, then
|
33
|
+
# params will be parsed weirdely. So we try parse request body as json
|
34
|
+
params = ensure_json(request.body.read, params)
|
35
|
+
# If message description contains any variables will merge them with
|
36
|
+
# received parameters.
|
37
|
+
params = message_desc['variables'].merge(params) if message_desc.key?('variables')
|
38
|
+
bot.send_message(template, params, opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module Helpers # :nodoc:
|
45
|
+
def ensure_json(body, params)
|
46
|
+
if headers['Content-Type'] == 'application/json'
|
47
|
+
params # Expected content type. Do nothing, just return basic params
|
48
|
+
else
|
49
|
+
JSON.parse(body) # Try parse body as json. If it possible will return as
|
50
|
+
# params
|
51
|
+
end
|
52
|
+
rescue JSON::JSONError => _error
|
53
|
+
params
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class << self
|
58
|
+
def create(config)
|
59
|
+
klazz = Class.new(Grape::API) do
|
60
|
+
extend Generator
|
61
|
+
helpers Helpers
|
62
|
+
end
|
63
|
+
|
64
|
+
klazz.configure(config)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/pechkin/cli.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'optparse/time'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
require_relative 'version'
|
6
|
+
|
7
|
+
module Pechkin
|
8
|
+
# Command Line parser
|
9
|
+
module CLI
|
10
|
+
# Default values for CLI options
|
11
|
+
DEFAULT_OPTIONS = {
|
12
|
+
config_file: '/etc/pechkin/config.yml'
|
13
|
+
}.freeze
|
14
|
+
# Command Line Parser Builder
|
15
|
+
class CLIBuilder
|
16
|
+
attr_reader :options
|
17
|
+
def initialize(options_keeper)
|
18
|
+
@options = options_keeper
|
19
|
+
end
|
20
|
+
|
21
|
+
def build(parser) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
22
|
+
# rubocop:disable Metrics/LineLength
|
23
|
+
parser.banner = 'Usage: pechkin [options]'
|
24
|
+
parser.separator ''
|
25
|
+
|
26
|
+
parser.on('-c', '--config CONFIG_FILE', 'default value is /etc/pechkin/config.yml') do |value|
|
27
|
+
options.config_file = value
|
28
|
+
end
|
29
|
+
|
30
|
+
parser.on('-p', '--port PORT', Integer) do |value|
|
31
|
+
options.port = value
|
32
|
+
end
|
33
|
+
|
34
|
+
parser.on('--pid PID_FILE') do |value|
|
35
|
+
options.pid_file = value
|
36
|
+
end
|
37
|
+
|
38
|
+
parser.on('--log-dir LOG_DIR') do |value|
|
39
|
+
options.log_dir = value
|
40
|
+
end
|
41
|
+
|
42
|
+
parser.separator ''
|
43
|
+
parser.separator 'Common options:'
|
44
|
+
parser.on_tail('-h', '--help', 'Show this message') do
|
45
|
+
puts parser
|
46
|
+
exit
|
47
|
+
end
|
48
|
+
|
49
|
+
# Another typical switch to print the version.
|
50
|
+
parser.on_tail('--version', 'Show version') do
|
51
|
+
puts Version.version_string
|
52
|
+
exit
|
53
|
+
end
|
54
|
+
# rubocop:enable Metrics/LineLength
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class << self
|
59
|
+
def parse(args)
|
60
|
+
options_keeper = OpenStruct.new(DEFAULT_OPTIONS)
|
61
|
+
parser = OptionParser.new do |p|
|
62
|
+
CLIBuilder.new(options_keeper).build(p)
|
63
|
+
end
|
64
|
+
|
65
|
+
parser.parse(args)
|
66
|
+
options_keeper
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module Pechkin
|
6
|
+
module Telegram
|
7
|
+
# Easy way to render erb template
|
8
|
+
class Message < OpenStruct
|
9
|
+
def render(template_file)
|
10
|
+
ERB.new(IO.read(template_file)).result(binding)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
# Creates object which can send messages to assigned chanels
|
14
|
+
class Chanel
|
15
|
+
def initialize(bot_token, chat_ids)
|
16
|
+
@bot_token = bot_token
|
17
|
+
@chat_ids = chat_ids
|
18
|
+
@chat_ids = [chat_ids] unless chat_ids.is_a?(Array)
|
19
|
+
end
|
20
|
+
|
21
|
+
def send_message(message, data, options)
|
22
|
+
text = Message.new(data).render(message)
|
23
|
+
@chat_ids.map do |chat|
|
24
|
+
params = options.update(text: text, chat_id: chat)
|
25
|
+
response = send_data('sendMessage', params)
|
26
|
+
[response.code, response.body]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def send_data(method, data = {})
|
31
|
+
url = URI.parse(method_url(method))
|
32
|
+
Net::HTTP.post_form(url, data)
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_url(method)
|
36
|
+
"https://api.telegram.org/bot#{@bot_token}/#{method}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pechkin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ilya Arkhanhelsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grape
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.3.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.3.0
|
41
|
+
description:
|
42
|
+
email: ilya.arkhanhelsky at gmail.com
|
43
|
+
executables:
|
44
|
+
- pechkin
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- bin/pechkin
|
49
|
+
- lib/pechkin.rb
|
50
|
+
- lib/pechkin/api.rb
|
51
|
+
- lib/pechkin/cli.rb
|
52
|
+
- lib/pechkin/config.rb
|
53
|
+
- lib/pechkin/telegram.rb
|
54
|
+
- lib/pechkin/version.rb
|
55
|
+
homepage: https://github.com/iarkhanhelsky/pechkin
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.7.6
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Web service to proxy webhooks to Telegram Bot API
|
79
|
+
test_files: []
|