初始化仓库:https://github.com/rcy1314/news-action
修改action配置:
on:
push:
schedule:
- cron: "38 22 * * *" #早上6点45左右推送
jobs:
bot:
runs-on: ubuntu-22.04
steps:
- name: 'Checkout codes'
uses: actions/checkout@v3
- name: python env
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install bs4
pip install requests
- name: 'Get DataSource'
run: curl https://news.topurl.cn > ./result.html
- name: 'Get Info'
run: python ./getInfo.py
- name: 'Get Date'
run: |
echo "REPORT_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T')" >> $GITHUB_ENV
echo "TODAY_DATE=$(TZ=':Asia/Shanghai' date +'%Y-%m-%d')" >> $GITHUB_ENV
- name: 'Get TEXT'
run: |
echo 'FILE_TEXT<<EOF' >> $GITHUB_ENV
cat ./result.txt >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: 'Send to Telegram Channel'
run: |
python send_to_telegram.py
env:
TELEGRAM_API_TOKEN: ${{ secrets.TELEGRAM_API_TOKEN }}
TELEGRAM_CHANNEL_ID: <YOUR_CHANNEL_ID>
- name: '发送到微信群'
uses: fifsky/dingtalk-action@master
with:
url: ${{ secrets.WECHAT_WEBHOOK}}
type: text
at: all
content: |
${{env.FILE_TEXT}}
- name: '打完收工,提交代码!'
run: |
git config --local user.email "1129159201@qq.com"
git config --local user.name "rcy1314"
git remote set-url origin https://${{ github.actor }}:${{ secrets.MYGG_TOKEN }}@github.com/${{ github.repository }}
git pull
cp -Rf result.txt logs/${{env.TODAY_DATE}}.txt
git add .
git commit -m "Update By Github Action Bot"
git push
确保将<YOUR_CHANNEL_ID>替换为Telegram频道的ID,并将代码中的TELEGRAM_API_TOKEN和TELEGRAM_CHANNEL_ID改为实际值
同时,创建一个名为send_to_telegram.py的Python脚本文件,并将以下代码复制到该文件中
import requests
import os
telegram_api_token = os.environ['TELEGRAM_API_TOKEN']
telegram_channel_id = os.environ['TELEGRAM_CHANNEL_ID']
report_date = os.environ['REPORT_DATE']
file_text = os.environ['FILE_TEXT']
message = f"今日新闻简报推送 ({report_date}):\n\n{file_text}"
url = f"https://api.telegram.org/bot{telegram_api_token}/sendMessage"
data = {
'chat_id': telegram_channel_id,
'text': message
}
response = requests.post(url, json=data)
response.raise_for_status()
TELEGRAM_API_TOKEN和TELEGRAM_CHANNEL_ID改为实际值