telegruby 1.1.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 +7 -0
- data/lib/telegruby.rb +293 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 53358132510ab2ef5f34d7faa20e13c08730dab2
|
4
|
+
data.tar.gz: 4b6a9166d16611fe3bbba3cbae8f9c20edea5fb9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb80c64134254302b7dc3b4dcbc01c4565c49355b222bc887d9f5e50a3fcd08aafd5f3cef07add30e4831f17774f9b0b69de2da1e9fc8e62bc42f836f5614023
|
7
|
+
data.tar.gz: 98c557ece1450f7b9b8d3f5990dec71268f24dd816d2fc0a7bc5b3242af5bf271abbfbc33a13db885a19fc712d179fde0f8be6656b2870c3c4fd62cb273b4304
|
data/lib/telegruby.rb
ADDED
@@ -0,0 +1,293 @@
|
|
1
|
+
require 'httmultiparty'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Telegruby
|
5
|
+
class Bot
|
6
|
+
attr_accessor :id
|
7
|
+
|
8
|
+
def initialize(api_token)
|
9
|
+
@token = api_token
|
10
|
+
@endpoint = "https://api.telegram.org/bot#{@token}/"
|
11
|
+
@id = 0
|
12
|
+
end
|
13
|
+
|
14
|
+
# See https://core.telegram.org/bots/api for the full method list
|
15
|
+
|
16
|
+
def get_updates(options = {}, error_callback = nil)
|
17
|
+
if options == {}
|
18
|
+
options = {
|
19
|
+
:offset => @id
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
request = self.get_request("getUpdates", options)
|
24
|
+
if request.code != 200
|
25
|
+
return error_callback request
|
26
|
+
end
|
27
|
+
update = Update.new(JSON.parse(request.body, object_class: OpenStruct))
|
28
|
+
|
29
|
+
if !update.result.empty?
|
30
|
+
# there are updates, so mark them as 'seen'
|
31
|
+
@id = (update.result.last.update_id + 1)
|
32
|
+
end
|
33
|
+
|
34
|
+
return update
|
35
|
+
end
|
36
|
+
|
37
|
+
# Send a plaintext message to a chat id
|
38
|
+
def send_message(id, text, reply: nil)
|
39
|
+
options = {
|
40
|
+
:chat_id => id,
|
41
|
+
:text => text
|
42
|
+
}
|
43
|
+
|
44
|
+
if !reply.nil?
|
45
|
+
options.merge!(:reply_to_message_id => reply)
|
46
|
+
end
|
47
|
+
|
48
|
+
self.get_request("sendMessage", options)
|
49
|
+
end
|
50
|
+
|
51
|
+
def forward_message(id, from_id, message_id)
|
52
|
+
options = {
|
53
|
+
:chat_id => id,
|
54
|
+
:from_chat_id => from_id,
|
55
|
+
:message_id => message_id
|
56
|
+
}
|
57
|
+
|
58
|
+
self.get_request("forwardMessage", options)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Sends a photo message to a chat id
|
62
|
+
def send_photo(id, filename: nil, file_id: nil, reply: nil)
|
63
|
+
options = {
|
64
|
+
:chat_id => id,
|
65
|
+
}
|
66
|
+
if file_id
|
67
|
+
options.merge!(:photo => file_id)
|
68
|
+
else
|
69
|
+
options.merge!(:photo => File.new(filename))
|
70
|
+
end
|
71
|
+
|
72
|
+
if !reply.nil?
|
73
|
+
options.merge!(:reply_to_message_id => reply)
|
74
|
+
end
|
75
|
+
|
76
|
+
self.post_request("sendPhoto", options)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Sends a photo from a byte string
|
80
|
+
def send_photo_bytestring(id, str, reply: nil)
|
81
|
+
Tempfile.open(["img", ".jpg"]) { |f|
|
82
|
+
f.binmode
|
83
|
+
f.write(str)
|
84
|
+
|
85
|
+
self.send_photo(id, f.path, reply)
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
def send_voice(id, filename: nil, file_id: nil, reply: nil)
|
90
|
+
options = {
|
91
|
+
:chat_id => id,
|
92
|
+
}
|
93
|
+
if file_id
|
94
|
+
options.merge!(:audio => file_id)
|
95
|
+
else
|
96
|
+
options.merge!(:audio => File.new(filename))
|
97
|
+
end
|
98
|
+
|
99
|
+
if !reply.nil?
|
100
|
+
options.merge!(:reply_to_message_id => reply)
|
101
|
+
end
|
102
|
+
|
103
|
+
self.post_request("sendVoice", options)
|
104
|
+
end
|
105
|
+
|
106
|
+
def send_sticker(id, file_id: nil, filename: nil, reply: nil)
|
107
|
+
options = {
|
108
|
+
:chat_id => id,
|
109
|
+
}
|
110
|
+
if file_id
|
111
|
+
options.merge!(:sticker => file_id)
|
112
|
+
else
|
113
|
+
options.merge!(:sticker => File.new(filename))
|
114
|
+
end
|
115
|
+
|
116
|
+
if !reply.nil?
|
117
|
+
options.merge!(:reply_to_message_id => reply)
|
118
|
+
end
|
119
|
+
|
120
|
+
self.post_request("sendSticker", options)
|
121
|
+
end
|
122
|
+
|
123
|
+
def send_audio(id, filename: nil, file_id: nil, reply: nil)
|
124
|
+
options = {
|
125
|
+
:chat_id => id,
|
126
|
+
}
|
127
|
+
if file_id
|
128
|
+
options.merge!(:audio => file_id)
|
129
|
+
else
|
130
|
+
options.merge!(:audio => File.new(filename))
|
131
|
+
end
|
132
|
+
|
133
|
+
if !reply.nil?
|
134
|
+
options.merge!(:reply_to_message_id => reply)
|
135
|
+
end
|
136
|
+
|
137
|
+
self.post_request("sendAudio", options)
|
138
|
+
end
|
139
|
+
|
140
|
+
def send_video(id, filename: nil, file_id: nil, reply: nil)
|
141
|
+
options = {
|
142
|
+
:chat_id => id,
|
143
|
+
}
|
144
|
+
if file_id
|
145
|
+
options.merge!(:video => file_id)
|
146
|
+
else
|
147
|
+
options.merge!(:video => File.new(filename))
|
148
|
+
end
|
149
|
+
|
150
|
+
if !reply.nil?
|
151
|
+
options.merge!(:reply_to_message_id => reply)
|
152
|
+
end
|
153
|
+
|
154
|
+
self.post_request("sendVideo", options)
|
155
|
+
end
|
156
|
+
|
157
|
+
def send_location(id, lat, long, reply: nil)
|
158
|
+
options = {
|
159
|
+
:chat_id => id,
|
160
|
+
:latitude => lat,
|
161
|
+
:longitude => long
|
162
|
+
}
|
163
|
+
|
164
|
+
if !reply.nil?
|
165
|
+
options.merge!(:reply_to_message_id => reply)
|
166
|
+
end
|
167
|
+
|
168
|
+
self.post_request("sendLocation", options)
|
169
|
+
end
|
170
|
+
def send_action(id, action)
|
171
|
+
options = {
|
172
|
+
:chat_id => id,
|
173
|
+
:action => action
|
174
|
+
}
|
175
|
+
|
176
|
+
return self.post_request("sendChatAction", options)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Sends a document by filename or file ID
|
180
|
+
def send_document(id, filename: nil, file_id: nil, reply: nil)
|
181
|
+
options = {
|
182
|
+
:chat_id => id
|
183
|
+
}
|
184
|
+
|
185
|
+
if file_id
|
186
|
+
options.merge!(:document => file_id)
|
187
|
+
else
|
188
|
+
options.merge!(:document => File.new(filename))
|
189
|
+
end
|
190
|
+
|
191
|
+
if !reply.nil?
|
192
|
+
options.merge!(:reply_to_message_id => reply)
|
193
|
+
end
|
194
|
+
|
195
|
+
return self.post_request("sendDocument", options)
|
196
|
+
end
|
197
|
+
def get_userphoto(id, offset: nil, limit: nil)
|
198
|
+
options = {
|
199
|
+
:user_id => id,
|
200
|
+
:offset => offset,
|
201
|
+
:limit => limit
|
202
|
+
}
|
203
|
+
|
204
|
+
return self.get_request("getUserProfilePhotos", options)
|
205
|
+
end
|
206
|
+
|
207
|
+
def set_webhook(url = nil, certificate = nil)
|
208
|
+
options = {
|
209
|
+
:url => nil,
|
210
|
+
}
|
211
|
+
if certificate
|
212
|
+
options.merge!(:certificate => File.new(certificate))
|
213
|
+
end
|
214
|
+
|
215
|
+
return self.get_request("setWebhook", options)
|
216
|
+
end
|
217
|
+
|
218
|
+
protected
|
219
|
+
|
220
|
+
# Provides a generic method for GET requests.
|
221
|
+
def get_request(name, options = {})
|
222
|
+
HTTMultiParty::get(@endpoint + name, query: options)
|
223
|
+
end
|
224
|
+
|
225
|
+
# Provides a generic method for POST requests.
|
226
|
+
def post_request(name, options = {})
|
227
|
+
HTTMultiParty::post(@endpoint + name, query: options).body
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
module_function
|
232
|
+
|
233
|
+
# Reads an API token from a JSON file.
|
234
|
+
# The result of this should be
|
235
|
+
# passed to Telegruby::Bot.new
|
236
|
+
def read_config(filename)
|
237
|
+
begin
|
238
|
+
data = JSON.parse(File.read(filename))
|
239
|
+
|
240
|
+
if !data.key? "token"
|
241
|
+
return false
|
242
|
+
end
|
243
|
+
|
244
|
+
return data
|
245
|
+
rescue
|
246
|
+
return false
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
# Given an Update object, gets messages as structs.
|
251
|
+
def collect_msgs(update)
|
252
|
+
update.result.map { |msg|
|
253
|
+
Message.new(msg)
|
254
|
+
}
|
255
|
+
end
|
256
|
+
|
257
|
+
# Update object generated by
|
258
|
+
# Telegruby::Bot::get_updates
|
259
|
+
class Update < OpenStruct
|
260
|
+
def results?
|
261
|
+
!self.result.empty?
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
# Message structure generated by
|
266
|
+
# collect_msgs from an update hash.
|
267
|
+
# Has some convenience methods.
|
268
|
+
class Message < OpenStruct
|
269
|
+
def initialize(hash_msg)
|
270
|
+
super(hash_msg)
|
271
|
+
end
|
272
|
+
|
273
|
+
def timestamp
|
274
|
+
self.message.date
|
275
|
+
end
|
276
|
+
|
277
|
+
def older_than?(secs)
|
278
|
+
((Time.now.to_i - self.timestamp) > secs)
|
279
|
+
end
|
280
|
+
|
281
|
+
def chat_id
|
282
|
+
self.message.chat.id
|
283
|
+
end
|
284
|
+
|
285
|
+
def body
|
286
|
+
self.message.text
|
287
|
+
end
|
288
|
+
|
289
|
+
def message_id
|
290
|
+
self.message.message_id
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: telegruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- '"Lain"'
|
8
|
+
- Rei
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-07-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Basic Telegram Bot API library, used for a few bots in the wild.
|
15
|
+
email: lain@lain.org.uk
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/telegruby.rb
|
21
|
+
homepage: https://github.com/telegram-wired/telegruby
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.4.5.1
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Basic Telegram Bot API library.
|
45
|
+
test_files: []
|