telegramAPI 1.0.17 → 1.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 +4 -4
- data/README.md +5 -6
- data/lib/telegramAPI.rb +15 -43
- metadata +3 -4
- data/lib/telegramObjects.rb +0 -215
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4d4ed6e7f6b6e7bab1bd1fccfe9ba95db51ed19
|
4
|
+
data.tar.gz: fdd8295ba7c49a574ccf848fcaceaa5ce0529288
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5001ef8a7d75604c7e7e03e6f8c069451b50d284928ac630ae35ea0a841846de17b9fd53172f4a3f03f387a35430db6cc4f76bc857edca5b09effc314c17198
|
7
|
+
data.tar.gz: 860db86abaf052d0722ebe85899d586457f22e8f611526211e759c4c4b7ca1986949f880591a10754911b8a5b2ccaea97df294a771d17090f4bcafe0f58e8d85
|
data/README.md
CHANGED
@@ -33,14 +33,13 @@ require 'telegramAPI'
|
|
33
33
|
token = "******"
|
34
34
|
api = TelegramAPI.new token
|
35
35
|
bot = api.getMe
|
36
|
-
puts "I'm bot #{bot
|
37
|
-
puts "But you can call me @#{bot
|
36
|
+
puts "I'm bot #{bot['first_name']} with id #{bot['id']}"
|
37
|
+
puts "But you can call me @#{bot['username']}"
|
38
38
|
```
|
39
39
|
|
40
40
|
## Documentation
|
41
41
|
|
42
|
-
Here you can
|
43
|
-
|
42
|
+
Here you can use the Telegram official [documentation](https://core.telegram.org/bots/api#available-methods)
|
44
43
|
|
45
44
|
## Examples
|
46
45
|
|
@@ -53,7 +52,7 @@ while true do
|
|
53
52
|
# Get last messages if there are, or wait 180 seconds for new messages
|
54
53
|
u=api.getUpdates({"timeout"=>180})
|
55
54
|
u.each do |m|
|
56
|
-
api.sendMessage(m
|
55
|
+
api.sendMessage(m['message']['chat']['id'], m['message']['text'])
|
57
56
|
end
|
58
57
|
end
|
59
58
|
```
|
@@ -90,5 +89,5 @@ markup = {
|
|
90
89
|
# "hide_keyboard"=>true
|
91
90
|
}
|
92
91
|
|
93
|
-
api.sendMessage m
|
92
|
+
api.sendMessage m['message']['chat']['id'], "Am I sexy?", {"reply_markup"=>markup}
|
94
93
|
```
|
data/lib/telegramAPI.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'open-uri'
|
2
1
|
require 'json'
|
3
2
|
require 'net/http'
|
4
3
|
require 'net/https'
|
5
4
|
require 'uri'
|
6
|
-
require '
|
7
|
-
require '
|
5
|
+
require 'rest-client'
|
6
|
+
require 'ostruct'
|
8
7
|
|
9
8
|
# This library provides an easy way to access to the Telegram Bot API
|
10
9
|
# Author:: Benedetto Nespoli
|
@@ -13,9 +12,6 @@ require 'telegramObjects'
|
|
13
12
|
class TelegramAPI
|
14
13
|
@@core = "https://api.telegram.org/bot"
|
15
14
|
|
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
15
|
def initialize token
|
20
16
|
@token = token
|
21
17
|
@last_update = 0
|
@@ -36,63 +32,39 @@ class TelegramAPI
|
|
36
32
|
params.each do |param| p << param.join("=") end
|
37
33
|
params_s = "?#{p.join("&")}" if p.length!=0
|
38
34
|
|
39
|
-
JSON.parse(
|
35
|
+
JSON.parse(RestClient.get(@@core+@token+"/"+api+params_s).body)
|
40
36
|
end
|
41
37
|
|
42
38
|
def post api, name, path, to, options={}
|
43
|
-
|
39
|
+
JSON.parse(RestClient.post(@@core+@token+api, {name=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(parse_hash(options))).body, object_class: OpenStruct)["result"]
|
44
40
|
end
|
45
41
|
|
46
|
-
# Provide information about the bot itself
|
47
|
-
# @return [User] Information about the bot
|
48
|
-
def getMe
|
49
|
-
User.new self.query("getMe")
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get last updates, including last received messages
|
53
|
-
# @param options [Hash<String, String>] Optional settings
|
54
|
-
# @return [Array<Update>] List of all updates
|
55
42
|
def getUpdates options={"timeout"=>0, "limit"=>100}
|
56
43
|
r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(parse_hash(options))
|
57
44
|
if r['ok']!=true then return nil end
|
58
|
-
|
59
|
-
|
60
|
-
|
45
|
+
if r['result'][-1]!=nil then @last_update=r['result'][-1]['update_id']+1 end
|
46
|
+
return r['result']
|
47
|
+
end
|
48
|
+
|
49
|
+
def getMe
|
50
|
+
self.query("getMe")
|
61
51
|
end
|
62
52
|
|
63
|
-
# Send a message to the user with id +to+, with the text +text+
|
64
|
-
# @param to [Integer] chat_id to which send the message. Usually message.chat.id
|
65
|
-
# @param text [String] The text to send
|
66
|
-
# @param options (see #getUpdates)
|
67
|
-
# @return [Message] Message with the Photo sent
|
68
53
|
def sendMessage to, text, options={}
|
69
54
|
if options.has_key?"reply_markup" then
|
70
55
|
options["reply_markup"]=options["reply_markup"].to_json
|
71
56
|
end
|
72
|
-
|
57
|
+
self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(parse_hash(options)))["result"]
|
73
58
|
end
|
74
59
|
|
75
|
-
# Send a message as forwarded
|
76
|
-
# @param to (see #sendMessage)
|
77
|
-
# @param from [Integer] chat_id of the original message.
|
78
|
-
# @param msg [Integer] The message_id of the original message
|
79
|
-
# @return (see #sendPhoto)
|
80
60
|
def forwardMessage to, from, msg
|
81
|
-
|
61
|
+
self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})["result"]
|
82
62
|
end
|
83
63
|
|
84
|
-
# Send a local file containing a photo
|
85
|
-
# @param to (see #sendMessage)
|
86
|
-
# @param path [String] The path of the file to send
|
87
|
-
# @param options (see #sendMessage)
|
88
|
-
# @return (see #sendMessage)
|
89
64
|
def sendPhoto to, path, options={}
|
90
65
|
self.post "/sendPhoto", :photo, path, to, options
|
91
66
|
end
|
92
67
|
|
93
|
-
# Send an audio file in Ogg OPUS format of max 50MB
|
94
|
-
# @param (see #sendPhoto)
|
95
|
-
# @return (see #sendPhoto)
|
96
68
|
def sendAudio to, path, options={}
|
97
69
|
self.post "/sendAudio", :audio, path, to, options
|
98
70
|
end
|
@@ -117,7 +89,7 @@ class TelegramAPI
|
|
117
89
|
# @param options (see #sendPhoto)
|
118
90
|
# @return (see #sendPhoto)
|
119
91
|
def sendSticker to, id, options={}
|
120
|
-
|
92
|
+
JSON.parse(RestClient.post(@@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(parse_hash(options))).body)["result"]
|
121
93
|
end
|
122
94
|
|
123
95
|
# Send a video file in mp4 format of max 50MB
|
@@ -134,7 +106,7 @@ class TelegramAPI
|
|
134
106
|
# @param options (see #sendPhoto)
|
135
107
|
# @return (see #sendPhoto)
|
136
108
|
def sendLocation to, lat, long, options={}
|
137
|
-
|
109
|
+
self.query("sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge(parse_hash(options)))["result"]
|
138
110
|
end
|
139
111
|
|
140
112
|
# Send a Chat Action
|
@@ -149,7 +121,7 @@ class TelegramAPI
|
|
149
121
|
# @param options (see #sendPhoto)
|
150
122
|
# @return [UserProfilePhotos]
|
151
123
|
def getUserProfilePhotos id, options={}
|
152
|
-
|
124
|
+
self.query("getUserProfilePhotos", {"user_id"=>id}.merge(parse_hash(options)))["result"]
|
153
125
|
end
|
154
126
|
|
155
127
|
# Kick the user user_id from the chat chat_id
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegramAPI
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: '1.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benedetto Nespoli
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 1.7.3
|
33
|
-
description: A lightweight Ruby
|
33
|
+
description: A lightweight wrapper in Ruby for Telegram API Bots
|
34
34
|
email: benedetto.nespoli@gmail.com
|
35
35
|
executables: []
|
36
36
|
extensions: []
|
@@ -38,7 +38,6 @@ extra_rdoc_files: []
|
|
38
38
|
files:
|
39
39
|
- README.md
|
40
40
|
- lib/telegramAPI.rb
|
41
|
-
- lib/telegramObjects.rb
|
42
41
|
homepage: https://github.com/bennesp/telegramAPI
|
43
42
|
licenses:
|
44
43
|
- MIT
|
@@ -62,5 +61,5 @@ rubyforge_project:
|
|
62
61
|
rubygems_version: 2.5.1
|
63
62
|
signing_key:
|
64
63
|
specification_version: 4
|
65
|
-
summary: Telegram API for Bots
|
64
|
+
summary: Telegram API Wrapper for Bots
|
66
65
|
test_files: []
|
data/lib/telegramObjects.rb
DELETED
@@ -1,215 +0,0 @@
|
|
1
|
-
class Update
|
2
|
-
attr_accessor :update_id, :message
|
3
|
-
def initialize json
|
4
|
-
return if !json
|
5
|
-
@update_id = json["update_id"]
|
6
|
-
@message = Message.new json["message"]
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
# Object describing a Bot or User
|
11
|
-
class User
|
12
|
-
attr_accessor :id, :first_name, :last_name, :username
|
13
|
-
def initialize json
|
14
|
-
return if !json
|
15
|
-
@id = json["id"]
|
16
|
-
@first_name = json["first_name"]
|
17
|
-
@last_name = json["last_name"]
|
18
|
-
@username = json["username"]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Object describing a Chat Group
|
23
|
-
class GroupChat
|
24
|
-
attr_accessor :id, :title
|
25
|
-
def initialize json
|
26
|
-
return if !json
|
27
|
-
@id = json["id"]
|
28
|
-
@title = json["title"]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class ArrayOf
|
33
|
-
attr_accessor :array, :type
|
34
|
-
def initialize ar, cl
|
35
|
-
return if !ar
|
36
|
-
@array = []
|
37
|
-
@type = cl
|
38
|
-
ar.each do |e|
|
39
|
-
@array<<cl.new(e)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_a
|
44
|
-
@array
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
# Object describing a Message
|
49
|
-
class Message
|
50
|
-
attr_accessor :message_id, :from, :date, :chat, :forward_from, :forward_date,
|
51
|
-
:reply_to_message, :text, :audio, :document, :photo, :voice, :sticker, :video,
|
52
|
-
:contact, :location, :new_chat_participant, :left_chat_participant,
|
53
|
-
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_create
|
54
|
-
|
55
|
-
def initialize json
|
56
|
-
return if !json
|
57
|
-
@message_id = json["message_id"]
|
58
|
-
@from = User.new json["from"]
|
59
|
-
@date = json["date"]
|
60
|
-
@chat = !json["chat"] ? nil : (json["chat"].has_key?("title") ? GroupChat.new(json["chat"]) : User.new(json["chat"]))
|
61
|
-
@forward_from = User.new json["forward_from"]
|
62
|
-
@forward_date = json["forward_date"]
|
63
|
-
@reply_to_message = Message.new json["reply_to_message"]
|
64
|
-
@text = json["text"]
|
65
|
-
@audio = Audio.new json["audio"]
|
66
|
-
@document = Document.new json["document"]
|
67
|
-
@photo = ArrayOf.new(json["photo"], PhotoSize).to_a
|
68
|
-
@voice = Voice.new json["voice"]
|
69
|
-
@sticker = Sticker.new json["sticker"]
|
70
|
-
@video = Video.new json["video"]
|
71
|
-
@contact = Contact.new json["contact"]
|
72
|
-
@location = Location.new json["location"]
|
73
|
-
@new_chat_participant = User.new json["new_chat_participant"]
|
74
|
-
@left_chat_participant = User.new json["left_chat_participant"]
|
75
|
-
@new_chat_title = json["new_chat_title"]
|
76
|
-
@new_chat_photo = ArrayOf.new(json["new_chat_photo"],PhotoSize).to_a
|
77
|
-
@delete_chat_photo = json["delete_chat_photo"]
|
78
|
-
@group_chat_create = json["group_chat_create"]
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# Object Describing a Photo or Sticker
|
83
|
-
class PhotoSize
|
84
|
-
attr_accessor :file_id, :width, :height, :file_size
|
85
|
-
def initialize json
|
86
|
-
return if !json
|
87
|
-
@file_id = json["file_id"]
|
88
|
-
@width = json["width"]
|
89
|
-
@height = json["height"]
|
90
|
-
@file_size = json["file_size"]
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class Audio
|
95
|
-
attr_accessor :file_id, :duration, :mime_type, :file_size
|
96
|
-
def initialize json
|
97
|
-
return if !json
|
98
|
-
@file_id = json["file_id"]
|
99
|
-
@duration = json["duration"]
|
100
|
-
@mime_type = json["mime_type"]
|
101
|
-
@file_size = json["file_size"]
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
class Document
|
106
|
-
attr_accessor :file_id, :thumb, :file_name, :mime_type, :file_size
|
107
|
-
def initialize json
|
108
|
-
return if !json
|
109
|
-
@file_id = json["file_id"]
|
110
|
-
@thumb = PhotoSize.new json["thumb"]
|
111
|
-
@file_name = json["file_name"]
|
112
|
-
@mime_type = json["mime_type"]
|
113
|
-
@file_size = json["file_size"]
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
class Voice
|
118
|
-
attr_accessor :file_id, :duration, :mime_type, :file_size
|
119
|
-
def initialize json
|
120
|
-
return if !json
|
121
|
-
@file_id = json["file_id"]
|
122
|
-
@duration = json["duration"]
|
123
|
-
@mime_type = json["mime_type"]
|
124
|
-
@file_size = json["file_size"]
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
class Sticker
|
129
|
-
attr_accessor :file_id, :width, :height, :thumb, :file_size
|
130
|
-
def initialize json
|
131
|
-
return if !json
|
132
|
-
@file_id = json["file_id"]
|
133
|
-
@width = json["width"]
|
134
|
-
@height = json["height"]
|
135
|
-
@thumb = PhotoSize.new json["thumb"]
|
136
|
-
@file_size = json["file_size"]
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
class Video
|
141
|
-
attr_accessor :file_id, :width, :height, :duration, :thumb, :mime_type, :file_size, :caption
|
142
|
-
def initialize json
|
143
|
-
return if !json
|
144
|
-
@file_id = json["file_id"]
|
145
|
-
@width = json["width"]
|
146
|
-
@height = json["height"]
|
147
|
-
@duration = json["duration"]
|
148
|
-
@thumb = PhotoSize.new json["thumb"]
|
149
|
-
@mime_type = json["mime_type"]
|
150
|
-
@file_size = json["file_size"]
|
151
|
-
@caption = json["caption"]
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
class Contact
|
156
|
-
attr_accessor :phone_number, :first_name, :last_name, :user_id
|
157
|
-
def initialize json
|
158
|
-
return if !json
|
159
|
-
@phone_number = json["phone_number"]
|
160
|
-
@first_name = json["first_name"]
|
161
|
-
@last_name = json["last_name"]
|
162
|
-
@user_id = json["user_id"]
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
class Location
|
167
|
-
attr_accessor :latitude, :longitude
|
168
|
-
def initialize json
|
169
|
-
return if !json
|
170
|
-
@latitude = json["latitude"]
|
171
|
-
@longitude = json["longitude"]
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
# Object describing a list of photos in up to 4 sizes each
|
176
|
-
class UserProfilePhotos
|
177
|
-
attr_accessor :total_count, :photos
|
178
|
-
def initialize json
|
179
|
-
return if !json
|
180
|
-
@total_count = json["total_count"]
|
181
|
-
@photos = []
|
182
|
-
json["photos"].each do |p|
|
183
|
-
@photos<<ArrayOf.new(p, PhotoSize).to_a
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
class ReplyKeyboardMarkup
|
189
|
-
attr_accessor :keyboard, :resize_keyboard, :one_time_keyboard, :selective
|
190
|
-
def initialize json
|
191
|
-
return if !json
|
192
|
-
@keyboard = json["keyboard"]
|
193
|
-
@resize_keyboard = json["resize_keyboard"]
|
194
|
-
@one_time_keyboard = json["one_time_keyboard"]
|
195
|
-
@selective = json["selective"]
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
class ReplyKeyboardHide
|
200
|
-
attr_accessor :hide_keyboard, :selective
|
201
|
-
def initialize json
|
202
|
-
return if !json
|
203
|
-
@hide_keyboard = json["hide_keyboard"]
|
204
|
-
@selective = json["selective"]
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
class ForceReply
|
209
|
-
attr_accessor :force_reply, :selective
|
210
|
-
def initialize json
|
211
|
-
return if !json
|
212
|
-
@force_reply = json["force_reply"]
|
213
|
-
@selective = json["selective"]
|
214
|
-
end
|
215
|
-
end
|