xasin-telegram 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xasin/telegram/MQTT_Adapter.rb +70 -48
- data/lib/xasin/telegram/TestHTTPCore.rb +40 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25c7030241f3f7f6eb094897f5175c49a177e299
|
4
|
+
data.tar.gz: 9d335a41200777ead31dad505e059d3a9ae726c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fe1d6ce851631e0460bba3b843eef7fbf3583bff045bd854870c9cf1cb8526c8b300e5a793d7e13e5c8ebbf22a1fc926bf0727a7f13b7d243faf35878c26685
|
7
|
+
data.tar.gz: 2dc0c584e4e5e4b829d0f8c8b12ef0664a78326f6d5d7769fdfbb4da53bd2c3a47688137ea17f39bd12042bb980b65a1770629307b7b086bcfb099801b9eddbf
|
@@ -9,6 +9,7 @@ module Telegram
|
|
9
9
|
attr_accessor :usernameList
|
10
10
|
|
11
11
|
def initialize(httpCore, mqtt)
|
12
|
+
# Check if we already have a HTTPCore, else create one
|
12
13
|
if(httpCore.is_a? Telegram::HTTPCore)
|
13
14
|
@httpCore = httpCore;
|
14
15
|
else
|
@@ -28,24 +29,34 @@ module Telegram
|
|
28
29
|
setup_mqtt();
|
29
30
|
end
|
30
31
|
|
31
|
-
def _process_inline_keyboard(keyboardLayout, gID)
|
32
|
-
|
33
|
-
return nil unless
|
32
|
+
def _process_inline_keyboard(keyboardLayout, gID = nil)
|
33
|
+
# Return unless we have a structure we can form into a keyboard
|
34
|
+
return nil unless (keyboardLayout.is_a? Array or keyboardLayout.is_a? Hash)
|
34
35
|
|
36
|
+
# Make sure the structure of keyboardLayout is [{}] or [[]]
|
37
|
+
if(keyboardLayout.is_a? Hash)
|
38
|
+
keyboardLayout = [keyboardLayout]
|
39
|
+
elsif(not (keyboardLayout[0].is_a? Array or keyboardLayout[0].is_a? Hash))
|
40
|
+
keyboardLayout = [keyboardLayout]
|
41
|
+
end
|
35
42
|
|
36
|
-
keyboardLayout = [keyboardLayout] unless(keyboardLayout[0].is_a? Array);
|
37
43
|
outData = Array.new();
|
38
44
|
|
45
|
+
# Iterate through the rows of keyboards
|
39
46
|
keyboardLayout.each do |row|
|
40
47
|
newRow = Array.new();
|
41
48
|
|
49
|
+
# Create the INLINE KEY button elements
|
42
50
|
row.each do |key, val|
|
43
51
|
cbd = {i: gID, k: (val or key)};
|
44
52
|
newRow << {text: key, callback_data: cbd.to_json}
|
45
53
|
end
|
54
|
+
|
55
|
+
# Add the new row to the array of rows
|
46
56
|
outData << newRow;
|
47
57
|
end
|
48
58
|
|
59
|
+
# Return the ready reply_markup element
|
49
60
|
return {inline_keyboard: outData};
|
50
61
|
end
|
51
62
|
|
@@ -59,93 +70,105 @@ module Telegram
|
|
59
70
|
def _handle_send(data, uID)
|
60
71
|
# Resolve a saved Username to a User-ID
|
61
72
|
uID = @usernameList[uID] if(@usernameList.key? uID)
|
62
|
-
return if (uID = uID.to_i) == 0;
|
73
|
+
return if (uID = uID.to_i) == 0; # Return if a unknown Username was used
|
63
74
|
|
64
|
-
|
65
|
-
data = JSON.parse(data, symbolize_names: true) if data.is_a? String
|
66
|
-
rescue
|
67
|
-
# Allow for pure-text to be sent (easier on the ESPs)
|
68
|
-
data = {text: data}
|
69
|
-
end
|
75
|
+
gID = data[:gid];
|
70
76
|
|
71
|
-
if
|
77
|
+
# Check if a GroupID is present and a former message is known
|
78
|
+
if(gID and @groupIDList[uID][gID])
|
72
79
|
if(data[:replace])
|
73
80
|
_handle_delete(gID, uID)
|
74
|
-
elsif(data[:overwrite]
|
81
|
+
elsif(data[:overwrite])
|
75
82
|
_handle_edit(data, uID);
|
76
|
-
return;
|
83
|
+
return; # After editing, no new message should be sent!
|
77
84
|
end
|
78
85
|
end
|
79
86
|
|
87
|
+
# Lay out all mandatory parameters for sendMessage request
|
80
88
|
outData = {
|
81
89
|
chat_id: uID,
|
82
|
-
parse_mode: (data[:parse_mode] or "Markdown"),
|
90
|
+
parse_mode: (data[:parse_mode] or "Markdown"), # Markdown parse mode is just nice
|
83
91
|
text: data[:text]
|
84
92
|
}
|
85
93
|
|
86
|
-
if
|
87
|
-
|
94
|
+
# Check if the message is meant to be sent without notification
|
95
|
+
if(data[:silent])
|
96
|
+
outData[:disable_notification] = true;
|
97
|
+
end
|
98
|
+
|
99
|
+
# Check if an inline keyboard layout is given, and parse that.
|
100
|
+
if((ilk = data[:inline_keyboard]))
|
101
|
+
outData[:reply_markup] = _process_inline_keyboard(ilk, gID);
|
88
102
|
end
|
89
103
|
|
90
104
|
reply = @httpCore.perform_post("sendMessage", outData);
|
91
|
-
return unless reply[:ok]
|
105
|
+
return unless reply[:ok] # Something was wrong about our message layout
|
106
|
+
# TODO Add a proper error handler here.
|
92
107
|
|
93
|
-
#
|
94
|
-
|
95
|
-
# Save this grouping ID
|
96
|
-
@groupIDList[uID][gID] = reply[:result][:message_id];
|
97
|
-
end
|
108
|
+
# If a GroupID was given, save the sent message's ID
|
109
|
+
@groupIDList[uID][gID] = reply[:result][:message_id] if(gID);
|
98
110
|
end
|
99
111
|
|
100
112
|
def _handle_edit(data, uID)
|
101
113
|
# Resolve a saved Username to a User-ID
|
102
114
|
uID = @usernameList[uID] if(@usernameList.key? uID)
|
103
|
-
return if (uID = uID.to_i) == 0;
|
104
|
-
|
105
|
-
begin
|
106
|
-
data = JSON.parse(data, symbolize_names: true) if data.is_a? String
|
115
|
+
return if (uID = uID.to_i) == 0; # Return if a unknown Username was used
|
107
116
|
|
108
|
-
|
109
|
-
|
117
|
+
# Fetch the target MessageID - Return if none is present
|
118
|
+
return unless mID = @groupIDList[uID][data[:gid]]
|
110
119
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
120
|
+
# Lay out all mandatory arguments for the edit POST
|
121
|
+
outData = {
|
122
|
+
chat_id: uID,
|
123
|
+
message_id: mID,
|
124
|
+
};
|
115
125
|
|
116
|
-
|
117
|
-
|
118
|
-
|
126
|
+
# If a inline keyboard was given, parse that.
|
127
|
+
if(ilk = data[:inline_keyboard])
|
128
|
+
outData[:reply_markup] = _process_inline_keyboard(ilk, data[:gid]);
|
129
|
+
end
|
119
130
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
rescue
|
131
|
+
if(data[:text]) # Check if text was given
|
132
|
+
outData[:text] = data[:text];
|
133
|
+
# Send the POST request editing the message text
|
134
|
+
@httpCore.perform_post("editMessageText", outData);
|
135
|
+
else
|
136
|
+
# Otherwise, only edit the reply markup (keyboard etc.)
|
137
|
+
@httpCore.perform_post("editMessageReplyMarkup", outData);
|
128
138
|
end
|
129
139
|
end
|
130
140
|
|
131
141
|
def _handle_delete(data, uID)
|
132
142
|
# Resolve a saved Username to a User-ID
|
133
143
|
uID = @usernameList[uID] if(@usernameList.key? uID)
|
134
|
-
return if (uID = uID.to_i) == 0;
|
144
|
+
return if (uID = uID.to_i) == 0; # Return unless the username was known
|
135
145
|
|
136
146
|
# Fetch the real message ID held by a grouping ID
|
137
147
|
return unless mID = @groupIDList[uID][data]
|
138
|
-
@groupIDList[uID].delete(data);
|
148
|
+
@groupIDList[uID].delete(data); # Clear that ID from the list
|
139
149
|
|
150
|
+
# Perform the actual delete
|
140
151
|
@httpCore.perform_post("deleteMessage", {chat_id: uID, message_id: mID});
|
141
152
|
end
|
142
153
|
|
143
154
|
def setup_mqtt()
|
144
155
|
@mqtt.subscribe_to "Telegram/+/Send" do |data, tSplit|
|
156
|
+
begin
|
157
|
+
data = JSON.parse(data);
|
158
|
+
rescue
|
159
|
+
data = {text: data}
|
160
|
+
end
|
161
|
+
|
145
162
|
_handle_send(data, tSplit[0]);
|
146
163
|
end
|
147
164
|
|
148
165
|
@mqtt.subscribe_to "Telegram/+/Edit" do |data, tSplit|
|
166
|
+
begin
|
167
|
+
data = JSON.parse(data);
|
168
|
+
rescue
|
169
|
+
next;
|
170
|
+
end
|
171
|
+
|
149
172
|
_handle_edit(data, tSplit[0])
|
150
173
|
end
|
151
174
|
|
@@ -195,12 +218,11 @@ module Telegram
|
|
195
218
|
uID = newUID
|
196
219
|
end
|
197
220
|
|
198
|
-
return unless /(\S+):(\S+)/ =~ msg[:data]
|
199
221
|
begin
|
200
222
|
data = JSON.parse(msg[:data], symbolize_names: true);
|
201
223
|
|
202
224
|
data = {
|
203
|
-
gid: data[:
|
225
|
+
gid: data[:i],
|
204
226
|
key: data[:k],
|
205
227
|
}
|
206
228
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require_relative 'HTTPCore.rb'
|
3
|
+
|
4
|
+
module Xasin
|
5
|
+
module Telegram
|
6
|
+
class TestCore < HTTPCore
|
7
|
+
attr_accessor :lastPostData, :lastPostRequest
|
8
|
+
attr_reader :currentMessageID
|
9
|
+
attr_accessor :toReturn
|
10
|
+
|
11
|
+
def initialize()
|
12
|
+
prepare();
|
13
|
+
end
|
14
|
+
|
15
|
+
def prepare()
|
16
|
+
@lastPostData = Array.new();
|
17
|
+
@lastPostRequest = Array.new();
|
18
|
+
|
19
|
+
@currentMessageID = rand(0..9999);
|
20
|
+
|
21
|
+
@toReturn = {
|
22
|
+
ok: true,
|
23
|
+
result: {
|
24
|
+
message_id: @currentMessageID
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def attach_receptor(receptor)
|
30
|
+
end
|
31
|
+
|
32
|
+
def perform_post(postRequest, postData)
|
33
|
+
@lastPostRequest << postRequest;
|
34
|
+
@lastPostData << postData;
|
35
|
+
|
36
|
+
return @toReturn;
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xasin-telegram
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xasin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mqtt-sub_handler
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- lib/xasin/telegram/HTTPCore.rb
|
37
37
|
- lib/xasin/telegram/MQTT_Adapter.rb
|
38
38
|
- lib/xasin/telegram/SingleUser.rb
|
39
|
+
- lib/xasin/telegram/TestHTTPCore.rb
|
39
40
|
homepage: https://github.com/XasWorks/XasCode/tree/GEM_Telegram/Ruby/Telegram
|
40
41
|
licenses:
|
41
42
|
- GPL-3.0
|