telegram-bot-ruby 0.2.0 → 0.2.1

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: 42f3aaa643f89d7ac2534a93a9867567706c3412
4
- data.tar.gz: c1e130a68c7e98375a0c536ebe8a62f16bdc5990
3
+ metadata.gz: 1d52d14ef44cbb3583be07ce94d6a497c5fcdbe9
4
+ data.tar.gz: a9e8eb1afb4a9ed07ff4a645f68f54f4978505d2
5
5
  SHA512:
6
- metadata.gz: 6a030e8417d75db739206e74cafae2502d0cfe97b5dd584ec9880d391fea050a0dd4e9102374fdba347d8a1c6ac88772bbf517857740132562f620e8cfa4cce2
7
- data.tar.gz: 5a226a421b69610e6d691ea0aeeb91a7c455ddf0ff82f70d518625b18c12ff4f7730953bc6bfd448bd40ca740ddd85183af67e7699de8a4d3abcead5d800bf87
6
+ metadata.gz: 9f79b2bf808f6b88e2003a8eb628a88dc45d093aae9e67b05a8659de96f71d48b9f37418c09bb0eb9b0b6d20507cf0f183bedd8329a476af1e030dcd965d1b62
7
+ data.tar.gz: 50db3e10ebadbd7daa4cb0f877e9453a191a1b1a5bebf8b530f16971917dea7bffa32819eb22dd67bf26899cc5a237e7e364e1b84cca34f42a7e5808c4bfa4cd
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Ruby wrapper for [Telegram's Bot API](https://core.telegram.org/bots/api).
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/telegram-bot-ruby.svg)](http://badge.fury.io/rb/telegram-bot-ruby)
6
+ [![Code Climate](https://codeclimate.com/github/atipugin/telegram-bot-ruby/badges/gpa.svg)](https://codeclimate.com/github/atipugin/telegram-bot-ruby)
7
+
5
8
  ## Installation
6
9
 
7
10
  Add following line to your Gemfile:
@@ -34,8 +37,10 @@ token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
34
37
  Telegram::Bot::Client.run(token) do |bot|
35
38
  bot.listen do |message|
36
39
  case message.text
37
- when /^hello/
40
+ when /^\/start$/
38
41
  bot.api.sendMessage(chat_id: message.chat.id, text: "Hello, #{message.from.username}")
42
+ when /^\/stop$/
43
+ bot.api.sendMessage(chat_id: message.chat.id, text: "Bye, #{message.from.username}")
39
44
  end
40
45
  end
41
46
  end
@@ -45,6 +50,28 @@ Note that `bot.api` object implements [Telegram Bot API methods](https://core.te
45
50
 
46
51
  Same thing about `message` object - it implements [Message](https://core.telegram.org/bots/api#message) spec, so you always know what to expect from it.
47
52
 
53
+ ## Custom keyboards
54
+
55
+ You can use your own [custom keyboards](https://core.telegram.org/bots#keyboards). Here is an example:
56
+
57
+ ```ruby
58
+ bot.listen do |message|
59
+ case message.text
60
+ when /^\/start$/
61
+ question = 'London is a capital of which country?'
62
+ # See more: https://core.telegram.org/bots/api#replykeyboardmarkup
63
+ answers =
64
+ Telegram::Bot::Types::ReplyKeyboardMarkup
65
+ .new(keyboard: [%w(A B), %w(C D)], one_time_keyboard: true)
66
+ bot.api.sendMessage(chat_id: message.chat.id, text: question, reply_markup: answers)
67
+ when /^\/stop$/
68
+ # See more: https://core.telegram.org/bots/api#replykeyboardhide
69
+ kb = Telegram::Bot::Types::ReplyKeyboardHide.new(hide_keyboard: true)
70
+ bot.api.sendMessage(chat_id: message.chat.id, text: 'Sorry to see you go :(', reply_markup: kb)
71
+ end
72
+ end
73
+ ```
74
+
48
75
  ## Contributing
49
76
 
50
77
  1. Fork it
@@ -2,7 +2,7 @@ require 'httparty'
2
2
  require 'persistent_httparty'
3
3
  require 'virtus'
4
4
 
5
- require 'telegram/bot/api'
6
5
  require 'telegram/bot/types'
6
+ require 'telegram/bot/api'
7
7
  require 'telegram/bot/client'
8
8
  require 'telegram/bot/version'
@@ -8,6 +8,11 @@ module Telegram
8
8
  sendSticker sendVideo sendLocation sendChatAction getUserProfilePhotos
9
9
  getUpdates setWebhook
10
10
  ).freeze
11
+ REPLY_MARKUP_TYPES = [
12
+ Telegram::Bot::Types::ReplyKeyboardMarkup,
13
+ Telegram::Bot::Types::ReplyKeyboardHide,
14
+ Telegram::Bot::Types::ForceReply
15
+ ].freeze
11
16
 
12
17
  attr_reader :token
13
18
 
@@ -22,8 +27,27 @@ module Telegram
22
27
  ENDPOINTS.include?(method_name.to_s) ? call(method_name, *args) : super
23
28
  end
24
29
 
25
- def call(endpoint, params = {})
26
- self.class.get("/bot#{token}/#{endpoint}", query: params).to_h
30
+ def call(endpoint, raw_params = {})
31
+ params = build_params(raw_params)
32
+ response = self.class.get("/bot#{token}/#{endpoint}", query: params)
33
+ response.code == 200 ? response.to_h : {}
34
+ end
35
+
36
+ private
37
+
38
+ def build_params(h)
39
+ h.each_with_object({}) do |(key, value), params|
40
+ params[key] = sanitize_value(value)
41
+ end
42
+ end
43
+
44
+ def sanitize_value(value)
45
+ jsonify_reply_markup(value)
46
+ end
47
+
48
+ def jsonify_reply_markup(value)
49
+ return value unless REPLY_MARKUP_TYPES.include?(value.class)
50
+ value.to_h.to_json
27
51
  end
28
52
  end
29
53
  end
@@ -10,3 +10,6 @@ require 'telegram/bot/types/contact'
10
10
  require 'telegram/bot/types/location'
11
11
  require 'telegram/bot/types/message'
12
12
  require 'telegram/bot/types/update'
13
+ require 'telegram/bot/types/reply_keyboard_markup'
14
+ require 'telegram/bot/types/reply_keyboard_hide'
15
+ require 'telegram/bot/types/force_reply'
@@ -0,0 +1,10 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class ForceReply < Base
5
+ attribute :force_reply, Boolean
6
+ attribute :selective, Boolean, default: false
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class ReplyKeyboardHide < Base
5
+ attribute :hide_keyboard, Boolean
6
+ attribute :selective, Boolean, default: false
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class ReplyKeyboardMarkup < Base
5
+ attribute :keyboard, Array[Array[String]]
6
+ attribute :resize_keyboard, Boolean, default: false
7
+ attribute :one_time_keyboard, Boolean, default: false
8
+ attribute :selective, Boolean, default: false
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-bot-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Tipugin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-27 00:00:00.000000000 Z
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -131,10 +131,13 @@ files:
131
131
  - lib/telegram/bot/types/base.rb
132
132
  - lib/telegram/bot/types/contact.rb
133
133
  - lib/telegram/bot/types/document.rb
134
+ - lib/telegram/bot/types/force_reply.rb
134
135
  - lib/telegram/bot/types/group_chat.rb
135
136
  - lib/telegram/bot/types/location.rb
136
137
  - lib/telegram/bot/types/message.rb
137
138
  - lib/telegram/bot/types/photo_size.rb
139
+ - lib/telegram/bot/types/reply_keyboard_hide.rb
140
+ - lib/telegram/bot/types/reply_keyboard_markup.rb
138
141
  - lib/telegram/bot/types/sticker.rb
139
142
  - lib/telegram/bot/types/update.rb
140
143
  - lib/telegram/bot/types/user.rb