Example #1
0
def file_catcher(bot, update):
    doc = update.message.document
    file_id = telegram.File(doc.file_id)["file_id"]
    file_name = telegram.File(doc.file_name)["file_id"]
    tmp_path = "tmp{}{}".format(seperator, file_name)

    newFile = bot.getFile(file_id)
    newFile.download(tmp_path)
    file_md5 = hashlib.md5(
        fp.file_as_bytes(open("tmp{}{}".format(seperator, file_name),
                              'rb'))).hexdigest()
    bot.send_message(
        chat_id=update.message.chat_id,
        text="You uploaded {}, md5({}), telegram_message_id({})".format(
            file_name, file_md5, update.message.message_id))
    fpfp = fp.FileProcessor()
    fpfp.file_checker(tmp_path)
    if fpfp.counter != 0:
        bot.send_message(chat_id=update.message.chat_id,
                         text="You already submtted this sample")
    # virustotal info
    bot.send_message(chat_id=update.message.chat_id,
                     text="virustotal link {}".format(fpfp.vt_url))

    # hybrid analysis info
    bot.send_message(chat_id=update.message.chat_id,
                     text="hybrid-analysis link {}".format(fpfp.hybrid_url))
Example #2
0
def email_attachments(update, context):
    global ATTACHMENTS
    types = ['photo', 'audio', 'document', 'video']
    file_types_found = []
    file_bytes_list = {}
    for t in types:
        if update.message[t]:
            file_types_found.append(t)
    for file_type in file_types_found:
        if file_type == 'photo':
            file_id = update.message[file_type][len(update.message[file_type])
                                                - 1]['file_id']
            file_name = update.message[file_type][
                len(update.message[file_type]) - 1]['file_id'] + '.jpg'
        else:
            file_id = update.message[file_type]['file_id']
            file_name = update.message[file_type]['file_name']
        file_path = context.bot.getFile(file_id).file_path
        file = telegram.File(file_id, bot=context.bot, file_path=file_path)
        file_bytes = bytes(file.download_as_bytearray())
        file_bytes_list[file_name] = file_bytes
    if send_email(files=file_bytes_list):
        update.message.reply_text('Email sent successfully')
    else:
        update.message.reply_text('Sending email failed')
    return ConversationHandler.END
Example #3
0
    def test_equality(self):
        a = telegram.File(self.audio_file_id, self._bot)
        b = telegram.File(self.audio_file_id, self._bot)
        c = telegram.File(self.audio_file_id, None)
        d = telegram.File(self.document_file_id, self._bot)
        e = telegram.Voice(self.audio_file_id, 0)

        self.assertEqual(a, b)
        self.assertEqual(hash(a), hash(b))
        self.assertIsNot(a, b)

        self.assertEqual(a, c)
        self.assertEqual(hash(a), hash(c))

        self.assertNotEqual(a, d)
        self.assertNotEqual(hash(a), hash(d))

        self.assertNotEqual(a, e)
        self.assertNotEqual(hash(a), hash(e))
Example #4
0
    def test_equality(self):
        a = telegram.File("DOESNTMATTER", self._bot)
        b = telegram.File("DOESNTMATTER", self._bot)
        c = telegram.File("DOESNTMATTER", None)
        d = telegram.File("DOESNTMATTER2", self._bot)
        e = telegram.Voice("DOESNTMATTER", 0)

        self.assertEqual(a, b)
        self.assertEqual(hash(a), hash(b))
        self.assertIsNot(a, b)

        self.assertEqual(a, c)
        self.assertEqual(hash(a), hash(c))

        self.assertNotEqual(a, d)
        self.assertNotEqual(hash(a), hash(d))

        self.assertNotEqual(a, e)
        self.assertNotEqual(hash(a), hash(e))
Example #5
0
 def run(self, message, photo, tg_id, token, query=None):
     self.setup(tg_id, token, query)
     query = Query.from_json(query) if query is not None else None
     photo = telegram.File(**photo, bot=self.bot) if photo is not None else None
     if query is not None:
         if query["pid"] is not None:
             self.post_page(query.uid, query.pid, message, photo)
         elif query.uid is not None:
             self.post_user(query.uid, query.pid, message, photo)
     else:
         self.post(message, photo)
Example #6
0
def detect_malaria(update, context):
    global FASTAI_MODEL
    context.bot.send_message(
        chat_id=update.effective_chat.id,
        text="I'll tell you if this is infected in a moment.")
    photo_list = update.message['photo']
    file_id = photo_list[len(photo_list) - 1]['file_id']
    file_path = context.bot.getFile(file_id).file_path
    file = telegram.File(file_id, bot=context.bot, file_path=file_path)
    image = file.download('sample.png')
    img = open_image('sample.png')
    pred_class, b, c = FASTAI_MODEL.predict(img)
    response = f'This is {pred_class}.'
    context.bot.send_message(chat_id=update.effective_chat.id, text=response)

    return ConversationHandler.END
Example #7
0
def img_handler(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="I'll tell you what this is in a moment.")
    photo_list = update.message['photo']
    file_id = photo_list[len(photo_list) - 1]['file_id']
    file_path = context.bot.getFile(file_id).file_path
    file = telegram.File(file_id, bot=context.bot, file_path=file_path)
    image_bytes = file.download_as_bytearray()
    # image = file.download('sample.png')
    image_np_array = get_np_array(image_bytes)
    predictions = classify(image_np_array)[0]
    if not predictions:
        context.bot.send_message(
            chat_id=update.effective_chat.id,
            text="Couldn't figure that out. Try again later")
        return
    logger.info(predictions)
    best_pred = ' '.join([
        word.capitalize()
        for word in predictions[0][1].replace('_', ' ').split()
    ])
    response = f'This is a {best_pred}. I am {predictions[0][2]*100:.2f}% confident'
    context.bot.send_message(chat_id=update.effective_chat.id, text=response)