Skip to content

Commit

Permalink
Merge pull request #49 from d-Rickyy-b/dev
Browse files Browse the repository at this point in the history
Template support for TelegramAction
  • Loading branch information
d-Rickyy-b committed Oct 27, 2018
2 parents 7d191b4 + 5567c28 commit 7c388bc
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ deploy:
secure: Zq5/2MWAVDgV1KbEZTeK0zNdY/XFajY7d6RHiONsgAf/B3weeRG7tCc+9L/0MPGvDh+kpzFUEELPrZ3MHFuc6+VQVWSXojEfVuUCA8A3zDM/IYS+2Whp25ggY9NkOQNaD2kb/CTTsa5x9SjD0lyp+XphN0nRCXw77tvcwkxi/YmNOz5vBH7JmSMvKvScUgh4L4TkDsvopEIHPCah2CuW4QoDsZdR9NMEM8Ll0a2usnRu+UBgwStp4eoYsiaZyQbCb0yupVqOVHgKSbSirNSamuIOve4Yc7TjS+Y0GyAB8k7FUqKjUXL9rbggjk8hSTGCgtbGENDnRvYRzM6VO8CN8YxuCYWjG/rYbBkibvEAS8BsY5fHOdmDbRBQGD2aQrk3siBDuVyD78glQrqZB5FYpmUTXcJu5fkb5gWDW8NonT5X13OHin4c+eGZCXlRBkTBS/P2v/KMdkBQvQ8p9tkJ3181S0oP2hSXYSZbk6sdW7TijbVt6TMJ9ZD9QMPvlVBusAMmNteShW5kMIa8XVpQ92zZjZJOIwOMUWEvS2Oms67MrLOYb/ng2imgyJY9DxUnxNQn28/g5q1BYb5/31DxCW4sRIv8Qj/KZdtJiQ/6F5qAQe79NGJB5vQWAOCo6YimfJz3EQAdbjSGFrcUmwfLKH+wS63+uJ/3I322SXPZD94=
distributions: sdist bdist_wheel
skip_cleanup: true
skip_existing: true
on:
repo: d-Rickyy-b/pastepwn
tags: true
branch: master
python: "3.6"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To use the pastepwn framework you need to follow these simple steps:
3) Create a file (e.g. `main.py`) in your project root, where you put your code in¹
4) Fill that file with content - add analyzers and actions. Check the [example](https://github.com/d-Rickyy-b/pastepwn/tree/master/examples/example.py) implementation.

¹ *(If you want to store all pastes, make sure to setup a `mongodb`, `mysql` or `sylite` instance)*
¹ *(If you want to store all pastes, make sure to setup a `mongodb`, `mysql` or `sqlite` instance)*

### Behind a proxy

Expand Down
19 changes: 13 additions & 6 deletions pastepwn/actions/telegramaction.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
import logging
import re
from string import Template

from pastepwn.util import Request, DictWrapper
from .basicaction import BasicAction
from pastepwn.util import Request


class TelegramAction(BasicAction):
Expand All @@ -20,14 +21,20 @@ def __init__(self, token, receiver, custom_payload=None, template=None):
self.token = token
self.receiver = receiver
self.custom_payload = custom_payload
self.template = template
# TODO add possibility to send a template message and inject the paste data into the template
if template is not None:
self.template = Template(template)
else:
self.template = None

def perform(self, paste, analyzer_name=None):
"""Send a message via a Telegram bot to a specified user, without checking for errors"""
# if self.template:
# text = self.template.format()
r = Request()
text = "New paste matched by analyzer '{0}' - Link: {1}".format(analyzer_name, paste.full_url)
if self.template is None:
text = "New paste matched by analyzer '{0}' - Link: {1}".format(analyzer_name, paste.full_url)
else:
paste_dict = paste.to_dict()
paste_dict["analyzer_name"] = analyzer_name
text = self.template.safe_substitute(DictWrapper(paste_dict))

api_url = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}".format(self.token, self.receiver, text)
r.get(api_url)
Empty file added pastepwn/core/tests/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions pastepwn/core/tests/paste_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

import unittest

from pastepwn import Paste


class PasteTest(unittest.TestCase):

def setUp(self):
p = {"scrape_url": "https://scrape.pastebin.com/api_scrape_item.php?i=0CeaNm8Y",
"full_url": "https://pastebin.com/0CeaNm8Y",
"date": "1442911802",
"key": "0CeaNm8Y",
"size": "890",
"expire": "1442998159",
"title": "Once we all know when we goto function",
"syntax": "java",
"user": "admin",
"body": "this is a test paste"}

self.p = p
self.paste = Paste(p.get("key"),
p.get("title"),
p.get("user"),
p.get("size"),
p.get("date"),
p.get("expire"),
p.get("syntax"),
p.get("scrape_url"),
p.get("full_url"))

def tearDown(self):
pass

def test_init_paste(self):
self.assertEqual(self.p.get("key"), self.paste.key)
self.assertEqual(self.p.get("title"), self.paste.title)
self.assertEqual(self.p.get("user"), self.paste.user)
self.assertEqual(self.p.get("size"), self.paste.size)
self.assertEqual(self.p.get("date"), self.paste.date)
self.assertEqual(self.p.get("expire"), self.paste.expire)
self.assertEqual(self.p.get("syntax"), self.paste.syntax)
self.assertEqual(self.p.get("scrape_url"), self.paste.scrape_url)
self.assertEqual(self.p.get("full_url"), self.paste.full_url)
self.assertEqual(None, self.paste.body)

def test_set_body(self):
my_body = "This is a test for pastepwn"
self.paste.set_body(my_body)
self.assertEqual(my_body, self.paste.body)
3 changes: 2 additions & 1 deletion pastepwn/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from .request import Request
from .dictwrapper import DictWrapper
from .threadingutils import start_thread

__all__ = ('Request', 'start_thread')
__all__ = ('Request', 'DictWrapper', 'start_thread')
7 changes: 7 additions & 0 deletions pastepwn/util/dictwrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-


# https://stackoverflow.com/questions/19799609/leaving-values-blank-if-not-passed-in-str-format
class DictWrapper(dict):
def __missing__(self, key):
return '{' + key + '}'

0 comments on commit 7c388bc

Please sign in to comment.