因此,我编写了一个脚本来汇总一些新闻通讯并通过 Telegram 发送给我。该脚本本身运行完美。现在我尝试每天早上 7 点通过 Ubun 上的 crontab 运行此脚本...
因此,我编写了一个脚本来汇总一些新闻通讯并通过 Telegram 发送给我。该脚本本身运行完美。现在,我尝试每天早上 7 点通过 Ubunutu 服务器上的 crontab 运行此脚本。日志文件看起来不错,我在脚本中使用的每个 print() 都正常显示而没有错误,但它不会发送消息。
我的 Crontab 命令:
00 12 * * * DISPLAY=:0 /usr/bin/python3 /root/data/App/Briefing/main.py >> /home/main.log &2>1
Python 脚本:
主程序
import ai as AiHandler
import mail as MailHandler
import send as Telegram
import pandas as pd
from datetime import date
def start():
files = MailHandler.scanEmails()
files = AiHandler.summarize(files)
print(files)
data = pd.read_csv('checkpoint.csv', sep=';')
checkpoint = createMsg(data)
s = f"morgentliches Briefing vom {date.today().strftime("%d.%m.%Y")}".replace(".", "\\.")
s += "\n\n\n *Die* *Lage* *am* *Morgen* *\\-* *Spiegel*\n\n\n" + createMsg(pd.read_csv('spiegel.csv', sep=';'))
s += "\n\n\n *Berlin* *\\-* *Tagesspiegel* *Checkpoint*\n\n\n" + checkpoint
s += "\n\n\n *Tech* *Update* *\\-* *ManagerMagazin* \n\n\n" + createMsg(pd.read_csv('manager.csv', sep=';'))
Telegram.sendMessage(s)
def createMsg(data):
s = ""
for i in range(5):
thema = data['Thema'][i].replace('*', '\\*')
zsm = data['Zusammenfassung'][i].replace('*', '\\*')
s += "\t" + "*" + thema + "*" + "\n"
s += zsm + "\n"
s = s.replace("-", "\\-")
s = s.replace(".", "\\.")
s = s.replace("(", "\\(")
s = s.replace(")", "\\)")
#s = s.replace("*", "\\*")
s = s.replace("!", "\\!")
return s
start()
发送.py
import telebot
def sendMessage(msg):
bot = telebot.TeleBot("XXXXXX")
bot.send_message(chat_id="@morgenbriefing", text=msg, parse_mode="MarkdownV2")
我尝试将 DISPLAY:=0 添加到 crontab 命令中,但没有帮助
只是为了让评论部分的每个人都安心;在 RF 中并使用 python 标准库:
*** Settings ***
*** Variables ***
${DATE1} 2021-07-23T14:07:21Z
${DATE2} 2021-07-23T16:07:21+02:00
*** Keywords ***
Convert Date From Iso Format
[Documentation] fromisoformat() requires python 3.11 for certain formats!
[Arguments] ${dt}
${ret}= Evaluate datetime.datetime.fromisoformat("${dt}") modules=datetime
RETURN ${ret}
*** Test Cases ***
Compare the dates take 1
[Documentation] Compares DATE1 and DATE2 variables by converting them to datetime objects
... and using builtin Should Be Equal keyword
${newdate1} Convert Date From Iso Format ${DATE1}
${newdate2} Convert Date From Iso Format ${DATE2}
Should Be Equal ${newdate1} ${newdate2}
Compare the dates take 2
[Documentation] Compares DATE1 and DATE2 variables by converting them to datetime objects and using Evaluate keyword
... to compare them and returns boolean value to be used in Should Be True keyword
${ret}= Evaluate datetime.datetime.fromisoformat("${DATE1}")==datetime.datetime.fromisoformat("${DATE2}") modules=datetime
Should Be True ${ret}