From 6cb3ecc3737ff854cf8cd65f39c2f916cdae806f Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 13 Jan 2019 12:54:45 +0100 Subject: [PATCH] Added a WooCommerce Coupon plugin This plugin sets up a coupon on installation and sends this coupon to a user that asks for it. The plugin supports an expiration period. After this period, it will renew the coupon. --- plugins/woocommerce_coupons/README.md | 1 + .../woocommerce_coupons/config.json.template | 14 ++ plugins/woocommerce_coupons/messages/help | 2 + .../messages/messages.dutch.json | 5 + plugins/woocommerce_coupons/requirements.txt | 1 + .../woocommerce_coupons.py | 131 ++++++++++++++++++ 6 files changed, 154 insertions(+) create mode 100644 plugins/woocommerce_coupons/README.md create mode 100644 plugins/woocommerce_coupons/config.json.template create mode 100644 plugins/woocommerce_coupons/messages/help create mode 100644 plugins/woocommerce_coupons/messages/messages.dutch.json create mode 100644 plugins/woocommerce_coupons/requirements.txt create mode 100644 plugins/woocommerce_coupons/woocommerce_coupons.py diff --git a/plugins/woocommerce_coupons/README.md b/plugins/woocommerce_coupons/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/plugins/woocommerce_coupons/README.md @@ -0,0 +1 @@ + diff --git a/plugins/woocommerce_coupons/config.json.template b/plugins/woocommerce_coupons/config.json.template new file mode 100644 index 0000000..1c000fb --- /dev/null +++ b/plugins/woocommerce_coupons/config.json.template @@ -0,0 +1,14 @@ +{ + "rest_api": { + "shop_url": "https://alcuinus.nl", + "consumer_key": "", + "consumer_secret": "", + "wp_api": true, + "version": "wc/v1" + }, + + "coupon": { + "max_days": 31, + "percentage":20 + } +} diff --git a/plugins/woocommerce_coupons/messages/help b/plugins/woocommerce_coupons/messages/help new file mode 100644 index 0000000..a3b75d6 --- /dev/null +++ b/plugins/woocommerce_coupons/messages/help @@ -0,0 +1,2 @@ +
coupon
+Als je me een bericht stuurt met het keyword coupon zal ik je de coupon sturen die voor 20% korting op de Alcuinus webshop zorgt. diff --git a/plugins/woocommerce_coupons/messages/messages.dutch.json b/plugins/woocommerce_coupons/messages/messages.dutch.json new file mode 100644 index 0000000..5a88627 --- /dev/null +++ b/plugins/woocommerce_coupons/messages/messages.dutch.json @@ -0,0 +1,5 @@ +{ + + "coupon_message" : "De huidige code om {}% korting te krijgen is: {}. Ga meteen naar de webshop om hem te gebruiken!" + +} diff --git a/plugins/woocommerce_coupons/requirements.txt b/plugins/woocommerce_coupons/requirements.txt new file mode 100644 index 0000000..cb25c7c --- /dev/null +++ b/plugins/woocommerce_coupons/requirements.txt @@ -0,0 +1 @@ +WooCommerce diff --git a/plugins/woocommerce_coupons/woocommerce_coupons.py b/plugins/woocommerce_coupons/woocommerce_coupons.py new file mode 100644 index 0000000..37c07ef --- /dev/null +++ b/plugins/woocommerce_coupons/woocommerce_coupons.py @@ -0,0 +1,131 @@ +from matrix_bot_api.mregex_handler import MRegexHandler +from woocommerce import API +from base64 import b64encode +import os +import json +import datetime as dt + +MESSAGE_DIR = os.path.join(os.path.dirname(__file__), 'messages') +DATA_DIR = os.path.join(os.path.dirname(__file__),'../../data/woocommerce') +CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.json') + +DATA_LOCATION = DATA_DIR + '/id.json' +HELP_LOCATION = MESSAGE_DIR + '/help' +MESSAGES_LOCATION = MESSAGE_DIR + '/messages.dutch.json' + +class Plugin: + """ This is an example plugin with only a single callback. When + a user says "Hello bot" in a room in which te bot is present, + the user replies with "Hello !". + """ + + def __init__(self, bot): + # Load the configuration + with open(CONFIG_LOCATION) as json_data: + self.config = json.load(json_data) + + # Load ID of coupon + with open(DATA_LOCATION) as json_data: + self.coupon_id = json.load(json_data) + + # Load all messages for this plugin + with open(MESSAGES_LOCATION) as json_data: + self.messages = json.load(json_data) + + # Define sensitivity + self.handler = [] + + self.handler.append(MRegexHandler("Peter coupon", self.grab_coupon_callback)) + + # initialize WooCommerce connection + self.wcapi = API( + url=self.config['rest_api']['shop_url'], + consumer_key=self.config['rest_api']['consumer_key'], + consumer_secret=self.config['rest_api']['consumer_secret'], + wp_api=self.config['rest_api']['wp_api'], + version=self.config['rest_api']['version'], + query_string_auth=True + ) + + # Save parent bot + self.bot = bot + + def grab_coupon_callback(self, room, event): + coupon_json = self.wcapi.get(f"coupons/{self.coupon_id}").json() + + datetime_now = dt.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") + + + if coupon_json['expiry_date'] < datetime_now: + # Refresh coupon + + # Create random token and determine max validity + vld_days = self.config['coupon']['max_days'] + + token = b64encode(os.urandom(8)).decode('utf-8').lower()[0:-1] + expires_dat = (dt.datetime.now() + + dt.timedelta(days = vld_days)).strftime( + "%Y-%m-%dT%H:%M:%S") + + data = { + "code": token, + "amount": self.config['coupon']['percentage'], + "expiry_date": expires_dat, + } + + self.wcapi.put(f"coupons/{self.coupon_id}", data) + + room.send_html(self.messages['coupon_message'].format( + coupon_json["amount"].split('.')[0], + token)) + + else: + room.send_html(self.messages['coupon_message'].format( + coupon_json["amount"].split('.')[0], + coupon_json["code"])) + + def help(self): + return open(HELP_LOCATION, mode="r").read() + +def setup(): + """This function initializes a coupon with a given percentage""" + + # Load the configuration + with open(CONFIG_LOCATION) as json_data: + config = json.load(json_data) + + # Create random token and determine max validity + vld_days = config['coupon']['max_days'] + + token = b64encode(os.urandom(8)).decode('utf-8').lower()[0:-1] + expires_dat = (dt.datetime.now() + dt.timedelta(days = vld_days)).strftime( + "%Y-%m-%dT%H:%M:%S") + + data = { + "code": token, + "discount_type": "percent", + "amount": config['coupon']['percentage'], + "individual_use": True, + "exclude_sale_items": True, + "expiry_date": expires_dat, + } + + wcapi = API( + url=config['rest_api']['shop_url'], + consumer_key=config['rest_api']['consumer_key'], + consumer_secret=config['rest_api']['consumer_secret'], + wp_api=config['rest_api']['wp_api'], + version=config['rest_api']['version'], + query_string_auth=True + ) + + # Send to shop + ret = wcapi.post("coupons", data) + + # Create data directory + os.mkdir(DATA_DIR) + + # Write to JSON file to save ID + with open(DATA_LOCATION, 'w') as json_data: + json.dump(ret['id'], json_data) +