ya_telegram_bot 0.1.1 → 0.2.0
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 +4 -4
- data/README.md +15 -4
- data/lib/ya_telegram_bot/base.rb +23 -14
- data/lib/ya_telegram_bot/exceptions.rb +1 -0
- data/lib/ya_telegram_bot/telegram_api/message.rb +84 -0
- data/lib/ya_telegram_bot/version.rb +1 -1
- data/ya_telegram_bot.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61a5de13ed8de99426a7bfa61de2546284469107
|
4
|
+
data.tar.gz: e9fab4795d112701d2c5af7fca256b0b7a96ce4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c80cb576d0254a8b2b76c56b907a9120585b32598ac41c4547673483b87da9914282454823bad44db4a51f123a344c8b4bb4ffc9681deefe80ba3dc9132e41d5
|
7
|
+
data.tar.gz: 869d9928d5b2e91236a3c542e33bda90705906887430290b321e14ac29573fd88e312365d9a3710c9498c82f88ef75bb775b82b1d28ea5990d7c83e5759ad553
|
data/README.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
**IN DEV**
|
4
4
|
|
5
|
+
## What's new **0.2.0**
|
6
|
+
|
7
|
+
* Class `YATelegramBot::TelegramAPI::Message`.
|
8
|
+
* `Base#Update` generates array of `Message`. See Usage.
|
9
|
+
|
10
|
+
|
5
11
|
## Usage
|
6
12
|
|
7
13
|
```ruby
|
@@ -19,15 +25,20 @@ updates.each do |message|
|
|
19
25
|
user = message['from']['first_name']
|
20
26
|
text = "Hello, *#{user}*"
|
21
27
|
|
22
|
-
Bot.
|
23
|
-
|
24
|
-
|
28
|
+
Bot.send_text chat: chat_id,
|
29
|
+
text: text,
|
30
|
+
markdown: true
|
25
31
|
end
|
32
|
+
|
33
|
+
Bot.updates.each { |message| message.reply text: 'Leave me alone!' }
|
34
|
+
|
26
35
|
```
|
27
36
|
|
28
37
|
## Contributing
|
29
38
|
|
30
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Nondv/telegram_bot.
|
40
|
+
This project is intended to be a safe, welcoming space for collaboration,
|
41
|
+
and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
31
42
|
|
32
43
|
|
33
44
|
## License
|
data/lib/ya_telegram_bot/base.rb
CHANGED
@@ -2,12 +2,17 @@ require 'rest-client'
|
|
2
2
|
require 'json'
|
3
3
|
require 'time'
|
4
4
|
|
5
|
+
require_relative 'exceptions'
|
6
|
+
Dir[File.expand_path('../telegram_api/*.rb', __FILE__)].each { |file| require file }
|
7
|
+
|
5
8
|
module YATelegramBot
|
6
9
|
#
|
7
10
|
# extend your class with this module to use API.
|
8
11
|
# Or include it if you want to use in instances
|
9
12
|
#
|
10
13
|
module Base
|
14
|
+
include TelegramAPI
|
15
|
+
|
11
16
|
def token(value)
|
12
17
|
@token = value
|
13
18
|
@api_request_prefix = "bot#{@token}"
|
@@ -28,7 +33,7 @@ module YATelegramBot
|
|
28
33
|
fail ResponseIsNotOk unless updates['ok']
|
29
34
|
|
30
35
|
@last_update_id = updates['result'].last['update_id'] unless updates['result'].empty?
|
31
|
-
updates['result'].map { |u| u['message'] }
|
36
|
+
updates['result'].map { |u| Message.new(u['message'], self) }
|
32
37
|
end
|
33
38
|
|
34
39
|
#
|
@@ -39,26 +44,30 @@ module YATelegramBot
|
|
39
44
|
# * :text - your message
|
40
45
|
#
|
41
46
|
# additional params:
|
42
|
-
# * :markdown
|
47
|
+
# * :markdown [Boolean] use telegram markdown
|
48
|
+
# * :reply_to [Integer] id of message you reply
|
43
49
|
#
|
44
|
-
def
|
50
|
+
def send_text(params = {})
|
51
|
+
response = send_api_request 'sendMessage',
|
52
|
+
params_for_sending_text(params)
|
53
|
+
response['ok']
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def params_for_sending_text(params)
|
45
59
|
fail NoChatSpecified unless params[:chat]
|
46
60
|
fail NoMessageSpecified unless params[:text]
|
47
61
|
|
48
|
-
|
49
|
-
params
|
50
|
-
|
51
|
-
if params[:markdown]
|
52
|
-
|
53
|
-
params[:parse_mode] = 'Markdown'
|
54
|
-
end
|
62
|
+
result = {}
|
63
|
+
result[:chat_id] = params[:chat]
|
64
|
+
result[:text] = params[:text]
|
65
|
+
result[:parse_mode] = 'Markdown' if params[:markdown]
|
66
|
+
result[:reply_to_message_id] = params[:reply_to].to_i if params[:reply_to]
|
55
67
|
|
56
|
-
|
57
|
-
response['ok']
|
68
|
+
result
|
58
69
|
end
|
59
70
|
|
60
|
-
private
|
61
|
-
|
62
71
|
def send_api_request(method, params = {})
|
63
72
|
fail NoTokenSpecified unless defined?(@token)
|
64
73
|
@telegram_api_url = 'https://api.telegram.org' unless defined? @telegram_api_url
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module YATelegramBot
|
4
|
+
module TelegramAPI
|
5
|
+
#
|
6
|
+
# represents incoming messages
|
7
|
+
#
|
8
|
+
class Message < Hash
|
9
|
+
TYPES = [:text, :audio, :photo, :document, :video, :voice, :contact, :location].freeze
|
10
|
+
|
11
|
+
#
|
12
|
+
# @param hash_message [Hash] update['message']
|
13
|
+
# @param bot [Base] for using api like sending replies
|
14
|
+
#
|
15
|
+
def initialize(hash_message, bot = nil)
|
16
|
+
@bot = bot
|
17
|
+
merge! hash_for_merging(hash_message)
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# @example message.text
|
22
|
+
#
|
23
|
+
def method_missing(m, *args)
|
24
|
+
return self[m] if self[m] && args.empty?
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# checking on message type via `message.<type>?`
|
30
|
+
# @example message.text?
|
31
|
+
#
|
32
|
+
TYPES.each do |t|
|
33
|
+
define_method("#{t}?") { self[t] }
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# @return [Symbol] type of a message (see Message::TYPES)
|
38
|
+
#
|
39
|
+
def type
|
40
|
+
TYPES.find { |t| self[t] }
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# send reply to this message via bot from #initialize
|
45
|
+
#
|
46
|
+
# params values:
|
47
|
+
# * :text [String]
|
48
|
+
# * :as_plain_message [Boolean] default: true. If it's set, method won't set :reply_to parameter
|
49
|
+
#
|
50
|
+
# @example message.reply(text: 'Hi, *friend*!', markdown: true)
|
51
|
+
#
|
52
|
+
def reply(params = {})
|
53
|
+
fail InitWithoutBot unless @bot
|
54
|
+
|
55
|
+
params[:chat] = self[:chat]['id']
|
56
|
+
|
57
|
+
params[:as_plain_message] = true unless params.key? :as_plain_message
|
58
|
+
params[:reply_to] = self[:id] unless params[:as_plain_message]
|
59
|
+
param.delete :as_plain_message
|
60
|
+
|
61
|
+
@bot.send_text params
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
#
|
67
|
+
# converts some fields like 'user'
|
68
|
+
# @return [Hash]
|
69
|
+
#
|
70
|
+
def hash_for_merging(hash)
|
71
|
+
new_hash = {}
|
72
|
+
new_hash[:id] = hash['message_id'].to_i
|
73
|
+
new_hash[:date] = Time.at hash['date'].to_i
|
74
|
+
new_hash[:from] = hash['from'] # TODO: class User
|
75
|
+
new_hash[:chat] = hash['chat'] # TODO: class Chat
|
76
|
+
|
77
|
+
type = TYPES.find { |t| hash[t.to_s] }
|
78
|
+
new_hash[type] = hash[type.to_s] # TODO: fail if type not found
|
79
|
+
|
80
|
+
new_hash
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/ya_telegram_bot.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = 'Yet Another Telegram Bot'
|
13
13
|
spec.description = 'Module for using Telegram Bot API'
|
14
|
-
spec.homepage = ''
|
14
|
+
spec.homepage = 'https://github.com/Nondv/telegram_bot'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ya_telegram_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitriy Non
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,9 +99,10 @@ files:
|
|
99
99
|
- lib/ya_telegram_bot.rb
|
100
100
|
- lib/ya_telegram_bot/base.rb
|
101
101
|
- lib/ya_telegram_bot/exceptions.rb
|
102
|
+
- lib/ya_telegram_bot/telegram_api/message.rb
|
102
103
|
- lib/ya_telegram_bot/version.rb
|
103
104
|
- ya_telegram_bot.gemspec
|
104
|
-
homepage:
|
105
|
+
homepage: https://github.com/Nondv/telegram_bot
|
105
106
|
licenses:
|
106
107
|
- MIT
|
107
108
|
metadata: {}
|