errbot icon indicating copy to clipboard operation
errbot copied to clipboard

callback_stream not working for Telegram Backend

Open ajay1mg opened this issue 6 years ago • 3 comments

In order to let us help you better, please fill out the following fields as best you can:

I am requesting help with running my bot for file uploads.

I am running...

  • Errbot version: 5.2.0
  • OS version: ubuntu 16.04
  • Python version: 3.5
  • Using a virtual environment: yes

Issue description

Below is the code that I am using for listening to incoming file transfer using telegram backend.

import logging from errbot import botcmd, BotPlugin from config import BOT_DATA_DIR from os import path, mkdir, walk from io import open import shutil

FILESHARE_PATH = path.join(BOT_DATA_DIR, "public")

if not path.exists(FILESHARE_PATH): mkdir(FILESHARE_PATH)

class FileShare(BotPlugin): min_err_version = '2.2.0-beta' # for file transfers

def target(self, name):
    full_path = path.join(FILESHARE_PATH, name)
    if full_path != path.abspath(full_path):
        logging.warn('Refused the filename "%s" is it an injection attempt?' % name)
        return ''
    return full_path

def callback_stream(self, stream):
    super(FileShare, self).callback_stream(stream)
    import pdb;pdb.set_trace()
    if not stream.name:
        logging.info("Anonymous stream, I can't save that")
        return

    logging.debug('Receive the file "%s"' % stream.name)
    destination_path = self.target(stream.name)
    if not destination_path:
        self.send(stream.identity, "Invalid filename %s." % stream.name)
        return
    with open(destination_path, "wb") as destination:
        shutil.copyfileobj(stream, destination)
    self.send(stream.identity, "File %q well received." % stream.name)


@botcmd
def download(self, mess, args):
    target = self.target(args)
    if not target:
        return 'Invalid filename "%s"' % target

    if not path.exists(target):
        return 'File not found %s' % args
    self.send_stream_request(mess.frm, open(target, 'rb'), name=args, size=path.getsize(target),
                             stream_type='document')
    return 'File request sent'

@botcmd
def upload(self, mess, args):
    return "upload your file"

@botcmd
def upload_file(self, mess, args):
    target = '/home/beast/Downloads/errbot.pdf'
    self.send_stream_request(mess.frm, open(target, 'rb'), name=args, size=path.getsize(target),
                             stream_type='document')
    return "file uploaded successfully"

@botcmd
def ls(self, mess, args):
    return '\n'.join(['\n'.join([n for n in f]) for p, _, f in walk(FILESHARE_PATH)])

Problem is that program execution never reads callback_stream method and fails with and error message saying 'Message ignored (not a text message)'.

My question is: does errbot Telegram backend support this feature? If yes then how can I make it work?

ajay1mg avatar Apr 13 '19 08:04 ajay1mg

Sorry I have the same problem with slack. Have you tried this code on slack to test?

andcenciar avatar May 08 '19 00:05 andcenciar

@sijis Is it not implement in telegram and slack?

Nov1kov avatar Oct 07 '21 13:10 Nov1kov

I think it is in slack, not sure about telegram. Generally speaking, they should all have it. If it's not a supported feature of the backend, there should be some graceful fallback for it.

sijis avatar Oct 08 '21 00:10 sijis

In order to let us help you better, please fill out the following fields as best you can:

I am requesting help with running my bot for file uploads.

I am running...

  • Errbot version: 5.2.0
  • OS version: ubuntu 16.04
  • Python version: 3.5
  • Using a virtual environment: yes

Issue description

Below is the code that I am using for listening to incoming file transfer using telegram backend.

import logging from errbot import botcmd, BotPlugin from config import BOT_DATA_DIR from os import path, mkdir, walk from io import open import shutil

FILESHARE_PATH = path.join(BOT_DATA_DIR, "public")

if not path.exists(FILESHARE_PATH): mkdir(FILESHARE_PATH)

class FileShare(BotPlugin): min_err_version = '2.2.0-beta' # for file transfers

def target(self, name):
    full_path = path.join(FILESHARE_PATH, name)
    if full_path != path.abspath(full_path):
        logging.warn('Refused the filename "%s" is it an injection attempt?' % name)
        return ''
    return full_path

def callback_stream(self, stream):
    super(FileShare, self).callback_stream(stream)
    import pdb;pdb.set_trace()
    if not stream.name:
        logging.info("Anonymous stream, I can't save that")
        return

    logging.debug('Receive the file "%s"' % stream.name)
    destination_path = self.target(stream.name)
    if not destination_path:
        self.send(stream.identity, "Invalid filename %s." % stream.name)
        return
    with open(destination_path, "wb") as destination:
        shutil.copyfileobj(stream, destination)
    self.send(stream.identity, "File %q well received." % stream.name)


@botcmd
def download(self, mess, args):
    target = self.target(args)
    if not target:
        return 'Invalid filename "%s"' % target

    if not path.exists(target):
        return 'File not found %s' % args
    self.send_stream_request(mess.frm, open(target, 'rb'), name=args, size=path.getsize(target),
                             stream_type='document')
    return 'File request sent'

@botcmd
def upload(self, mess, args):
    return "upload your file"

@botcmd
def upload_file(self, mess, args):
    target = '/home/beast/Downloads/errbot.pdf'
    self.send_stream_request(mess.frm, open(target, 'rb'), name=args, size=path.getsize(target),
                             stream_type='document')
    return "file uploaded successfully"

@botcmd
def ls(self, mess, args):
    return '\n'.join(['\n'.join([n for n in f]) for p, _, f in walk(FILESHARE_PATH)])

Problem is that program execution never reads callback_stream method and fails with and error message saying 'Message ignored (not a text message)'.

And it reiterated the previous code as Target for the self mess arguments as the return statement quotes message ignored not a text message.

VikramJaryal avatar Aug 26 '23 00:08 VikramJaryal

Closing, as it's targeting an old release.

sijis avatar Jan 05 '24 16:01 sijis