Created
July 4, 2023 01:49
-
-
Save paralin/f89eafed7d653d76083daa2c4cc9deb0 to your computer and use it in GitHub Desktop.
motion eye discord hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
import json | |
import argparse | |
import requests | |
class MotionEyeDiscordWebHook: | |
def __init__(self, args): | |
self.args = args | |
self.message = "" | |
if args.debug: | |
print("Notifying Discord") | |
if args.usetitle: | |
self.message += f"{args.title}" | |
if args.mention: | |
self.message += f"{args.mention} " | |
def get_latest_file(self): | |
path = os.path.join(self.args.path, "lastsnap.jpg") | |
if os.path.isfile(path): | |
return path | |
def send_to_discord(self, file_name=""): | |
self.message += f"Motion was detected on {self.args.name}" | |
data = {"payload_json": json.dumps({"content": self.message})} | |
files = {} | |
if os.path.isfile(file_name): | |
files = {"file": open(file_name, "rb")} | |
response = requests.post(self.args.hookurl, data=data, files=files) | |
if self.args.delete: | |
os.remove(file_name) | |
if self.args.debug: | |
print(response.text) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description="Discord Webhook Script for MotionEye.", | |
formatter_class=argparse.RawDescriptionHelpFormatter | |
) | |
parser.add_argument("--hookurl", help="Discord webhook url", type=str, required=True) | |
parser.add_argument("-n", "--name", help="Camera name", type=str, default="Camera") | |
parser.add_argument("-p", "--path", help="Camera folder path", type=str, default="/var/lib/motioneye/Camera1/") | |
parser.add_argument("--usetitle", help="Adds a message title. Customize with --title 'Your Title' ", action='store_true') | |
parser.add_argument("--title", help="Message Title", type=str, default="__**Motion Detected!**__\n\n") | |
parser.add_argument("-m", "--mention", help="Mention User(s)", type=str) | |
parser.add_argument("--delete", help="Will delete file(s) after sending", action='store_true') | |
parser.add_argument("--debug", help="Prints debug information to console", action='store_true') | |
args = parser.parse_args() | |
wh = MotionEyeDiscordWebHook(args) | |
wh.send_to_discord(wh.get_latest_file()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment