telegram-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +97 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/examples/api/inline_keyboard_markup.rb +17 -0
  13. data/examples/api/send_message.rb +5 -0
  14. data/examples/client/handler.rb +19 -0
  15. data/lib/telegram/bot.rb +29 -0
  16. data/lib/telegram/bot/api.rb +261 -0
  17. data/lib/telegram/bot/client.rb +54 -0
  18. data/lib/telegram/bot/client_accessors.rb +5 -0
  19. data/lib/telegram/bot/configuration.rb +11 -0
  20. data/lib/telegram/bot/exceptions.rb +13 -0
  21. data/lib/telegram/bot/types.rb +47 -0
  22. data/lib/telegram/bot/types/animation.rb +17 -0
  23. data/lib/telegram/bot/types/audio.rb +14 -0
  24. data/lib/telegram/bot/types/callback_game.rb +4 -0
  25. data/lib/telegram/bot/types/callback_query.rb +23 -0
  26. data/lib/telegram/bot/types/chat.rb +25 -0
  27. data/lib/telegram/bot/types/chat_member.rb +28 -0
  28. data/lib/telegram/bot/types/chat_photo.rb +10 -0
  29. data/lib/telegram/bot/types/chosen_inline_result.rb +21 -0
  30. data/lib/telegram/bot/types/contact.rb +12 -0
  31. data/lib/telegram/bot/types/document.rb +17 -0
  32. data/lib/telegram/bot/types/file.rb +15 -0
  33. data/lib/telegram/bot/types/force_reply.rb +9 -0
  34. data/lib/telegram/bot/types/game.rb +26 -0
  35. data/lib/telegram/bot/types/inline_keyboard_button.rb +19 -0
  36. data/lib/telegram/bot/types/inline_keyboard_markup.rb +20 -0
  37. data/lib/telegram/bot/types/inline_query.rb +21 -0
  38. data/lib/telegram/bot/types/invoice.rb +13 -0
  39. data/lib/telegram/bot/types/keyboard_button.rb +11 -0
  40. data/lib/telegram/bot/types/location.rb +10 -0
  41. data/lib/telegram/bot/types/mask_position.rb +12 -0
  42. data/lib/telegram/bot/types/message.rb +145 -0
  43. data/lib/telegram/bot/types/message_entity.rb +17 -0
  44. data/lib/telegram/bot/types/order_info.rb +16 -0
  45. data/lib/telegram/bot/types/photo_size.rb +12 -0
  46. data/lib/telegram/bot/types/pre_checkout_query.rb +23 -0
  47. data/lib/telegram/bot/types/reply_keyboard_markup.rb +22 -0
  48. data/lib/telegram/bot/types/reply_keyboard_remove.rb +9 -0
  49. data/lib/telegram/bot/types/shipping_address.rb +14 -0
  50. data/lib/telegram/bot/types/shipping_query.rb +20 -0
  51. data/lib/telegram/bot/types/sticker.rb +24 -0
  52. data/lib/telegram/bot/types/sticker_set.rb +16 -0
  53. data/lib/telegram/bot/types/successful_payment.rb +19 -0
  54. data/lib/telegram/bot/types/update.rb +54 -0
  55. data/lib/telegram/bot/types/user.rb +14 -0
  56. data/lib/telegram/bot/types/user_profile_photos.rb +14 -0
  57. data/lib/telegram/bot/types/venue.rb +16 -0
  58. data/lib/telegram/bot/types/video.rb +19 -0
  59. data/lib/telegram/bot/types/video_note.rb +17 -0
  60. data/lib/telegram/bot/types/voice.rb +12 -0
  61. data/lib/telegram/bot/types/webhook_info.rb +15 -0
  62. data/lib/telegram/version.rb +3 -0
  63. data/telegram-ruby.gemspec +29 -0
  64. metadata +176 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 846c1cc10ffea65fd25ecac7a05884fa1edbc0e9321f81245558cffcc61c6984
4
+ data.tar.gz: f9545a566f47c5e98f5d312c6039d84eba1bd9f5dad210f76e241268ee4c5b7b
5
+ SHA512:
6
+ metadata.gz: 429581b8f2e9fd438d44f5d2f4f62c3a6d2a27e57f267594e7732b886367ec980b33ad373d222da4e4f58b8b38dfff1753080090ad05919c3f82c02013c86e12
7
+ data.tar.gz: 460a6bd5ef2e217360785c0bad3a3fa59335ebd322856d786f081ad6e8e5a12b81c50a4c15d0c1bb4e614966adc8cefc70bb6fae1f404c2b0670e46f32abba4a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ##0.1.0
4
+ - Initialize repo
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in telegram-rails.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alex
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.
@@ -0,0 +1,97 @@
1
+ # telegram-ruby
2
+
3
+ Ruby wrapper for [Telegram's Bot API](https://core.telegram.org/bots/api).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'telegram-ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install telegram-ruby
20
+
21
+ ## Telegram API support
22
+ All types and methods of the Telegram Bot API 3.5 are supported.
23
+
24
+ ## Configuration
25
+ First of all you need create your own bot and obtain a token
26
+
27
+ [How do I create a bot?](https://core.telegram.org/bots#3-how-do-i-create-a-bot)
28
+
29
+ Then you can configure **Telegram::Bot** in config/initializers/telegram.rb
30
+
31
+
32
+ ```
33
+ require 'telegram/bot'
34
+
35
+ Telegram::Bot.configure do |conf|
36
+ conf.token = 'Here your token'
37
+ conf.raise_exceptions = false #By default true
38
+ end
39
+ ```
40
+
41
+
42
+ ## Usage
43
+
44
+ You can use **Telegram::Bot::Api** stand alone
45
+
46
+
47
+ ```
48
+ api = Telegram::Bot::Api.new([Token key])
49
+ api.getMe
50
+ ```
51
+
52
+ Or by using **Telegram::Bot::Client** to receive updates from Telegram
53
+
54
+ Create handler class
55
+
56
+ ```
57
+ class MyHandler
58
+ include Telegram::Bot::ClientAccessors
59
+
60
+ # this method will be called if /test string was found in message text
61
+ def cmd_test
62
+ api.sendMessage(message.chat.id, 'Yes, it is test')
63
+ end
64
+
65
+ # this method will be called if no command found
66
+ def cmd_fallback
67
+ # log message
68
+ end
69
+ end
70
+ ```
71
+
72
+ In your controller add
73
+
74
+ ```
75
+ def create
76
+ client = Telegram::Bot::Client.new(MyHandler)
77
+ client.handle(params)
78
+ end
79
+ ```
80
+
81
+ In your handler you can use helper accessors
82
+
83
+ **api** - **Telegram::Bot::Api** instance
84
+
85
+ **update** - This [object](https://core.telegram.org/bots/api#update) represents an incoming update.
86
+
87
+ **message** - This [object](https://core.telegram.org/bots/api#message) represents a message.
88
+
89
+ **command** - current command if found in message text (without prefix cmd_)
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Stajor/telegram-ruby. 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.
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "telegram/rails"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'telegram/bot'
3
+
4
+ api = Telegram::Bot::Api.new('[place-your-token]')
5
+ btn1 = Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Command 1', callback_data: '/command1')
6
+ btn2 = Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Command 2', callback_data: '/command2')
7
+ btn3 = Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Command 3', callback_data: '/command3')
8
+ api.sendMessage('[chat-id]', 'some text', reply_markup: Telegram::Bot::Types::InlineKeyboardMarkup.new([
9
+ [btn1, btn2], [btn3]
10
+ ]))
11
+
12
+ # or
13
+
14
+ api.sendMessage('[chat-id]', 'some text', reply_markup: Telegram::Bot::Types::InlineKeyboardMarkup.new([
15
+ [{text: 'Command 1', callback_data: '/command1'}, {text: 'Command 2', callback_data: '/command2'}],
16
+ [{text: 'Command 3', callback_data: '/command3'}],
17
+ ]))
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'telegram/bot'
3
+
4
+ api = Telegram::Bot::Api.new('[place-your-token]')
5
+ api.sendMessage('[chat-id]', '<b>some text</b>', {parse_mode: 'HTML'})
@@ -0,0 +1,19 @@
1
+ # In controller
2
+ #...
3
+ #def create
4
+ # client = Telegram::Bot::Client.new(MyHandler)
5
+ # client.handle(params)
6
+ #end
7
+ #...
8
+
9
+ class MyHandler
10
+ include Telegram::Bot::ClientAccessors
11
+
12
+ def cmd_test
13
+ api.sendMessage(message.chat.id, 'Yes, it is test')
14
+ end
15
+
16
+ def cmd_fallback
17
+ # log message
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ require 'telegram/version'
5
+ require 'telegram/bot/configuration'
6
+ require 'telegram/bot/exceptions'
7
+ require 'telegram/bot/api'
8
+ require 'telegram/bot/types'
9
+ require 'telegram/bot/client'
10
+ require 'telegram/bot/client_accessors'
11
+
12
+ module Telegram
13
+ module Bot
14
+ API_VERSION = 3.5.freeze
15
+ API_URL = 'https://api.telegram.org'.freeze
16
+
17
+ class << self
18
+ attr_writer :configuration
19
+ end
20
+
21
+ def self.configuration
22
+ @configuration ||= Configuration.new
23
+ end
24
+
25
+ def self.configure
26
+ yield(configuration)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,261 @@
1
+ module Telegram
2
+ module Bot
3
+ class Api
4
+ def initialize(token = nil)
5
+ @token = token || Bot.configuration.token
6
+ end
7
+
8
+ def token
9
+ @token
10
+ end
11
+
12
+ def getMe
13
+ request('getMe', {}, Types::User)
14
+ end
15
+
16
+ def setWebhook(url, options = {})
17
+ request('setWebhook', {url: url}.merge(options))
18
+ end
19
+
20
+ def deleteWebhook
21
+ request('deleteWebhook')
22
+ end
23
+
24
+ def getWebhookInfo
25
+ request('getWebhookInfo', {}, Types::WebhookInfo)
26
+ end
27
+
28
+ def getUpdates(options = {})
29
+ request('getWebhookInfo', options, Types::Update, true)
30
+ end
31
+
32
+ def getFile(file_id)
33
+ request('getFile', {file_id: file_id}, Types::File)
34
+ end
35
+
36
+ def sendMessage(chat_id, text, options = {})
37
+ request('sendMessage', {chat_id: chat_id, text: text}.merge(options), Types::Message)
38
+ end
39
+
40
+ def forwardMessage(chat_id, from_chat_id, message_id, options = {})
41
+ request('forwardMessage', {
42
+ chat_id: chat_id, from_chat_id: from_chat_id, message_id: message_id
43
+ }.merge(options), Types::Message)
44
+ end
45
+
46
+ def sendPhoto(chat_id, photo, options = {})
47
+ request('sendPhoto', {chat_id: chat_id, photo: photo}.merge(options), Types::Message)
48
+ end
49
+
50
+ def sendAudio(chat_id, audio, options = {})
51
+ request('sendAudio', {chat_id: chat_id, audio: audio}.merge(options), Types::Message)
52
+ end
53
+
54
+ def sendDocument(chat_id, document, options = {})
55
+ request('sendDocument', {chat_id: chat_id, document: document}.merge(options), Types::Message)
56
+ end
57
+
58
+ def sendVideo(chat_id, video, options = {})
59
+ request('sendVideo', {chat_id: chat_id, video: video}.merge(options), Types::Message)
60
+ end
61
+
62
+ def sendVoice(chat_id, voice, options = {})
63
+ request('sendVoice', {chat_id: chat_id, voice: voice}.merge(options), Types::Message)
64
+ end
65
+
66
+ def sendMediaGroup(chat_id, video_note, options = {})
67
+ request('sendMediaGroup', {chat_id: chat_id, video_note: video_note}.merge(options), Types::Message)
68
+ end
69
+
70
+ def sendVideoNote(chat_id, media, options = {})
71
+ request('sendVideoNote', {chat_id: chat_id, media: media}.merge(options), Types::Message, true)
72
+ end
73
+
74
+ def sendLocation(chat_id, latitude, longitude, options = {})
75
+ request('sendLocation', {
76
+ chat_id: chat_id, latitude: latitude, longitude: longitude
77
+ }.merge(options), Types::Message)
78
+ end
79
+
80
+ def editMessageLiveLocation(latitude, longitude, options = {})
81
+ request('editMessageLiveLocation', {latitude: latitude, longitude: longitude}.merge(options), Types::Message)
82
+ end
83
+
84
+ def stopMessageLiveLocation(options = {})
85
+ request('stopMessageLiveLocation', options, Types::Message)
86
+ end
87
+
88
+ def sendVenue(chat_id, latitude, longitude, title, address, options = {})
89
+ request('sendVenue', {
90
+ chat_id: chat_id, latitude: latitude, longitude: longitude, title: title, address: address
91
+ }.merge(options), Types::Message)
92
+ end
93
+
94
+ def sendContact(chat_id, phone_number, first_name, options = {})
95
+ request('sendContact', {
96
+ chat_id: chat_id, phone_number: phone_number, first_name: first_name
97
+ }.merge(options), Types::Message)
98
+ end
99
+
100
+ def sendChatAction(chat_id, action, options = {})
101
+ request('sendChatAction', {chat_id: chat_id, action: action}.merge(options))
102
+ end
103
+
104
+ def getUserProfilePhotos(user_id, options = {})
105
+ request('getUserProfilePhotos', {user_id: user_id}.merge(options), Types::UserProfilePhotos)
106
+ end
107
+
108
+ def kickChatMember(chat_id, user_id, options = {})
109
+ request('kickChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
110
+ end
111
+
112
+ def unbanChatMember(chat_id, user_id, options = {})
113
+ request('unbanChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
114
+ end
115
+
116
+ def promoteChatMember(chat_id, user_id, options = {})
117
+ request('promoteChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
118
+ end
119
+
120
+ def restrictChatMember(chat_id, user_id, options = {})
121
+ request('restrictChatMember', {chat_id: chat_id, user_id: user_id}.merge(options))
122
+ end
123
+
124
+ def exportChatInviteLink(chat_id)
125
+ request('exportChatInviteLink', {chat_id: chat_id})
126
+ end
127
+
128
+ def setChatPhoto(chat_id, photo)
129
+ request('setChatPhoto', {chat_id: chat_id, photo: photo})
130
+ end
131
+
132
+ def deleteChatPhoto(chat_id)
133
+ request('deleteChatPhoto', {chat_id: chat_id})
134
+ end
135
+
136
+ def setChatTitle(chat_id, title)
137
+ request('setChatTitle', {chat_id: chat_id, title: title})
138
+ end
139
+
140
+ def setChatDescription(chat_id, options = {})
141
+ request('setChatDescription', {chat_id: chat_id}.merge(options))
142
+ end
143
+
144
+ def pinChatMessage(chat_id, message_id, options = {})
145
+ request('pinChatMessage', {chat_id: chat_id, message_id: message_id}.merge(options))
146
+ end
147
+
148
+ def unpinChatMessage(chat_id)
149
+ request('unpinChatMessage', {chat_id: chat_id})
150
+ end
151
+
152
+ def leaveChat(chat_id)
153
+ request('leaveChat', {chat_id: chat_id})
154
+ end
155
+
156
+ def getChat(user_id)
157
+ request('getChat', {chat_id: user_id}, Types::Chat)
158
+ end
159
+
160
+ def getChatAdministrators(chat_id)
161
+ request('getChatAdministrators', {chat_id: chat_id}, Types::ChatMember, true)
162
+ end
163
+
164
+ def getChatMembersCount(chat_id)
165
+ request('getChatMembersCount', {chat_id: chat_id})
166
+ end
167
+
168
+ def getChatMember(chat_id, user_id)
169
+ request('getChatMember', {chat_id: chat_id, user_id: user_id}, Types::ChatMember)
170
+ end
171
+
172
+ def setChatStickerSet(chat_id, sticker_set_name)
173
+ request('setChatStickerSet', {chat_id: chat_id, sticker_set_name: sticker_set_name})
174
+ end
175
+
176
+ def deleteChatStickerSet(chat_id)
177
+ request('deleteChatStickerSet', {chat_id: chat_id})
178
+ end
179
+
180
+ def answerCallbackQuery(callback_query_id, options = {})
181
+ request('answerCallbackQuery', {callback_query_id: callback_query_id}.merge(options))
182
+ end
183
+
184
+ def editMessageText(text, options = {})
185
+ request('editMessageText', {text: text}.merge(options), Types::Message)
186
+ end
187
+
188
+ def editMessageCaption(options = {})
189
+ request('editMessageCaption', options, Types::Message)
190
+ end
191
+
192
+ def editMessageReplyMarkup(options = {})
193
+ request('editMessageReplyMarkup', options, Types::Message)
194
+ end
195
+
196
+ def deleteMessage(chat_id, message_id)
197
+ request('deleteMessage', {chat_id: chat_id, message_id: message_id})
198
+ end
199
+
200
+ def sendSticker(chat_id, sticker, options = {})
201
+ request('sendSticker', {chat_id: chat_id, sticker: sticker}.merge(options), Types::Message)
202
+ end
203
+
204
+ def getStickerSet(name)
205
+ request('getStickerSet', {name: name}, Types::StickerSet)
206
+ end
207
+
208
+ def uploadStickerFile(user_id, png_sticker)
209
+ request('uploadStickerFile', {user_id: user_id, png_sticker: png_sticker}, Types::File)
210
+ end
211
+
212
+ def createNewStickerSet(user_id, name, title, png_sticker, emojis, options = {})
213
+ request('createNewStickerSet', {
214
+ user_id: user_id, name: name, title: title, png_sticker: png_sticker, emojis: emojis
215
+ }.merge(options))
216
+ end
217
+
218
+ def addStickerToSet(user_id, name, png_sticker, emojis, options = {})
219
+ request('addStickerToSet', {
220
+ user_id: user_id, name: name, png_sticker: png_sticker, emojis: emojis
221
+ }.merge(options))
222
+ end
223
+
224
+ def setStickerPositionInSet(sticker, position)
225
+ request('setStickerPositionInSet', {sticker: sticker, position: position})
226
+ end
227
+
228
+ def deleteStickerFromSet(sticker)
229
+ request('deleteStickerFromSet', {sticker: sticker})
230
+ end
231
+
232
+ def request(method, query = {}, type_obj = nil, as_array = false)
233
+ query[:reply_markup] = query[:reply_markup].to_json if query.has_key?(:reply_markup)
234
+
235
+ response = connection.post("/bot#{@token}/#{method}", query)
236
+ body = response.body
237
+
238
+ if Bot.configuration.raise_exceptions && (response.status != 200 || body['ok'] != true)
239
+ raise Exceptions::ResponseError.new(body['description'])
240
+ end
241
+
242
+ if type_obj.nil?
243
+ body['result']
244
+ elsif as_array
245
+ body['result'].map {|r| type_obj.new(r) }
246
+ else
247
+ type_obj.new(body['result'])
248
+ end
249
+ end
250
+
251
+ def connection
252
+ @conn ||= Faraday.new(url: API_URL) do |faraday|
253
+ faraday.response :json, content_type: /\bjson$/
254
+ faraday.request :multipart
255
+ faraday.request :url_encoded
256
+ faraday.adapter Faraday.default_adapter
257
+ end
258
+ end
259
+ end
260
+ end
261
+ end