Esempio n. 1
0
 def run(self):
     gallery = Gallery().search(tgid = self.chat_id)
     if gallery:
         newfile = self.bot.getFile(self.update.message.document.file_id)
         file_name = self.update.message.document.file_id
         newfile.download(file_name)
         writed = False
         if os.path.exists(file_name):
             writed = write_file(file_name, read_file(file_name, storage = 'local', append_path = False), acl = 'public-read', mime_type = self.update.message.document.mime_type)
             thumbnail(file_name)
             os.remove(file_name)
             write_file('%s.json' % file_name, self.update.to_json())
         if writed:
             file_id = File(gallery_eid = gallery.eid.value, file_id = self.update.message.document.file_id)
             file_id.save()
             sendLink = getattr(gallery, 'sendLink', None)
             if sendLink and sendLink.value:
                 self.text = 'File URL: %s' % url_for('image', file_id = file_id.eid.value, _external = True, disable_web_page_preview = True)
         else:
             self.text = 'Failed to download file'
     else:
         self.text = 'Gallery does not exist, please create first'
Esempio n. 2
0
def telegramWebHook():
    update = Update.de_json(request.get_json(force=True))
    text = None
    if getattr(update.message, 'document'):
        gallery = Gallery().search(tgid = update.message.chat.id)
        if gallery:
            newfile = bot.getFile(update.message.document.file_id)
            file_name = update.message.document.file_id
            newfile.download(file_name)
            writed = False
            if os.path.exists(file_name):
                writed = write_file(file_name, read_file(file_name, storage = 'local', append_path = False), acl = 'public-read', mime_type = update.message.document.mime_type)
                thumbnail(file_name)
                os.remove(file_name)
                write_file('%s.json' % file_name, update.to_json())
            if writed:
                file_id = File(gallery_eid = gallery.eid, file_id = update.message.document.file_id)
                file_id.save()
                sendLink = getattr(gallery, 'sendLink', None)
                if sendLink == 'True':
                    text = 'File URL: %s' % url_for('image', file_id = file_id.eid, _external = True, disable_web_page_preview = True)
            else:
                text = 'Failed to download file'
        else:
            text = 'Gallery does not exist, please create first'
        pass
    if getattr(update.message, 'text'):
        args = update.message.text.split(' ', 2)
        if args[0] == '/register':
            text = 'Username:'******'Complete register: https://telegram.me/ACSGalleryBot?start=%s' % update.message.from_user.id
            else:
                text = 'User added to gallery'
            # set gallery permission at this point because i have chat id
        elif args[0] == '/start':
            if len(args) > 1 and int(args[1]) == int(update.message.chat.id):
                text = 'Username:'******'force_reply' : True })
            else:
                text = update.to_json()

        elif getattr(update.message, 'reply_to_message'):
            if update.message.reply_to_message.text == 'Username:'******'Password:'******'force_reply' : True })
                return 'ok'
            elif update.message.reply_to_message.text == 'Password:'******'User succesfuly registered'
        elif args[0] == '/create':
            if hasattr(update.message.chat, 'title'):
                gallery = Gallery().search(tgid = update.message.chat.id)
                if not gallery:
                    gallery = Gallery(tgid = update.message.chat.id, title = update.message.chat.title).save()
                text = 'Gallery URL: %s' % url_for('gallery', id = gallery.eid, _external = True, _scheme = 'https')
            else:
                text = 'Bot only works in groups'
        elif args[0] == '/remove':
            gallery = Gallery().search(tgid = update.message.chat.id)
            if gallery:
                gallery.delete()
                text = 'Gallery deleted'
            else:
                text = 'Gallery is not registered'
            # TODO: Confirm
        elif args[0] == '/config':
            args.pop(0)
            gallery = Gallery.search(tgid = update.message.chat.id)
            if gallery:
                if len(args) == 0:
                    text = g.config(update.message.chat.id)
                elif len(args) == 1:
                    text = 'get one'
                    text = g.config(update.message.chat.id, args[0])
                else:
                    text = g.config(update.message.chat.id, args[0], args[1])
            else:
                text = 'Gallery is not registered'
        #else:
        #    text = update.to_json()
    if text:
        bot.sendMessage(update.message.chat.id, text, disable_web_page_preview=True)
    return ""