Browse Source

Add commands and support using name on telegram group name

drymer 2 years ago
parent
commit
fa9c44b45c
1 changed files with 83 additions and 46 deletions
  1. 83
    46
      jabbergram.py

+ 83
- 46
jabbergram.py View File

@@ -49,6 +49,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
49 49
         self.groups = groups.split()
50 50
         self.bot = telegram.Bot(self.token)
51 51
         self.telegram_users = {}
52
+        self.group_name = {}
52 53
 
53 54
         # initialize http upload on a thread since its needed to be connected
54 55
         # to xmpp
@@ -85,6 +86,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
85 86
 
86 87
     def read_tg(self):
87 88
         update_id = 0
89
+        name = ''
88 90
 
89 91
         # wait until http_upload has been tested
90 92
         sleep(5)
@@ -92,11 +94,11 @@ class Jabbergram(sleekxmpp.ClientXMPP):
92 94
             try:
93 95
                 for update in self.bot.getUpdates(offset=update_id,
94 96
                                                   timeout=10):
95
-
97
+                    if not self.group_name:
98
+                        self.group_name = update.message.chat.username
96 99
                     if update.message.audio or update.message.document or \
97 100
                        update.message.photo or update.message.video \
98
-                       or update.message.voice:
99
-
101
+                       or update.message.voice or update.message.sticker:
100 102
                         # proceed only if http upload is available
101 103
                         if self.max_size is not None:
102 104
                             if update.message.audio:
@@ -121,11 +123,11 @@ class Jabbergram(sleekxmpp.ClientXMPP):
121 123
                                 size = d_file.file_size
122 124
                             if self.max_size >= size:
123 125
                                 t_file = self.bot.getFile(d_file.file_id)
124
-                                name = '/tmp/' + d_file.file_id + ext
125
-                                t_file.download(name)
126
+                                f_name = '/tmp/' + d_file.file_id + ext
127
+                                t_file.download(f_name)
126 128
                                 url = self.http_upload.upload(
127 129
                                                               self.component,
128
-                                                              '', name, size)
130
+                                                              '', f_name, size)
129 131
 
130 132
                                 if update.message.caption:
131 133
                                     message = update.message.caption + ' '
@@ -134,11 +136,12 @@ class Jabbergram(sleekxmpp.ClientXMPP):
134 136
 
135 137
                                 message += url
136 138
                             else:
137
-                                message = 'A file has been uploaded to Telegra'
138
-                                'm, but is too big.'
139
+                                message = 'A file has been uploaded to Telegr'\
140
+                                          'am, but is too big.'
139 141
                         else:
140
-                            message = 'A file has been uploaderd to Telegram, '
141
-                            'but the XMPP server doesn\'t support HTTP Upload.'
142
+                            message = 'A file has been uploaderd to Telegram,'\
143
+                                      'but the XMPP server doesn\'t support H'\
144
+                                      'TTP Upload.'
142 145
 
143 146
                     elif update.message.new_chat_member:
144 147
                         message = 'This user has joined the group.'
@@ -148,9 +151,19 @@ class Jabbergram(sleekxmpp.ClientXMPP):
148 151
                         message = 'The group\'s title has changed: '+ \
149 152
                           update.message.new_chat_title
150 153
                     elif update.message.new_chat_photo:
151
-                        message = 'The group\'s photo haschanged.'
154
+                        message = 'The group\'s photo has changed.'
152 155
                     else:
153
-                        message = update.message.text
156
+                        if update.message.reply_to_message:
157
+                            name = update.message.reply_to_message.from_user\
158
+                                                                  .username
159
+                            if name != self.bot.username:
160
+                                message = name + ': ' + \
161
+                                          update.message.reply_to_message.text
162
+                            else:
163
+                                message = update.message.reply_to_message.text
164
+                            message = message + ' <- ' + update.message.text
165
+                        else:
166
+                            message = update.message.text
154 167
 
155 168
                     user = str(update.message.from_user.username)
156 169
 
@@ -163,23 +176,33 @@ class Jabbergram(sleekxmpp.ClientXMPP):
163 176
                     if not user:
164 177
                         user = str(update.message.from_user.last_name)
165 178
 
166
-                    msg = user + ": " + message
167
-                    chat_id = update.message.chat_id
179
+                    # Don't add name first if it's a reply
180
+                    if name:
181
+                        msg = message
182
+                    else:
183
+                        msg = user + ": " + message
184
+
185
+                    chat = '@' + update.message.chat.username
186
+                    if chat not in self.groups:
187
+                        chat = str(update.message.chat_id)
168 188
 
169
-                    if message and str(chat_id) in self.groups:
170
-                        index = self.groups.index(str(chat_id))
189
+                    if message and chat in self.groups:
190
+                        index = self.groups.index(chat)
171 191
                         receiver = self.muc_rooms[index]
172 192
 
173
-                        if chat_id in self.telegram_users:
174
-                            if user not in self.telegram_users[chat_id]:
175
-                                self.telegram_users[chat_id] += ' ' + user
193
+                        if chat in self.telegram_users:
194
+                            if user not in self.telegram_users[chat]:
195
+                                self.telegram_users[chat] += ' ' + user
176 196
                         else:
177
-                            self.telegram_users[chat_id] = ' ' + user
197
+
198
+                            self.telegram_users[chat] = ' ' + user
178 199
 
179 200
                         if message == '.users':
180
-                            index = self.groups.index(str(chat_id))
181
-                            muc = self.muc_rooms[index]
182
-                            self.say_users('telegram', muc, chat_id)
201
+                            self.say_users('telegram', muc, chat)
202
+                        elif message == '.help':
203
+                            self.say_help('telegram', muc, chat)
204
+                        elif message == '.where':
205
+                            self.say_where('telegram', muc, chat)
183 206
                         else:
184 207
                             self.send_message(mto=receiver, mbody=msg,
185 208
                                               mtype='groupchat')
@@ -211,7 +234,10 @@ class Jabbergram(sleekxmpp.ClientXMPP):
211 234
 
212 235
         if msg['body'] == '.users':
213 236
             self.say_users('xmpp', muc_room, tg_group)
214
-
237
+        elif msg['body'] == '.help':
238
+            self.say_help('xmpp', muc_room, tg_group)
239
+        elif msg['body'] == '.where':
240
+            self.say_where('xmpp', muc_room, tg_group)
215 241
         elif msg['mucnick'] != self.nick:
216 242
             message = str(msg['from']).split('/')[1] + ': ' + str(msg['body'])
217 243
             self.bot.sendMessage(tg_group, text=message)
@@ -234,32 +260,46 @@ class Jabbergram(sleekxmpp.ClientXMPP):
234 260
             self.xmpp_users[muc].remove(presence['muc']['nick'])
235 261
 
236 262
     def say_users(self, service, muc, group):
237
-        xmpp_users = ""
238
-        tg_users = ""
239
-        group = int(group)
240
-
241
-        if muc in self.xmpp_users:
242
-            for i in self.xmpp_users[muc]:
243
-                xmpp_users = xmpp_users + ' _' + i
244
-        else:
245
-            xmpp_users = ""
263
+        if service == 'xmpp':
264
+            if group in self.telegram_users:
265
+                tg_users = self.telegram_users[group]
266
+            else:
267
+                tg_users = ""
246 268
 
247
-        msg1 = 'XMPP Users:' + xmpp_users
269
+            msg = 'Telegram Users:' + tg_users
248 270
 
249
-        if group in self.telegram_users:
250
-            tg_users = self.telegram_users[group]
251
-        else:
252
-            tg_users = ""
271
+            self.send_message(mto=muc, mbody=msg, mtype='groupchat')
253 272
 
254
-        msg2 = 'Telegram Users:' + tg_users
273
+        elif service == 'telegram':
274
+            xmpp_users = ""
275
+            if muc in self.xmpp_users:
276
+                for i in self.xmpp_users[muc]:
277
+                    xmpp_users = xmpp_users + ' ' + i
278
+            else:
279
+                xmpp_users = ""
255 280
 
256
-        message = msg1 + '\n' + msg2
281
+            msg = 'XMPP Users:' + xmpp_users
282
+            self.bot.sendMessage(group, text=msg)
257 283
 
284
+    def say_help(self, service, muc, group):
285
+        msg = 'Hi, I\'m ' + self.bot.username + '. I have two commands : ".us'\
286
+              'ers" and ".where".'
258 287
         if service == 'xmpp':
259
-            self.send_message(mto=muc, mbody=message, mtype='groupchat')
288
+            self.send_message(mto=muc, mbody=msg, mtype='groupchat')
289
+        elif service == 'telegram':
290
+            self.bot.sendMessage(group, text=msg)
260 291
 
292
+    def say_where(self, service, muc, group):
293
+        if service == 'xmpp':
294
+            if self.group_name:
295
+                msg = 'I\'m on http://telegram.me/' + self.group_name + '.'
296
+            else:
297
+                msg = 'Sorry, I\'m on a private group, you\'ll have to ask fo'\
298
+                      'r an invitation.'
299
+            self.send_message(mto=muc, mbody=msg, mtype='groupchat')
261 300
         elif service == 'telegram':
262
-            self.bot.sendMessage(group, text=message)
301
+            msg = 'I\'m on ' + muc + '.'
302
+            self.bot.sendMessage(group, text=msg)
263 303
 
264 304
     class HttpUpload():
265 305
         def __init__(self, parent_self):
@@ -316,8 +356,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
316 356
             xml = minidom.parseString(str(send))
317 357
             put_url = xml.getElementsByTagName('put')[0].firstChild.data
318 358
 
319
-            verify_ssl = ''
320
-            if verify_ssl == 'False':
359
+            if verify_ssl == '' or verif_ssl == 'False':
321 360
                 req = requests.put(put_url, data=open(u_file, 'rb'),
322 361
                                    verify=False)
323 362
             else:
@@ -356,5 +395,3 @@ if __name__ == '__main__':
356 395
         print("Done")
357 396
     else:
358 397
         print("Unable to connect.")
359
-
360
-    # Vols un gram nen?

Loading…
Cancel
Save