增加挂机完成通知 #4322
Replies: 4 comments 4 replies
-
分享一下目前自己的邮件通知脚本。 @echo off
pushd <path to MAA top directory>
find /c /v "" debug\gui.log > debug\lines_1.tmp
popd 然后是结束后脚本 @echo off
setlocal enabledelayedexpansion
pushd <path to MAA top directory>
timeout /t 3 /nobreak
find /c /v "" debug\gui.log > debug\lines_2.tmp
SET BIN=<path of python.exe>
for /f "tokens=3" %%a in (debug\lines_1.tmp) do (
set line1=%%a & goto :continue1
)
:continue1
for /f "tokens=3" %%a in (debug\lines_2.tmp) do (
set line2=%%a & goto :continue2
)
:continue2
@REM Enter notification directory
cd notification
echo "%BIN% mail.py %line1% %line2%" >> notification.log
%BIN% mail.py %line1% %line2%
cd ..
del debug\lines_1.tmp
del debug\lines_2.tmp
popd 接下来是 #!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import linecache
import logging as log
from config import settings
def render_template(template, **kwargs):
''' renders a Jinja template into HTML '''
# check if template exists
if not os.path.exists(template):
print('No template file present: %s' % template)
sys.exit()
import jinja2
templateLoader = jinja2.FileSystemLoader(searchpath=".")
templateEnv = jinja2.Environment(loader=templateLoader, trim_blocks=True, lstrip_blocks=True)
templ = templateEnv.get_template(template)
return templ.render(**kwargs)
#------------------------------------------------------------------------------------------------
def send_email(to, sender='MyCompanyName<[email protected]>', cc=None, bcc=None, subject=None, body=None):
''' sends email using a Jinja HTML template '''
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
user = settings.user
password = settings.password
# convert TO into list if string
if type(to) is not list:
to = to.split()
to_list = to + [cc] + [bcc]
to_list = list(filter(lambda x: x is not None, to_list)) # remove null emails
msg = MIMEMultipart('alternative')
msg['From'] = sender
msg['Subject'] = subject
msg['To'] = ','.join(to_list)
msg['Cc'] = cc
msg['Bcc'] = bcc
msg.attach(MIMEText(body, 'html', 'utf8'))
server = smtplib.SMTP(settings.smtp_server) # or your smtp server
try:
log.info('sending email {}'.format(subject))
server.login (user=user, password=password)
server.sendmail(sender, to_list, msg.as_string())
except Exception as e:
log.error('Error sending email')
log.exception(str(e))
finally:
server.quit()
#------------------------------------------------------------------------------------------------
# MAIN
start = int(sys.argv[1])
end = int(sys.argv[2])
content = linecache.getlines(settings.log_file)[start: end]
# generate HTML from template
html = render_template('template.j2', **locals())
to_list = list(settings.recipients)
sender = settings.sender
cc = settings.cc
bcc = settings.bcc
subject = settings.subject
# send email to a list of email addresses
send_email(to_list, sender, cc, bcc, subject, html)
recipients = ['YOUR@MAIL']
sender = 'MAA Bot <YOUR@MAIL>'
cc = 'YOUR@MAIL'
bcc = ''
subject = 'MAA任务完成通知'
log_file = '../debug/gui.log'
smtp_server = '<smtp server address>' 在.secret.toml里面写自己邮箱的账号和密码:
jinja2模板 <style type="text/css">
* {
margin: 0;
padding: 0;
/* Fix CSS. */
box-sizing: border-box;
}
body {
margin: 20px;
}
/* Set font size and color for code blocks */
code {
font-size: 14px;
color: #333;
font-family: "Noto Sans CJK", "SimSun", "Microsoft JhengHei", sans-serif;
display: block;
white-space: pre-wrap;
}
/* Set background color and padding for pre element */
pre {
background-color: #f5f5f5;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
overflow-x: auto; /* Add horizontal scrollbar if content is wider than the container */
}
img {
max-width: 100%;
height: auto;
}
/* Set font size and line height for pre element */
pre code {
font-size: 14px;
line-height: 1.5;
}
a {
font-size: 12px;
line-height: 1.0;
color: #D3D3D3;
}
#maa {
width: 80%;
height: auto;
display: block;
margin-left: auto;
margin-right: auto;
}
.center {
text-align: center;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Link to Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-HyQr4qM6g6tNmNJiRBmZfH3bx9n0f/CbbVcwS5xgPpV/WmG1aXfZtYyyJ0MImk3X" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
window.onload = function () {
document.querySelector("#header_img").onclick = function () {
document.querySelector("#bottom").scrollIntoView({ alignToTop: false, behavior: "smooth" });
}
}
</script>
</head>
<body>
<header>
<img id="header_img" src="https://images4.alphacoders.com/103/1034459.jpg" alt="Header Image">
<!--h1>Welcome to my website</h1-->
<h2 class="center">MAA GUI日志</h2>
</header>
<!--div><a href="#bottom">跳转底部</a></div-->
<pre><code>
{% for line in content %}
{# line | replace("\r\n", "") #}
{{ line | replace("\n", "") }}
{% endfor %}
</code></pre>
<div id="bottom" class="footer">
<img id="maa" src="https://cdn.jsdelivr.net/gh/MaaAssistantArknights/design/banner/maa-banner_original.jpg" alt="MAA Icon">
</div>
</body> |
Beta Was this translation helpful? Give feedback.
-
@LiamSho gui log 能手动 sync 吗?感觉可以在收到部分回调的时候 sync 一下 |
Beta Was this translation helpful? Give feedback.
-
要不干脆直接集成一下 ServerChan 好了( |
Beta Was this translation helpful? Give feedback.
-
Hi, I've been using MAA around 6 months and recently I want to add nearly same feature in this discussion: "Send an e-mail when all the task is finished". =========== |
Beta Was this translation helpful? Give feedback.
-
目前使用MAA有遇到过这样的情况:
当人不在电脑旁边,就看不到这些情况,所以最好给MAA加一个通知机制。
最近的版本MAA加入了运行前脚本和运行后脚本,利用这个自己写了一点batch和python脚本,实现运行完成后把新增的日志(gui.log)以邮件的方式发送出去,手机上查看邮件就能看到详细的运行过程。
不过今天更新了v4.14.1发现MAA在关闭之后才会把日志写进gui.log,自己的脚本没办法在MAA运行的时候从gui.log中得到新的日志。
所以希望开发者能给MAA增加发送通知的功能。
像近期刷1-7,MAA的日志会很长,所以个人建议优先实现邮件的通知方式,后续增加公众号推送、钉钉机器人、telegram bot等通知方式。
Beta Was this translation helpful? Give feedback.
All reactions