def process_message(self, peer, mailfrom, rcpttos, data): logging.info("Receiving message from {} to {}".format( mailfrom, rcpttos)) email = Parser().parsestr(data) for name, cfg in self.inboxes.items(): address = cfg['address'] if not address in rcpttos: continue message = '**From:** {frm}\n**To:** {to}\n**Subject:** {subject}\n{body}'.format( frm=email['From'] or mailfrom, to=email['To'] or ', '.join(rcpttos), subject=email['Subject'], body=data) url = cfg['url'] logging.info( "Matched address {}; Sending message to webhook {}".format( address, url)) mattersend.send( channel=None, message=message, url=url, username='******', )
def test_fileinfo(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.coffee', fileinfo=True, just_return=True) content = r"```coffeescript\n%s```" % ('x' * 5000,) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "%s", "fields": [ { "short": true, "title": "Size", "value": "4.9KiB" }, { "short": true, "title": "Mime", "value": "None" } ], "text": "%s", "title": "source.coffee" } ], "channel": "town-square", "text": "" }""" % (content[:3501], content[:3501]))
def test_fileinfo(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.coffee', fileinfo=True, just_return=True) content = r"```coffeescript\n%s```" % ('x' * 5000, ) self.assertEqual( normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "%s", "fields": [ { "short": true, "title": "Size", "value": "4.9KiB" }, { "short": true, "title": "Mime", "value": "None" } ], "text": "%s", "title": "source.coffee" } ], "channel": "town-square", "text": "" }""" % (content[:3501], content[:3501]))
def test_simple_1(self): payload = mattersend.send(channel='town-square', message='test message', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "test message" }""")
def test_syntax_by_mime(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.diff', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "```diff\n```" }""")
def test_syntax_by_ext(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.coffee', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "```coffeescript\n%s```" }""" % ('x' * 5000))
def test_csv_dialect(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.csv', tabular='excel', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "| abc | def |\n| --- | --- |\n| foo | bar |" }""")
def test_override_url(self): payload = mattersend.send(channel='town-square', message='test message', url='http://chat.net/hooks/abdegh12', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST http://chat.net/hooks/abdegh12 { "channel": "town-square", "text": "test message" }""")
def test_filename_and_message(self): payload = mattersend.send(channel='town-square', filename='/etc/mime.types', message='test message', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "test message\n\ntext/x-diff diff" }""")
def test_override_url(self): payload = mattersend.send(channel='town-square', message='test message', url='http://chat.net/hooks/abdegh12', just_return=True) self.assertEqual( normalize_payload(payload), r"""POST http://chat.net/hooks/abdegh12 { "channel": "town-square", "text": "test message" }""")
def test_simple_1(self): payload = mattersend.send(channel='town-square', message='test message', just_return=True) self.assertEqual( normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "test message" }""")
def test_fileinfo(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.coffee', fileinfo=True, just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "text": "| Filename | Size | Mime |\n| --- | --- | --- |\n| source.coffee | 4.9KiB | None |\n\n```coffeescript\n%s```" }""" % ('x' * 5000))
def test_section(self): payload = mattersend.send(channel='town-square', message='test message', config_section='angrybot', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "icon_url": "https://chat.mydomain.com/static/emoji/1f620.png", "text": "test message", "username": "******" }""")
def test_section(self): payload = mattersend.send(channel='town-square', message='test message', config_section='angrybot', just_return=True) self.assertEqual( normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "channel": "town-square", "icon_url": "https://chat.mydomain.com/static/emoji/1f620.png", "text": "test message", "username": "******" }""")
def test_syntax_mk(self): payload = mattersend.send(channel='town-square', filename='/home/test/Makefile', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "```makefile\n```", "text": "```makefile\n```", "title": "Makefile" } ], "channel": "town-square", "text": "" }""")
def test_syntax_mk(self): payload = mattersend.send(channel='town-square', filename='/home/test/Makefile', just_return=True) self.assertEqual( normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "```makefile\n```", "text": "```makefile\n```", "title": "Makefile" } ], "channel": "town-square", "text": "" }""")
def test_csv(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.csv', tabular='sniff', just_return=True) self.assertEqual(normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "| abc | def |\n| --- | --- |\n| foo | bar |", "text": "| abc | def |\n| --- | --- |\n| foo | bar |", "title": "source.csv" } ], "channel": "town-square", "text": "" }""")
def test_filename_and_message(self): payload = mattersend.send(channel='town-square', filename='/etc/mime.types', message='test message', just_return=True) self.assertEqual( normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "text/x-diff diff", "text": "text/x-diff diff", "title": "mime.types" } ], "channel": "town-square", "text": "test message" }""")
def test_csv_dialect(self): payload = mattersend.send(channel='town-square', filename='/home/test/source.csv', tabular='excel', just_return=True) self.assertEqual( normalize_payload(payload), r"""POST https://chat.mydomain.com/hooks/abcdefghi123456 { "attachments": [ { "fallback": "| abc | def |\n| --- | --- |\n| foo | bar |", "text": "| abc | def |\n| --- | --- |\n| foo | bar |", "title": "source.csv" } ], "channel": "town-square", "text": "" }""")
def test_send(self, mock_post): with self.assertRaises(RuntimeError): payload = mattersend.send(channel='town-square', message='test message', url='http://chat.net/hooks/fail')
def test_send(self, mock_post): payload = mattersend.send(channel='town-square', message='test message', url='http://chat.net/hooks/abdegh12')