mayohr-auto-punch
v1.2.2
Published
Automatic punch in/out system for mayohr
Readme
mayohr-auto-punch
Automatic punch in/out system for mayohr
Requirements
- Node.js 20+
Usage
Create a .env file
Create a .env file with your credentials:
# Download the .env.example file:
mkdir -p ~/.mayohr-auto-punch \
&& wget -O ~/.mayohr-auto-punch/.env \
https://raw.githubusercontent.com/awesome-oa-tools/mayohr-auto-punch/main/.env.example
# Update the .env file with your credentials:
vi ~/.mayohr-auto-punch/.envInstall puppeteer-chrome browser
npx puppeteer browsers install chromeRun the script
There are two ways to run the script:
Option 1: Run from npm package
npx --yes --quite mayohr-auto-punch@latestOption 2: Clone and run locally
git clone https://github.com/awesome-oa-tools/mayohr-auto-punch.git
cd mayohr-auto-punch
npm install
npm run devRun the script with Crontab
# Download the crontab config
mkdir -p ~/.mayohr-auto-punch/crontab && \
wget -O ~/.mayohr-auto-punch/crontab/mayohr-auto-punch.sh \
https://raw.githubusercontent.com/awesome-oa-tools/mayohr-auto-punch/main/examples/crontab/mayohr-auto-punch.sh && \
chmod +x ~/.mayohr-auto-punch/crontab/mayohr-auto-punch.sh && \
wget -O ~/.mayohr-auto-punch/crontab/mayohr \
https://raw.githubusercontent.com/awesome-oa-tools/mayohr-auto-punch/main/examples/crontab/mayohr
# Add to existing crontab (this will merge with your existing crontab entries)
crontab -l | cat - ~/.mayohr-auto-punch/crontab/mayohr | crontab -
# Verify the crontab settings
crontab -l
# To remove crontab, run crontab -eRun the script with Docker
docker run --rm -it \
-v $HOME/.mayohr-auto-punch:/home/pptruser/.mayohr-auto-punch \
justintw/mayohr-auto-punch:latestRun the script with AWS Lambda
請使用 AWS 台北 ap-east-2 區域,否則登入公司帳號的 IP 來自外國,會被 IT 關切
# Download the .env.example file:
mkdir -p ~/.mayohr-auto-punch \
&& wget -O ~/.mayohr-auto-punch/.env \
https://raw.githubusercontent.com/awesome-oa-tools/mayohr-auto-punch/main/.env.example
# Update the .env file with your credentials:
vi ~/.mayohr-auto-punch/.env
source ~/.mayohr-auto-punch/.env
# Download the AWS template
mkdir -p ~/.mayohr-auto-punch/aws \
&& wget -O ~/.mayohr-auto-punch/aws/ecr-template.yaml \
https://raw.githubusercontent.com/awesome-oa-tools/mayohr-auto-punch/main/examples/aws/ecr-template.yaml \
&& wget -O ~/.mayohr-auto-punch/aws/lambda-template.yaml \
https://raw.githubusercontent.com/awesome-oa-tools/mayohr-auto-punch/main/examples/aws/lambda-template.yaml
# Create SSM parameters for sensitive data
aws ssm put-parameter \
--no-cli-pager \
--region ap-east-2 \
--name "/mayohr-auto-punch/ms-password" \
--value "${MS_PASSWORD}" \
--type "SecureString" \
--overwrite
aws ssm put-parameter \
--no-cli-pager \
--region ap-east-2 \
--name "/mayohr-auto-punch/ms-totp-secret" \
--value "${MS_TOPT_SECRET}" \
--type "SecureString" \
--overwrite
aws ssm put-parameter \
--no-cli-pager \
--region ap-east-2 \
--name "/mayohr-auto-punch/telegram-bot-token" \
--value "${TELEGRAM_BOT_TOKEN}" \
--type "SecureString" \
--overwrite
# Create the ecr stack
aws cloudformation create-stack \
--no-cli-pager \
--region ap-east-2 \
--stack-name mayohr-auto-punch-ecr \
--template-body file://~/.mayohr-auto-punch/aws/ecr-template.yaml
# Get ECR URI
ECR_URI=$(aws cloudformation describe-stacks \
--no-cli-pager \
--region ap-east-2 \
--stack-name mayohr-auto-punch-ecr \
--query "Stacks[0].Outputs[?OutputKey=='RepositoryUri'].OutputValue" \
--output text)
# Login to ECR
aws ecr get-login-password \
--no-cli-pager \
--region ap-east-2 \
| docker login \
--username AWS \
--password-stdin \
${ECR_URI}
# Push the image to ECR
docker pull --platform linux/amd64 justintw/mayohr-auto-punch:latest \
&& docker tag justintw/mayohr-auto-punch:latest ${ECR_URI}:latest \
&& docker push ${ECR_URI}:latest
# Create the stack
aws cloudformation create-stack \
--no-cli-pager \
--region ap-east-2 \
--stack-name mayohr-auto-punch-lambda \
--template-body file://~/.mayohr-auto-punch/aws/lambda-template.yaml \
--parameters \
ParameterKey=ImageUri,ParameterValue=${ECR_URI}:latest \
ParameterKey=MsDomain,ParameterValue=${MS_DOMAIN} \
ParameterKey=MsUsername,ParameterValue=${MS_USERNAME} \
ParameterKey=TelegramEnabled,ParameterValue=${TELEGRAM_ENABLED} \
ParameterKey=TelegramChatId,ParameterValue=${TELEGRAM_CHAT_ID} \
--capabilities CAPABILITY_IAM
# Trigger the function
aws lambda invoke \
--no-cli-pager \
--region ap-east-2 \
--function-name mayohr-auto-punch \
--invocation-type RequestResponse \
--payload '{}' \
output.json
# Check log
aws logs describe-log-streams \
--no-cli-pager \
--region ap-east-2 \
--log-group-name /aws/lambda/mayohr-auto-punch \
--order-by LastEventTime \
--descending \
--limit 1 \
| jq -r '.logStreams[0].logStreamName' \
| xargs -I {} aws logs get-log-events \
--no-cli-pager \
--region ap-east-2 \
--log-group-name /aws/lambda/mayohr-auto-punch \
--log-stream-name {} \
--limit 100Update Lambda
# Get ECR URI
ECR_URI=$(aws cloudformation describe-stacks \
--no-cli-pager \
--region ap-east-2 \
--stack-name mayohr-auto-punch-ecr \
--query "Stacks[0].Outputs[?OutputKey=='RepositoryUri'].OutputValue" \
--output text)
# Login to ECR
aws ecr get-login-password \
--no-cli-pager \
--region ap-east-2 \
| docker login \
--username AWS \
--password-stdin \
${ECR_URI}
# Pull latest image and push to ECR
docker pull --platform linux/amd64 justintw/mayohr-auto-punch:latest \
&& docker tag justintw/mayohr-auto-punch:latest ${ECR_URI}:latest \
&& docker push ${ECR_URI}:latest
# Update Lambda function
aws lambda update-function-code \
--no-cli-pager \
--region ap-east-2 \
--function-name mayohr-auto-punch \
--image-uri ${ECR_URI}:latest
# ⏳ Wait ~2 minutes for the Lambda function update to complete before invoking.
# You can check the status with:
aws lambda get-function-configuration \
--no-cli-pager \
--region ap-east-2 \
--function-name mayohr-auto-punch \
--query 'LastUpdateStatus'
# Invoke Lambda to test
aws lambda invoke \
--no-cli-pager \
--region ap-east-2 \
--function-name mayohr-auto-punch \
--payload '{}' \
--cli-binary-format raw-in-base64-out \
/dev/stdoutTelegram Notification (Optional)
To enable Telegram notifications:
- Create a Telegram bot through @BotFather
- Create a Telegram Channel and add the bot to it as an admin
- Get Channel ID from https://api.telegram.org/bot<bot_token>/getUpdates
- Add the following to your
~/.mayohr-auto-punch/.envfile:
TELEGRAM_ENABLED="true"
TELEGRAM_BOT_TOKEN="your_bot_token"
TELEGRAM_CHAT_ID="your_chat_id"