telegramAPI 1.0.1 → 1.0.2b
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +72 -0
- data/lib/telegramAPI.rb +75 -18
- data/lib/telegramObjects.rb +17 -0
- metadata +24 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89e16e0cd37195648280e189febf1a7c5b250527
|
4
|
+
data.tar.gz: d478828f86e3181ba3e43337ee7a12fa90ceeace
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9700f259459eb019acaf2cce1ccaddb47782818140fafcb111caf6b877240502a7774df2232ffd54b84dbe25d048eb37a0ebfd34641df3e3d461144a9c816d6c
|
7
|
+
data.tar.gz: d657f246416f0aaa84af591e170c18b68a352695e35ca2034c6d1d578c146e4bc9ddea063f40bd133da8a1b692fefc9251257c5c4d85e6c286e9f3591204d07f
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# TelegramAPI
|
2
|
+
|
3
|
+
This is a simple and lightweight Ruby API for Telegram Bots.
|
4
|
+
|
5
|
+
With this tiny library you can create awesome Telegram Bot!
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```
|
10
|
+
sudo gem install telegramAPI
|
11
|
+
```
|
12
|
+
|
13
|
+
## Use
|
14
|
+
|
15
|
+
Import the library in your script with:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'telegramAPI'
|
19
|
+
```
|
20
|
+
|
21
|
+
Obtain a token, if you haven't yet, talking with [@BotFather](https://telegram.me/botfather)
|
22
|
+
|
23
|
+
## Getting Started
|
24
|
+
|
25
|
+
To test your access token, you can use the *getMe* method
|
26
|
+
```ruby
|
27
|
+
require 'telegramAPI'
|
28
|
+
|
29
|
+
token = "******"
|
30
|
+
api = TelegramAPI.new token
|
31
|
+
bot = api.getMe
|
32
|
+
puts "I'm bot #{bot.first_name} with id #{bot.id}"
|
33
|
+
puts "But you can call me @#{bot.username}"
|
34
|
+
```
|
35
|
+
|
36
|
+
## Documentation
|
37
|
+
|
38
|
+
The complete documentation can be found at [www.rubydoc.info](http://www.rubydoc.info/gems/telegramAPI/)
|
39
|
+
|
40
|
+
|
41
|
+
## Examples
|
42
|
+
|
43
|
+
### Echo Server
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
token = "******"
|
47
|
+
api = TelegramAPI.new token
|
48
|
+
while true do
|
49
|
+
# Get last messages if there are, or wait 180 seconds for new messages
|
50
|
+
u=api.getUpdates {timeout=>180}
|
51
|
+
u.each do |m|
|
52
|
+
api.sendMessage(m.message.chat.id, m.message.text)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### Send Media
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
api.sendSticker m.message.chat.id, sticker_id
|
61
|
+
|
62
|
+
api.sendPhoto m.message.chat.id, "/home/path-of-image/image.jpg"
|
63
|
+
|
64
|
+
api.sendDocument m.message.chat.id, "/home/path-of-document/doc.gif"
|
65
|
+
|
66
|
+
api.sendAudio m.message.chat.id, "/home/path-of-audio/audio.opus"
|
67
|
+
|
68
|
+
api.sendVideo m.message.chat.id, "/home/path-of-video/video.mp4"
|
69
|
+
|
70
|
+
api.sendLocation m.message.chat.id, 45.462781, 9.177732
|
71
|
+
```
|
72
|
+
**Note:** According to Telegram, each audio must be encoded in **Ogg OPUS**, and each video must be encoded in **mp4**.
|
data/lib/telegramAPI.rb
CHANGED
@@ -3,17 +3,25 @@ require 'json'
|
|
3
3
|
require 'net/http'
|
4
4
|
require 'net/https'
|
5
5
|
require 'uri'
|
6
|
-
require 'rest-client'
|
6
|
+
require 'rest-client'
|
7
7
|
require 'telegramObjects'
|
8
8
|
|
9
|
+
# This library provides an easy way to access to the Telegram Bot API
|
10
|
+
# Author:: Benedetto Nespoli
|
11
|
+
# License:: MIT
|
12
|
+
|
9
13
|
class TelegramAPI
|
10
14
|
@@core = "https://api.telegram.org/bot"
|
11
15
|
|
12
|
-
|
13
|
-
|
16
|
+
# Create a new instance of TelegramAPI
|
17
|
+
#
|
18
|
+
# @param token [String] the access token, obtained thanks to {https://telegram.me/botfather @BotFather} on Telegram.
|
19
|
+
def initialize token
|
20
|
+
@token = token
|
14
21
|
@last_update = 0
|
15
22
|
end
|
16
23
|
|
24
|
+
private
|
17
25
|
def query api, params={}
|
18
26
|
p=[]
|
19
27
|
params_s=""
|
@@ -23,12 +31,18 @@ class TelegramAPI
|
|
23
31
|
|
24
32
|
JSON.parse(open(@@core+@token+"/"+api+params_s).read)
|
25
33
|
end
|
34
|
+
public
|
26
35
|
|
36
|
+
# Provide information about the bot itself
|
37
|
+
# @return [User] Information about the bot
|
27
38
|
def getMe
|
28
39
|
User.new self.query("getMe")
|
29
40
|
end
|
30
41
|
|
31
|
-
|
42
|
+
# Get last updates, including last received messages
|
43
|
+
# @param options [Hash<String, String>] Optional settings
|
44
|
+
# @return [Array<Update>] List of all updates
|
45
|
+
def getUpdates options={"timeout"=>0, "limit"=>100}
|
32
46
|
r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(options)
|
33
47
|
if r['ok']!=true then return nil end
|
34
48
|
up=ArrayOf.new(r['result'],Update).to_a
|
@@ -36,52 +50,95 @@ class TelegramAPI
|
|
36
50
|
return up
|
37
51
|
end
|
38
52
|
|
53
|
+
# Send a message to the user with id +to+, with the text +text+
|
54
|
+
# @param to [Integer] chat_id to which send the message. Usually message.chat.id
|
55
|
+
# @param text [String] The text to send
|
56
|
+
# @param options (see #getUpdates)
|
57
|
+
# @return [Message] Message with the Photo sent
|
39
58
|
def sendMessage to, text, options={}
|
40
|
-
|
59
|
+
if options.has_key?"reply_markup" then
|
60
|
+
options["reply_markup"]=options["reply_markup"].to_json
|
61
|
+
end
|
62
|
+
Message.new self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(options))["result"]
|
41
63
|
end
|
42
64
|
|
65
|
+
# Send a message as forwarded
|
66
|
+
# @param to (see #sendMessage)
|
67
|
+
# @param from [Integer] chat_id of the original message.
|
68
|
+
# @param msg [Integer] The message_id of the original message
|
69
|
+
# @return (see #sendPhoto)
|
43
70
|
def forwardMessage to, from, msg
|
44
|
-
Message.new self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})
|
71
|
+
Message.new self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})["result"]
|
45
72
|
end
|
46
73
|
|
74
|
+
# Send a local file containing a photo
|
75
|
+
# @param to (see #sendMessage)
|
76
|
+
# @param path [String] The path of the file to send
|
77
|
+
# @param options (see #sendMessage)
|
78
|
+
# @return (see #sendMessage)
|
47
79
|
def sendPhoto to, path, options={}
|
48
80
|
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendPhoto", {:photo=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
|
49
81
|
end
|
50
82
|
|
51
|
-
#
|
52
|
-
#
|
83
|
+
# Send an audio file in Ogg OPUS format of max 50MB
|
84
|
+
# @param (see #sendPhoto)
|
85
|
+
# @return (see #sendPhoto)
|
53
86
|
def sendAudio to, path, options={}
|
54
|
-
RestClient.post
|
87
|
+
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendAudio", {:audio=>File.new(path, 'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
|
55
88
|
end
|
56
89
|
|
90
|
+
# Send a general document (file, image, audio)
|
91
|
+
# @param (see #sendPhoto)
|
92
|
+
# @return (see #sendPhoto)
|
57
93
|
def sendDocument to, path, options={}
|
58
|
-
RestClient.post
|
94
|
+
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendDocument", {:document=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
|
59
95
|
end
|
60
96
|
|
97
|
+
# Send a Sticker from File
|
98
|
+
# @param (see #sendPhoto)
|
99
|
+
# @return (see #sendSticker)
|
61
100
|
def sendStickerFromFile to, path, options={}
|
62
|
-
RestClient.post
|
101
|
+
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendStiker", {:sticker=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
|
63
102
|
end
|
64
103
|
|
104
|
+
# Send a Sticker through its ID
|
105
|
+
# @param to (see #sendPhoto)
|
106
|
+
# @param id [Integer] the ID of the sticker to send
|
107
|
+
# @param options (see #sendPhoto)
|
108
|
+
# @return (see #sendPhoto)
|
65
109
|
def sendSticker to, id, options={}
|
66
|
-
RestClient.post
|
110
|
+
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(options)).body)["result"]
|
67
111
|
end
|
68
112
|
|
69
|
-
#
|
70
|
-
#
|
113
|
+
# Send a video file in mp4 format of max 50MB
|
114
|
+
# @param (see #sendPhoto)
|
115
|
+
# @return (see #sendPhoto)
|
71
116
|
def sendVideo to, path, options={}
|
72
|
-
RestClient.post
|
117
|
+
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendVideo", {:video=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
|
73
118
|
end
|
74
119
|
|
120
|
+
# Send a location
|
121
|
+
# @param to (see #sendPhoto)
|
122
|
+
# @param lat [Float] Latitude
|
123
|
+
# @param long [Float] Longitude
|
124
|
+
# @param options (see #sendPhoto)
|
125
|
+
# @return (see #sendPhoto)
|
75
126
|
def sendLocation to, lat, long, options={}
|
76
|
-
self.query
|
127
|
+
Message.new self.query("sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge(options))["result"]
|
77
128
|
end
|
78
129
|
|
79
|
-
#
|
130
|
+
# Send a Chat Action
|
131
|
+
# @param to (see #sendPhoto)
|
132
|
+
# @param act [String] One of: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location
|
80
133
|
def sendChatAction to, act
|
81
134
|
self.query "sendChatAction", {"chat_id"=>to, "action"=>act}
|
82
135
|
end
|
83
136
|
|
137
|
+
# Get a list of user profile photos, in up to 4 sizes each
|
138
|
+
# @param id [Integer] ID user whom getting the photos
|
139
|
+
# @param options (see #sendPhoto)
|
140
|
+
# @return [UserProfilePhotos]
|
84
141
|
def getUserProfilePhotos id, options={}
|
85
|
-
self.query
|
142
|
+
UserProfilePhotos.new self.query("getUserProfilePhotos", {"user_id"=>id}.merge(options))["result"]
|
86
143
|
end
|
87
144
|
end
|
data/lib/telegramObjects.rb
CHANGED
@@ -7,6 +7,7 @@ class Update
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
# Object describing a Bot or User
|
10
11
|
class User
|
11
12
|
attr_accessor :id, :first_name, :last_name, :username
|
12
13
|
def initialize json
|
@@ -18,6 +19,7 @@ class User
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
22
|
+
# Object describing a Chat Group
|
21
23
|
class GroupChat
|
22
24
|
attr_accessor :id, :title
|
23
25
|
def initialize json
|
@@ -43,6 +45,7 @@ class ArrayOf
|
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
48
|
+
# Object describing a Message
|
46
49
|
class Message
|
47
50
|
attr_accessor :message_id, :from, :date, :chat, :forward_from, :forward_date,
|
48
51
|
:reply_to_message, :text, :audio, :document, :photo, :sticker, :video,
|
@@ -75,6 +78,7 @@ class Message
|
|
75
78
|
end
|
76
79
|
end
|
77
80
|
|
81
|
+
# Object Describing a Photo or Sticker
|
78
82
|
class PhotoSize
|
79
83
|
attr_accessor :file_id, :width, :height, :file_size
|
80
84
|
def initialize json
|
@@ -156,6 +160,7 @@ class Location
|
|
156
160
|
end
|
157
161
|
end
|
158
162
|
|
163
|
+
# Object describing a list of photos in up to 4 sizes each
|
159
164
|
class UserProfilePhotos
|
160
165
|
attr_accessor :total_count, :photos
|
161
166
|
def initialize json
|
@@ -177,6 +182,10 @@ class ReplyKeyboardMarkup
|
|
177
182
|
@one_time_keyboard = json["one_time_keyboard"]
|
178
183
|
@selective = json["selective"]
|
179
184
|
end
|
185
|
+
|
186
|
+
def to_json
|
187
|
+
"{\"keyboard\":" + @keyboard.to_json + ", \"resize_keyboard\":" + (!@resize_keyboard ? "false" : "true") + ", \"one_time_keyboard\":" + (!@one_time_keyboard ? "false" : "true") + ", \"selective\":" + (!@selective ? "false" : "true") + "}"
|
188
|
+
end
|
180
189
|
end
|
181
190
|
|
182
191
|
class ReplyKeyboardHide
|
@@ -186,6 +195,10 @@ class ReplyKeyboardHide
|
|
186
195
|
@hide_keyboard = json["hide_keyboard"]
|
187
196
|
@selective = json["selective"]
|
188
197
|
end
|
198
|
+
|
199
|
+
def to_json
|
200
|
+
"{\"hide_keyboard\":true, \"selective\":"+(!@selective ? "false" : "true")+"}"
|
201
|
+
end
|
189
202
|
end
|
190
203
|
|
191
204
|
class ForceReply
|
@@ -195,4 +208,8 @@ class ForceReply
|
|
195
208
|
@force_reply = json["force_reply"]
|
196
209
|
@selective = json["selective"]
|
197
210
|
end
|
211
|
+
|
212
|
+
def to_json
|
213
|
+
"{\"force_reply\":true, \"selective\":"+(!@selective ? "false" : "true")+"}"
|
214
|
+
end
|
198
215
|
end
|
metadata
CHANGED
@@ -1,24 +1,39 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegramAPI
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2b
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benedetto Nespoli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2015-07-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A lightweight Ruby API for Telegram Bots
|
14
28
|
email: benedetto.nespoli@gmail.com
|
15
29
|
executables: []
|
16
30
|
extensions: []
|
17
31
|
extra_rdoc_files: []
|
18
32
|
files:
|
33
|
+
- README.md
|
19
34
|
- lib/telegramAPI.rb
|
20
35
|
- lib/telegramObjects.rb
|
21
|
-
homepage:
|
36
|
+
homepage: https://github.com/bennesp/telegramAPI
|
22
37
|
licenses:
|
23
38
|
- MIT
|
24
39
|
metadata: {}
|
@@ -33,13 +48,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
48
|
version: '0'
|
34
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
50
|
requirements:
|
36
|
-
- - "
|
51
|
+
- - ">"
|
37
52
|
- !ruby/object:Gem::Version
|
38
|
-
version:
|
53
|
+
version: 1.3.1
|
39
54
|
requirements: []
|
40
55
|
rubyforge_project:
|
41
56
|
rubygems_version: 2.2.2
|
42
57
|
signing_key:
|
43
58
|
specification_version: 4
|
44
|
-
summary: Telegram
|
59
|
+
summary: Telegram API for Bots
|
45
60
|
test_files: []
|
61
|
+
has_rdoc:
|