ya_telegram_bot 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33d0b778d491d225fe59fadff14f620515b9ef71
4
- data.tar.gz: 1a840811af1f7f8c687c02e8a6d93ed0dc3a6232
3
+ metadata.gz: 61a5de13ed8de99426a7bfa61de2546284469107
4
+ data.tar.gz: e9fab4795d112701d2c5af7fca256b0b7a96ce4c
5
5
  SHA512:
6
- metadata.gz: ce566672deef931074d949515b584e2cf6613266efd7bf7f4da0df9ed1cc8383a6a1e8f897b189a936afa787a44b34b4f3ce336d5b1dcfe2ed50d20e9313caa0
7
- data.tar.gz: 7fea81894b8587ac5796a548173fee2ff2e1552f5f59b17977e765a277e1ad26c6e994da4a8683c18752ce82a0ff88022d2ba2f99f45abba3edfe80453257aff
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.send_message chat: chat_id,
23
- text: text,
24
- markdown: true
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/[USERNAME]/telegram_bot. 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.
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
@@ -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 - use telegram markdown
47
+ # * :markdown [Boolean] use telegram markdown
48
+ # * :reply_to [Integer] id of message you reply
43
49
  #
44
- def send_message(params = {})
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
- params[:chat_id] = params[:chat]
49
- params.delete :chat
50
-
51
- if params[:markdown]
52
- params.delete :markdown
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
- response = send_api_request('sendMessage', params)
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
@@ -3,4 +3,5 @@ module YATelegramBot
3
3
  class ResponseIsNotOk; end
4
4
  class NoChatSpecified; end
5
5
  class NoMessageSpecified; end
6
+ class InitWithoutBot; end
6
7
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module YATelegramBot
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -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.1.1
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-02 00:00:00.000000000 Z
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: {}