telegramAPI 1.0.0beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/telegramAPI.rb +87 -0
- data/lib/telegramObjects.rb +198 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f925231513665a638dfd6b3501547457ce62658
|
4
|
+
data.tar.gz: 1b757bde5cd5daa0b677c648d53635d661d1d33f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef59985fdb020e209247be51d162f6849cd33540ffa5d2f115906d47d21173077e7960dce00956101fce0d1d3f7a18fcfeaa291909059fbdf4ff86f3d5e36daf
|
7
|
+
data.tar.gz: af4bb8cc0dbd6e03d8f7a395bfce388358bd5ff805ebb735b6012a0c6d06ca86a6ca28aaa3409b85f67963598bda52cfecc63510e589edc332ef9b7e730924ea
|
data/lib/telegramAPI.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'uri'
|
6
|
+
require 'rest-client' # sudo gem install rest-client
|
7
|
+
require_relative 'TelegramObjects'
|
8
|
+
|
9
|
+
class TelegramAPI
|
10
|
+
@@core = "https://api.telegram.org/bot"
|
11
|
+
|
12
|
+
def initialize t
|
13
|
+
@token = t
|
14
|
+
@last_update = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def query api, params={}
|
18
|
+
p=[]
|
19
|
+
params_s=""
|
20
|
+
|
21
|
+
params.each do |param| p<<param.join("=") end
|
22
|
+
params_s="?"+p.join("&") if p.length!=0
|
23
|
+
|
24
|
+
JSON.parse(open(@@core+@token+"/"+api+params_s).read)
|
25
|
+
end
|
26
|
+
|
27
|
+
def getMe
|
28
|
+
User.new self.query("getMe")
|
29
|
+
end
|
30
|
+
|
31
|
+
def getUpdates options={}
|
32
|
+
r=self.query "getUpdates", {"offset"=>@last_update.to_s}.merge(options)
|
33
|
+
if r['ok']!=true then return nil end
|
34
|
+
up=ArrayOf.new(r['result'],Update).to_a
|
35
|
+
if up[-1]!=nil then @last_update=up[-1].update_id+1 end
|
36
|
+
return up
|
37
|
+
end
|
38
|
+
|
39
|
+
def sendMessage to, text, options={}
|
40
|
+
Message.new self.query("sendMessage", {"chat_id"=>to.to_s, "text"=>URI::encode(text)}.merge(options))
|
41
|
+
end
|
42
|
+
|
43
|
+
def forwardMessage to, from, msg
|
44
|
+
Message.new self.query("forwardMessage", {"chat_id"=>to, "from_chat_id"=>from, "message_id"=>msg})
|
45
|
+
end
|
46
|
+
|
47
|
+
def sendPhoto to, path, options={}
|
48
|
+
Message.new JSON.parse(RestClient.post(@@core+@token+"/sendPhoto", {:photo=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)).body)["result"]
|
49
|
+
end
|
50
|
+
|
51
|
+
# Max size: 50MB
|
52
|
+
# Format: Ogg OPUS. On ubuntu: avconv -i input.ext -acodec libopus output.ogg
|
53
|
+
def sendAudio to, path, options={}
|
54
|
+
RestClient.post @@core+@token+"/sendAudio", {:audio=>File.new(path, 'rb'), :chat_id=>to.to_s}.merge(options)
|
55
|
+
end
|
56
|
+
|
57
|
+
def sendDocument to, path, options={}
|
58
|
+
RestClient.post @@core+@token+"/sendDocument", {:document=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)
|
59
|
+
end
|
60
|
+
|
61
|
+
def sendStickerFromFile to, path, options={}
|
62
|
+
RestClient.post @@core+@token+"/sendStiker", {:sticker=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)
|
63
|
+
end
|
64
|
+
|
65
|
+
def sendSticker to, id, options={}
|
66
|
+
RestClient.post @@core+@token+"/sendSticker", {:sticker=>id, :chat_id=>to.to_s}.merge(options)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Max size: 50MB
|
70
|
+
# Fromat: mp4
|
71
|
+
def sendVideo to, path, options={}
|
72
|
+
RestClient.post @@core+@token+"/sendVideo", {:video=>File.new(path,'rb'), :chat_id=>to.to_s}.merge(options)
|
73
|
+
end
|
74
|
+
|
75
|
+
def sendLocation to, lat, long, options={}
|
76
|
+
self.query "sendLocation", {"chat_id"=>to, "latitude"=>lat, "longitude"=>long}.merge(options)
|
77
|
+
end
|
78
|
+
|
79
|
+
# act is one between: typing, upload_photo, record_video, record_audio, upload_audio, upload_document, find_location
|
80
|
+
def sendChatAction to, act
|
81
|
+
self.query "sendChatAction", {"chat_id"=>to, "action"=>act}
|
82
|
+
end
|
83
|
+
|
84
|
+
def getUserProfilePhotos id, options={}
|
85
|
+
self.query "getUserProfilePhotos", {"user_id"=>id}.merge(options)
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,198 @@
|
|
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
|
+
class User
|
11
|
+
attr_accessor :id, :first_name, :last_name, :username
|
12
|
+
def initialize json
|
13
|
+
return if !json
|
14
|
+
@id = json["id"]
|
15
|
+
@first_name = json["first_name"]
|
16
|
+
@last_name = json["last_name"]
|
17
|
+
@username = json["username"]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class GroupChat
|
22
|
+
attr_accessor :id, :title
|
23
|
+
def initialize json
|
24
|
+
return if !json
|
25
|
+
@id = json["id"]
|
26
|
+
@title = json["title"]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class ArrayOf
|
31
|
+
attr_accessor :array, :type
|
32
|
+
def initialize ar, cl
|
33
|
+
return if !ar
|
34
|
+
@array = []
|
35
|
+
@type = cl
|
36
|
+
ar.each do |e|
|
37
|
+
@array<<cl.new(e)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_a
|
42
|
+
@array
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Message
|
47
|
+
attr_accessor :message_id, :from, :date, :chat, :forward_from, :forward_date,
|
48
|
+
:reply_to_message, :text, :audio, :document, :photo, :sticker, :video,
|
49
|
+
:contact, :location, :new_chat_participant, :left_chat_participant,
|
50
|
+
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_create
|
51
|
+
|
52
|
+
def initialize json
|
53
|
+
return if !json
|
54
|
+
@message_id = json["message_id"]
|
55
|
+
@from = User.new json["from"]
|
56
|
+
@date = json["date"]
|
57
|
+
@chat = !json["chat"] ? nil : (json["chat"].has_key?("title") ? GroupChat.new(json["chat"]) : User.new(json["chat"]))
|
58
|
+
@forward_from = User.new json["forward_from"]
|
59
|
+
@forward_date = json["forward_date"]
|
60
|
+
@reply_to_message = Message.new json["reply_to_message"]
|
61
|
+
@text = json["text"]
|
62
|
+
@audio = Audio.new json["audio"]
|
63
|
+
@document = Document.new json["document"]
|
64
|
+
@photo = ArrayOf.new(json["photo"], PhotoSize).to_a
|
65
|
+
@sticker = Sticker.new json["sticker"]
|
66
|
+
@video = Video.new json["video"]
|
67
|
+
@contact = Contact.new json["contact"]
|
68
|
+
@location = Location.new json["location"]
|
69
|
+
@new_chat_participant = User.new json["new_chat_participant"]
|
70
|
+
@left_chat_participant = User.new json["left_chat_participant"]
|
71
|
+
@new_chat_title = json["new_chat_title"]
|
72
|
+
@new_chat_photo = ArrayOf.new(json["new_chat_photo"],PhotoSize).to_a
|
73
|
+
@delete_chat_photo = json["delete_chat_photo"]
|
74
|
+
@group_chat_create = json["group_chat_create"]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class PhotoSize
|
79
|
+
attr_accessor :file_id, :width, :height, :file_size
|
80
|
+
def initialize json
|
81
|
+
return if !json
|
82
|
+
@file_id = json["file_id"]
|
83
|
+
@width = json["width"]
|
84
|
+
@height = json["height"]
|
85
|
+
@file_size = json["file_size"]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class Audio
|
90
|
+
attr_accessor :file_id, :duration, :mime_type, :file_size
|
91
|
+
def initialize json
|
92
|
+
return if !json
|
93
|
+
@file_id = json["file_id"]
|
94
|
+
@duration = json["duration"]
|
95
|
+
@mime_type = json["mime_type"]
|
96
|
+
@file_size = json["file_size"]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Document
|
101
|
+
attr_accessor :file_id, :thumb, :file_name, :mime_type, :file_size
|
102
|
+
def initialize json
|
103
|
+
return if !json
|
104
|
+
@file_id = json["file_id"]
|
105
|
+
@thumb = PhotoSize.new json["thumb"]
|
106
|
+
@file_name = json["file_name"]
|
107
|
+
@mime_type = json["mime_type"]
|
108
|
+
@file_size = json["file_size"]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Sticker
|
113
|
+
attr_accessor :file_id, :width, :height, :thumb, :file_size
|
114
|
+
def initialize json
|
115
|
+
return if !json
|
116
|
+
@file_id = json["file_id"]
|
117
|
+
@width = json["width"]
|
118
|
+
@height = json["height"]
|
119
|
+
@thumb = PhotoSize.new json["thumb"]
|
120
|
+
@file_size = json["file_size"]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class Video
|
125
|
+
attr_accessor :file_id, :width, :height, :duration, :thumb, :mime_type, :file_size, :caption
|
126
|
+
def initialize json
|
127
|
+
return if !json
|
128
|
+
@file_id = json["file_id"]
|
129
|
+
@width = json["width"]
|
130
|
+
@height = json["height"]
|
131
|
+
@duration = json["duration"]
|
132
|
+
@thumb = PhotoSize.new json["thumb"]
|
133
|
+
@mime_type = json["mime_type"]
|
134
|
+
@file_size = json["file_size"]
|
135
|
+
@caption = json["caption"]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Contact
|
140
|
+
attr_accessor :phone_number, :first_name, :last_name, :user_id
|
141
|
+
def initialize json
|
142
|
+
return if !json
|
143
|
+
@phone_number = json["phone_number"]
|
144
|
+
@first_name = json["first_name"]
|
145
|
+
@last_name = json["last_name"]
|
146
|
+
@user_id = json["user_id"]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
class Location
|
151
|
+
attr_accessor :latitude, :longitude
|
152
|
+
def initialize json
|
153
|
+
return if !json
|
154
|
+
@latitute = json["latitude"]
|
155
|
+
@longitude = json["longitude"]
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
class UserProfilePhotos
|
160
|
+
attr_accessor :total_count, :photos
|
161
|
+
def initialize json
|
162
|
+
return if !json
|
163
|
+
@total_count = json["total_count"]
|
164
|
+
@photos = []
|
165
|
+
json["photos"].each do |p|
|
166
|
+
@photos<<ArrayOf.new(p).to_a
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
class ReplyKeyboardMarkup
|
172
|
+
attr_accessor :keyboard, :resize_keyboard, :one_time_keyboard, :selective
|
173
|
+
def initialize json
|
174
|
+
return if !json
|
175
|
+
@keyboard = json["keyboard"]
|
176
|
+
@resize_keyboard = json["resize_keyboard"]
|
177
|
+
@one_time_keyboard = json["one_time_keyboard"]
|
178
|
+
@selective = json["selective"]
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class ReplyKeyboardHide
|
183
|
+
attr_accessor :hide_keyboard, :selective
|
184
|
+
def initialize json
|
185
|
+
return if !json
|
186
|
+
@hide_keyboard = json["hide_keyboard"]
|
187
|
+
@selective = json["selective"]
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
class ForceReply
|
192
|
+
attr_accessor :force_reply, :selective
|
193
|
+
def initialize json
|
194
|
+
return if !json
|
195
|
+
@force_reply = json["force_reply"]
|
196
|
+
@selective = json["selective"]
|
197
|
+
end
|
198
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: telegramAPI
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0beta
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benedetto Nespoli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A lightweight Ruby API for Telegram-Bots
|
14
|
+
email: benedetto.nespoli@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/telegramAPI.rb
|
20
|
+
- lib/telegramObjects.rb
|
21
|
+
homepage: http://rubygems.org/gems/telegramAPI
|
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: 1.3.1
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.2.2
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Telegram api for Bots
|
45
|
+
test_files: []
|