2019-01-14 22:47:38 +00:00
|
|
|
__author__ = "Dennis Potter"
|
|
|
|
__copyright__ = "Copyright 2019, Dennis Potter"
|
|
|
|
__credits__ = ["Dennis Potter"]
|
|
|
|
__license__ = "GPL-3.0"
|
|
|
|
__version__ = "0.5.0"
|
|
|
|
__maintainer__ = "Dennis Potter"
|
|
|
|
__email__ = "dennis@dennispotter.eu"
|
|
|
|
|
2019-01-13 11:54:45 +00:00
|
|
|
from matrix_bot_api.mregex_handler import MRegexHandler
|
|
|
|
from woocommerce import API
|
|
|
|
from base64 import b64encode
|
|
|
|
import os
|
2019-01-14 22:47:38 +00:00
|
|
|
import hjson
|
2019-01-13 11:54:45 +00:00
|
|
|
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')
|
2019-01-14 22:47:38 +00:00
|
|
|
CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.hjson')
|
2019-01-13 11:54:45 +00:00
|
|
|
|
2019-01-14 22:47:38 +00:00
|
|
|
DATA_LOCATION = DATA_DIR + '/id.hjson'
|
2019-01-13 11:54:45 +00:00
|
|
|
HELP_LOCATION = MESSAGE_DIR + '/help'
|
2019-01-14 22:47:38 +00:00
|
|
|
MESSAGES_LOCATION = MESSAGE_DIR + '/messages.dutch.hjson'
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
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 <username>!".
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, bot):
|
|
|
|
# Load the configuration
|
2019-01-14 22:47:38 +00:00
|
|
|
with open(CONFIG_LOCATION) as hjson_data:
|
|
|
|
self.config = hjson.load(hjson_data)
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
# Load ID of coupon
|
2019-01-14 22:47:38 +00:00
|
|
|
with open(DATA_LOCATION) as hjson_data:
|
|
|
|
self.coupon_id = hjson.load(hjson_data)
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
# Load all messages for this plugin
|
2019-01-14 22:47:38 +00:00
|
|
|
with open(MESSAGES_LOCATION) as hjson_data:
|
|
|
|
self.messages = hjson.load(hjson_data)
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
# Define sensitivity
|
|
|
|
self.handler = []
|
|
|
|
|
2019-02-27 18:08:09 +00:00
|
|
|
self.handler.append(MRegexHandler(
|
|
|
|
"Peter coupon",
|
|
|
|
self.grab_coupon_callback,
|
|
|
|
bot))
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
# 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
|
2019-01-14 22:47:38 +00:00
|
|
|
with open(CONFIG_LOCATION) as hjson_data:
|
|
|
|
config = hjson.load(hjson_data)
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
# 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
|
2019-01-14 22:47:38 +00:00
|
|
|
ret = wcapi.post("coupons", data).json()
|
2019-01-13 11:54:45 +00:00
|
|
|
|
|
|
|
# Create data directory
|
|
|
|
os.mkdir(DATA_DIR)
|
|
|
|
|
|
|
|
# Write to JSON file to save ID
|
2019-01-14 22:47:38 +00:00
|
|
|
with open(DATA_LOCATION, 'w') as hjson_data:
|
|
|
|
hjson.dump(ret['id'], hjson_data)
|
2019-01-13 11:54:45 +00:00
|
|
|
|