botomizer 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/botomizer-0.1.0.gem +0 -0
- data/botomizer-0.1.1.gem +0 -0
- data/botomizer-0.1.2.gem +0 -0
- data/botomizer.gemspec +30 -0
- data/examples/telegram.rb +44 -0
- data/lib/botomizer/cache.rb +20 -0
- data/lib/botomizer/chat.rb +12 -0
- data/lib/botomizer/client.rb +77 -0
- data/lib/botomizer/exceptions.rb +31 -0
- data/lib/botomizer/handler.rb +11 -0
- data/lib/botomizer/message.rb +22 -0
- data/lib/botomizer/telegram/webhook.rb +33 -0
- data/lib/botomizer/telegram.rb +51 -0
- data/lib/botomizer/version.rb +3 -0
- data/lib/botomizer.rb +11 -0
- metadata +33 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 668bddf9110bdaf607ec33797816f10989c7d88f
|
4
|
+
data.tar.gz: 4e4e2c7f4eb1a2ea2b16e28dda3cbb2d69685640
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 274cd5bdb089d643de8ad0629e6a8fc088ad128cae60d8b9b85717b0d14db5b9e0ce5613cc7eaa5829b61363bfd0422f22379a50f3bb7d174d0901273a607cfc
|
7
|
+
data.tar.gz: 0afb77c72d1d79ac8d7d45552987caaf3d4235af2d44f586218b729ac5bc639db05a0f20ff55b276c3bce3a0a5e728cf1664552af11af2163110f3f075e9d08b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at alex.b@matomy.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
botomizer (0.1.0)
|
5
|
+
httparty (>= 0.15.6)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.1.4)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
concurrent-ruby (1.0.5)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
httparty (0.15.6)
|
18
|
+
multi_xml (>= 0.5.2)
|
19
|
+
i18n (0.9.1)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
minitest (5.10.3)
|
22
|
+
multi_xml (0.6.0)
|
23
|
+
rake (12.3.0)
|
24
|
+
rspec (3.7.0)
|
25
|
+
rspec-core (~> 3.7.0)
|
26
|
+
rspec-expectations (~> 3.7.0)
|
27
|
+
rspec-mocks (~> 3.7.0)
|
28
|
+
rspec-core (3.7.0)
|
29
|
+
rspec-support (~> 3.7.0)
|
30
|
+
rspec-expectations (3.7.0)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.7.0)
|
33
|
+
rspec-mocks (3.7.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.7.0)
|
36
|
+
rspec-support (3.7.0)
|
37
|
+
thread_safe (0.3.6)
|
38
|
+
tzinfo (1.2.4)
|
39
|
+
thread_safe (~> 0.1)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
activesupport (~> 5.1)
|
46
|
+
botomizer!
|
47
|
+
bundler (~> 1.16)
|
48
|
+
rake (> 10.0)
|
49
|
+
rspec (~> 3.0)
|
50
|
+
|
51
|
+
BUNDLED WITH
|
52
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Alex Belenky
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Gem::Botomizer
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gem/botomizer`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'gem-botomizer'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install gem-botomizer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gem-botomizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Gem::Botomizer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gem-botomizer/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gem/botomizer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/botomizer-0.1.0.gem
ADDED
Binary file
|
data/botomizer-0.1.1.gem
ADDED
Binary file
|
data/botomizer-0.1.2.gem
ADDED
Binary file
|
data/botomizer.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "botomizer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "botomizer"
|
8
|
+
spec.version = Botomizer::VERSION
|
9
|
+
spec.authors = ["Alex Belenky"]
|
10
|
+
spec.email = ["alex.b@matomy.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
13
|
+
spec.description = %q{Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/Stajor/botomizer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "activesupport", "~> 5.1"
|
28
|
+
|
29
|
+
spec.add_dependency 'httparty', "~> 0.15.6"
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'botomizer')
|
3
|
+
require 'active_support/cache'
|
4
|
+
|
5
|
+
request = {
|
6
|
+
"update_id" => 430379297,
|
7
|
+
"message" => {
|
8
|
+
"message_id" => 27167,
|
9
|
+
"from" => {
|
10
|
+
"id" => 42858,
|
11
|
+
"is_bot" => false,
|
12
|
+
"first_name" => "Alex",
|
13
|
+
"last_name" => "B",
|
14
|
+
"username" => "AlexBel",
|
15
|
+
"language_code" => "en"
|
16
|
+
}, "chat" => {
|
17
|
+
"id" => 42858,
|
18
|
+
"first_name" => "Alex",
|
19
|
+
"last_name" => "B",
|
20
|
+
"username" => "AlexBel",
|
21
|
+
"type" => "private"
|
22
|
+
},
|
23
|
+
"date" => 1510219771,
|
24
|
+
"text" => "/test bla"
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
class CmdHandler < Botomizer::Handler
|
29
|
+
def cmd_test
|
30
|
+
puts "test message " + @client.message.text
|
31
|
+
@client.send_message(@client.message.text)
|
32
|
+
end
|
33
|
+
|
34
|
+
def cmd_fallback
|
35
|
+
puts "fallback"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
client = Botomizer::Client.new(:telegram, {
|
40
|
+
token: '405249984:AAFCELndVU1H-wUeI6jkRGNuP0CTFpDgn-Y',
|
41
|
+
cache: ActiveSupport::Cache::MemoryStore.new
|
42
|
+
})
|
43
|
+
|
44
|
+
client.handle(request, CmdHandler)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Botomizer::Cache
|
2
|
+
attr_reader :prefix
|
3
|
+
|
4
|
+
def initialize(cache, prefix)
|
5
|
+
@cache = cache
|
6
|
+
@prefix = prefix
|
7
|
+
end
|
8
|
+
|
9
|
+
def fetch(key)
|
10
|
+
@cache.fetch("#{@prefix}_#{key}") if @cache
|
11
|
+
end
|
12
|
+
|
13
|
+
def write(key, value)
|
14
|
+
@cache.write("#{@prefix}_#{key}", value, expires_in: 1.hour) if @cache
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(key)
|
18
|
+
@cache.delete("#{@prefix}_#{key}") if @cache
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Botomizer::Chat
|
2
|
+
attr_reader :id, :type, :first_name, :last_name, :username, :title
|
3
|
+
|
4
|
+
def initialize(params)
|
5
|
+
@id = params[:id]
|
6
|
+
@type = params[:type]
|
7
|
+
@first_name = params[:first_name]
|
8
|
+
@last_name = params[:last_name]
|
9
|
+
@title = params[:title]
|
10
|
+
@username = params[:username]
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class Botomizer::Client
|
2
|
+
def initialize(client, options = {})
|
3
|
+
begin
|
4
|
+
require "botomizer/#{client}".downcase
|
5
|
+
|
6
|
+
@bot = Object.const_get("Botomizer::#{client.capitalize}")
|
7
|
+
@cache = Botomizer::Cache.new(options[:cache] || nil, :botomizer_cmd)
|
8
|
+
@name = client
|
9
|
+
@bot.options = options
|
10
|
+
@bot.client = self
|
11
|
+
rescue LoadError
|
12
|
+
raise Botomizer::UnsupportedClient.new("Unsupported client #{client}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def name
|
17
|
+
@name
|
18
|
+
end
|
19
|
+
|
20
|
+
def webhook
|
21
|
+
@bot.webhook
|
22
|
+
end
|
23
|
+
|
24
|
+
def message
|
25
|
+
bot.message
|
26
|
+
end
|
27
|
+
|
28
|
+
def chat
|
29
|
+
bot.chat
|
30
|
+
end
|
31
|
+
|
32
|
+
def bot
|
33
|
+
@bot
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle(request, handler)
|
37
|
+
@bot.parse_request(request)
|
38
|
+
|
39
|
+
handler = handler.new(@bot)
|
40
|
+
|
41
|
+
if @bot.message.cmd?
|
42
|
+
@cache.write(@bot.chat.id, @bot.message.cmd)
|
43
|
+
else
|
44
|
+
@bot.message.cmd = @cache.fetch(@bot.chat.id)
|
45
|
+
end
|
46
|
+
|
47
|
+
if handler.respond_to? "cmd_#{@bot.message.cmd}"
|
48
|
+
handler.public_send("cmd_#{@bot.message.cmd}")
|
49
|
+
else
|
50
|
+
handler.cmd_fallback
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def clean_history
|
55
|
+
@cache.cache.delete(@bot.chat.id)
|
56
|
+
end
|
57
|
+
|
58
|
+
def send(method, query = {})
|
59
|
+
@bot.send(method, query)
|
60
|
+
end
|
61
|
+
|
62
|
+
def send!(method, query = {})
|
63
|
+
result = send(method, query)
|
64
|
+
raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
def send_message(text, options = {})
|
69
|
+
@bot.send_message(text, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
def send_message!(text, options = {})
|
73
|
+
result = send_message(text, options)
|
74
|
+
raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed
|
75
|
+
result
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Botomizer
|
2
|
+
# @abstact Exceptions raised by Botomizer inherit from Error
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
# Exception raised when you attempt to set a non-existent client
|
6
|
+
class UnsupportedClient < Error; end
|
7
|
+
|
8
|
+
class WebhookError < Error; end
|
9
|
+
|
10
|
+
class ChatError < Error; end
|
11
|
+
|
12
|
+
class MessageError < Error; end
|
13
|
+
|
14
|
+
class BotError < Error; end
|
15
|
+
|
16
|
+
# @abstract Exceptions which inherit from ResponseError contain the Net::HTTP
|
17
|
+
# response object accessible via the {#response} method.
|
18
|
+
class ResponseError < Error
|
19
|
+
# Returns the response of the last request
|
20
|
+
# @return [Net::HTTPResponse] A subclass of Net::HTTPResponse, e.g.
|
21
|
+
# Net::HTTPOK
|
22
|
+
attr_reader :response
|
23
|
+
|
24
|
+
# Instantiate an instance of ResponseError with a Net::HTTPResponse object
|
25
|
+
# @param [Net::HTTPResponse]
|
26
|
+
def initialize(response)
|
27
|
+
@response = response
|
28
|
+
super(response)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Botomizer::Message
|
2
|
+
attr_accessor :id, :text, :cmd
|
3
|
+
|
4
|
+
def initialize(params)
|
5
|
+
@id = params[:id]
|
6
|
+
@text = params[:text]
|
7
|
+
|
8
|
+
if @text.index('/') == 0
|
9
|
+
sp = @text.split(' ')
|
10
|
+
@cmd = sp.first.downcase.gsub!('/', '')
|
11
|
+
@text = sp.drop(1).join(' ')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def cmd?
|
16
|
+
!@cmd.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def text?
|
20
|
+
!@text.blank?
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Botomizer::Telegram::Webhook
|
2
|
+
def initialize(client)
|
3
|
+
@client = client
|
4
|
+
end
|
5
|
+
|
6
|
+
def set(url)
|
7
|
+
@client.send('setWebhook', {url: url})
|
8
|
+
end
|
9
|
+
|
10
|
+
def set!(url)
|
11
|
+
begin
|
12
|
+
@client.send!('setWebhook', {url: url})
|
13
|
+
rescue Botomizer::ResponseError => e
|
14
|
+
raise Botomizer::WebhookError.new(e)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def info
|
19
|
+
@client.send('getWebhookInfo')
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete
|
23
|
+
@client.send('deleteWebhook')
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete!
|
27
|
+
begin
|
28
|
+
@client.send!('deleteWebhook')
|
29
|
+
rescue Botomizer::ResponseError => e
|
30
|
+
raise Botomizer::WebhookError.new(e)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Botomizer::Telegram
|
2
|
+
API_URL = 'https://api.telegram.org/bot'.freeze
|
3
|
+
|
4
|
+
class << self
|
5
|
+
attr_accessor :options, :message, :chat, :client
|
6
|
+
|
7
|
+
def webhook
|
8
|
+
Webhook.new(@client)
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse_request(request)
|
12
|
+
message_key = ['message', 'edited_message', 'channel_post', 'edited_channel_post'].detect do |key|
|
13
|
+
request.has_key? key
|
14
|
+
end
|
15
|
+
|
16
|
+
if message_key
|
17
|
+
message = request[message_key]
|
18
|
+
@message = Botomizer::Message.new({
|
19
|
+
id: message['message_id'],
|
20
|
+
text: message['text']
|
21
|
+
})
|
22
|
+
@chat = Botomizer::Chat.new({
|
23
|
+
id: message['chat']['id'],
|
24
|
+
type: message['chat']['type'],
|
25
|
+
title: message['chat']['title'] || nil,
|
26
|
+
first_name: message['chat']['first_name'] || nil,
|
27
|
+
last_name: message['chat']['last_name'] || nil,
|
28
|
+
username: message['chat']['username']
|
29
|
+
})
|
30
|
+
else
|
31
|
+
raise Botomizer::BotError.new("Can't parse request")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def send(method, query = {})
|
36
|
+
response = HTTParty.get("#{API_URL}#{@options[:token]}/#{method}", {query: query}).parsed_response
|
37
|
+
|
38
|
+
if response['ok']
|
39
|
+
{status: :success, result: response['result'], message: response['description']}
|
40
|
+
else
|
41
|
+
{status: :failed, error: response['error_code'], message: response['description']}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def send_message(text, options = {})
|
46
|
+
send(:sendMessage, {chat_id: @chat.id, text: text, parse_mode: 'HTML'}.merge(options))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
require 'botomizer/telegram/webhook'
|
data/lib/botomizer.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: botomizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Belenky
|
@@ -28,14 +28,14 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -70,14 +70,14 @@ dependencies:
|
|
70
70
|
name: httparty
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 0.15.6
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.15.6
|
83
83
|
description: Write a longer description or delete this line.
|
@@ -86,8 +86,34 @@ email:
|
|
86
86
|
executables: []
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
|
-
files:
|
90
|
-
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- botomizer-0.1.0.gem
|
102
|
+
- botomizer-0.1.1.gem
|
103
|
+
- botomizer-0.1.2.gem
|
104
|
+
- botomizer.gemspec
|
105
|
+
- examples/telegram.rb
|
106
|
+
- lib/botomizer.rb
|
107
|
+
- lib/botomizer/cache.rb
|
108
|
+
- lib/botomizer/chat.rb
|
109
|
+
- lib/botomizer/client.rb
|
110
|
+
- lib/botomizer/exceptions.rb
|
111
|
+
- lib/botomizer/handler.rb
|
112
|
+
- lib/botomizer/message.rb
|
113
|
+
- lib/botomizer/telegram.rb
|
114
|
+
- lib/botomizer/telegram/webhook.rb
|
115
|
+
- lib/botomizer/version.rb
|
116
|
+
homepage: https://github.com/Stajor/botomizer
|
91
117
|
licenses:
|
92
118
|
- MIT
|
93
119
|
metadata: {}
|