ya_telegram_bot 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61a5de13ed8de99426a7bfa61de2546284469107
4
- data.tar.gz: e9fab4795d112701d2c5af7fca256b0b7a96ce4c
3
+ metadata.gz: c607a290b4ddc0b1aa0e6655266736dfc049db55
4
+ data.tar.gz: d5de93cfa149d2e35f0f46053117335fdc3d6efd
5
5
  SHA512:
6
- metadata.gz: c80cb576d0254a8b2b76c56b907a9120585b32598ac41c4547673483b87da9914282454823bad44db4a51f123a344c8b4bb4ffc9681deefe80ba3dc9132e41d5
7
- data.tar.gz: 869d9928d5b2e91236a3c542e33bda90705906887430290b321e14ac29573fd88e312365d9a3710c9498c82f88ef75bb775b82b1d28ea5990d7c83e5759ad553
6
+ metadata.gz: a72d2a026a4bbe3e6d12f4e77db06a7b249df924236c42aed30dbdd60700e86a7ff46a9cff8ae7d69c7c24aff60b8343d044cd25f907f0708b360c84bd2aeda7
7
+ data.tar.gz: dadfbb256be7c04a62dbb82f61710564b861a7da99ec8d2695bd3f94e29c5666ac7cf67e198e6476ae8677f4d4be5b341d52461b3abc4698cd5b8a6b5d26d40d
data/README.md CHANGED
@@ -2,11 +2,10 @@
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.
5
+ ## What's new **0.3.0**
9
6
 
7
+ * Class `YATelegramBot::TelegramAPI::User`.
8
+ * Class `YATelegramBot::TelegramAPI::Chat`.
10
9
 
11
10
  ## Usage
12
11
 
@@ -19,6 +18,8 @@ class Bot
19
18
  token YOUR_TOKEN
20
19
  end
21
20
 
21
+ # getting LAST updates as array of YATelegramBot:TelegramAPI::Message
22
+ # "LAST" means that #updates method every time will return fresh updates (so you will never get same updates inside your process)
22
23
  updates = Bot.updates
23
24
  updates.each do |message|
24
25
  chat_id = message['chat']['id']
@@ -30,8 +31,18 @@ updates.each do |message|
30
31
  markdown: true
31
32
  end
32
33
 
34
+ # reply to message
33
35
  Bot.updates.each { |message| message.reply text: 'Leave me alone!' }
34
36
 
37
+ # YATelegramBot::TelegramAPI::User
38
+ user = Bot.updates[0].from
39
+ user.send_text text: '*hey hey!*',
40
+ markdown: true
41
+
42
+ #YATelegramBot::TelegramAPI::Chat
43
+ chat = Bot.updates[0].chat
44
+ chat.send_text text: "Hi, #{chat.type == :private ? 'dude' : 'all'}!"
45
+
35
46
  ```
36
47
 
37
48
  ## Contributing
@@ -50,7 +50,7 @@ module YATelegramBot
50
50
  def send_text(params = {})
51
51
  response = send_api_request 'sendMessage',
52
52
  params_for_sending_text(params)
53
- response['ok']
53
+ response['ok'] && Message.new(response['result'], self)
54
54
  end
55
55
 
56
56
  private
@@ -0,0 +1,42 @@
1
+ module YATelegramBot
2
+ module TelegramAPI
3
+ #
4
+ # represents Telegram Chat
5
+ #
6
+ class Chat < Hash
7
+ FIELDS = [:id, :type, :title, :first_name, :last_name, :username].freeze
8
+
9
+ #
10
+ # @param hash [Hash] hash from json.
11
+ # @param bot [Base] for using api methods
12
+ #
13
+ def initialize(hash, bot = nil)
14
+ super()
15
+ @bot = bot
16
+ FIELDS.each { |f| self[f] = hash[f.to_s] if hash[f.to_s] }
17
+
18
+ self[:type] = self[:type].to_sym
19
+ end
20
+
21
+ #
22
+ # @example chat.type
23
+ #
24
+ def method_missing(m, *args)
25
+ return self[m] if self[m] && args.empty?
26
+ super
27
+ end
28
+
29
+ #
30
+ # uses bot to send text to this user.
31
+ #
32
+ # @param params [Hash] params for Base#send_text. This method will only set :chat to self[:id]
33
+ #
34
+ def send_text(params = {})
35
+ fail InitWithoutBot unless @bot
36
+
37
+ params[:chat] = id
38
+ @bot.send_text params
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,4 +1,6 @@
1
1
  require 'time'
2
+ require_relative 'user'
3
+ require_relative 'chat'
2
4
 
3
5
  module YATelegramBot
4
6
  module TelegramAPI
@@ -45,7 +47,7 @@ module YATelegramBot
45
47
  #
46
48
  # params values:
47
49
  # * :text [String]
48
- # * :as_plain_message [Boolean] default: true. If it's set, method won't set :reply_to parameter
50
+ # * :as_plain_message [Boolean] default: true. If it's true, method won't set :reply_to parameter
49
51
  #
50
52
  # @example message.reply(text: 'Hi, *friend*!', markdown: true)
51
53
  #
@@ -68,11 +70,10 @@ module YATelegramBot
68
70
  # @return [Hash]
69
71
  #
70
72
  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
73
+ new_hash = { id: hash['message_id'].to_i,
74
+ date: Time.at(hash['date'].to_i),
75
+ from: User.new(hash['from'], @bot),
76
+ chat: Chat.new(hash['chat'], @bot) }
76
77
 
77
78
  type = TYPES.find { |t| hash[t.to_s] }
78
79
  new_hash[type] = hash[type.to_s] # TODO: fail if type not found
@@ -0,0 +1,40 @@
1
+ module YATelegramBot
2
+ module TelegramAPI
3
+ #
4
+ # represents Telegram User
5
+ #
6
+ class User < Hash
7
+ FIELDS = [:id, :first_name, :last_name, :username].freeze
8
+
9
+ #
10
+ # @param hash [Hash] hash from json.
11
+ # @param bot [Base] for using api methods
12
+ #
13
+ def initialize(hash, bot = nil)
14
+ super()
15
+ @bot = bot
16
+ FIELDS.each { |f| self[f] = hash[f.to_s] if hash[f.to_s] }
17
+ end
18
+
19
+ #
20
+ # @example user.first_name
21
+ #
22
+ def method_missing(m, *args)
23
+ return self[m] if self[m] && args.empty?
24
+ super
25
+ end
26
+
27
+ #
28
+ # uses bot to send text to this user.
29
+ #
30
+ # @param params [Hash] params for Base#send_text. This method will only set :chat to self[:id]
31
+ #
32
+ def send_text(params = {})
33
+ fail InitWithoutBot unless @bot
34
+
35
+ params[:chat] = id
36
+ @bot.send_text params
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module YATelegramBot
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.0
4
+ version: 0.3.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-06 00:00:00.000000000 Z
11
+ date: 2016-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,7 +99,9 @@ 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/chat.rb
102
103
  - lib/ya_telegram_bot/telegram_api/message.rb
104
+ - lib/ya_telegram_bot/telegram_api/user.rb
103
105
  - lib/ya_telegram_bot/version.rb
104
106
  - ya_telegram_bot.gemspec
105
107
  homepage: https://github.com/Nondv/telegram_bot