[first] commit

This commit is contained in:
xtao-dada 2022-03-05 16:10:53 +08:00
parent 4d2226e7f9
commit 2a3c35a55c
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
6 changed files with 156 additions and 1 deletions

4
.gitignore vendored
View File

@ -150,5 +150,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ .idea/
# Data
config.ini

104
app.py Normal file
View File

@ -0,0 +1,104 @@
from aioflask import Flask
from ci import app as bot
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
async def webhook(request):
data = request.json
# json contains an attribute that differentiates between the types, see
# https://docs.gitlab.com/ce/user/project/integrations/webhooks.html
# for more infos
kind = data['object_kind']
if kind == 'push':
msg = generatePushMsg(data)
elif kind == 'tag_push':
msg = generatePushMsg(data) # TODO:Make own function for this
elif kind == 'issue':
msg = generateIssueMsg(data)
elif kind == 'note':
msg = generateCommentMsg(data)
elif kind == 'merge_request':
msg = generateMergeRequestMsg(data)
elif kind == 'wiki_page':
msg = generateWikiMsg(data)
elif kind == 'pipeline':
msg = generatePipelineMsg(data)
elif kind == 'build':
msg = generateBuildMsg(data)
else:
msg = "Unknown event."
await bot.send_message(-1001441461877, msg)
return jsonify({'status': 'ok'})
def generatePushMsg(data):
msg = f'*🔨 {data["total_commits_count"]} new commits to ' \
f'{data["project"]["name"]}:{data["project"]["default_branch"]}*:\n\n'
for commit in data['commits']:
tag_link = f'<a href="{commit["url"]}">{commit["id"][:7]}</a>'
msg = msg + f"{tag_link}: {commit['message'].rstrip()}\n"
return msg
def generateIssueMsg(data):
action = data['object_attributes']['action']
if action == 'open':
assignees = ''
for assignee in data.get('assignees', []):
assignees += assignee['name'] + ' '
msg = '*{0}* new issue for *{1}*:\n' \
.format(data['project']['name'], assignees)
elif action == 'reopen':
assignees = ''
for assignee in data.get('assignees', []):
assignees += assignee['name'] + ' '
msg = '*{0}* issue re-opened for *{1}*:\n' \
.format(data['project']['name'], assignees)
elif action == 'close':
msg = '*{0}* issue closed by *{1}*:\n' \
.format(data['project']['name'], data['user']['name'])
elif action == 'update':
assignees = ''
for assignee in data.get('assignees', []):
assignees += assignee['name'] + ' '
msg = '*{0}* issue assigned to *{1}*:\n' \
.format(data['project']['name'], assignees)
else:
msg = ""
msg = msg + '[{0}]({1})' \
.format(data['object_attributes']['title'], data['object_attributes']['url'])
return msg
def generateCommentMsg(data):
ntype = data['object_attributes']['noteable_type']
if ntype == 'Commit':
msg = 'note to commit'
elif ntype == 'MergeRequest':
msg = 'note to MergeRequest'
elif ntype == 'Issue':
msg = 'note to Issue'
elif ntype == 'Snippet':
msg = 'note on code snippet'
else:
msg = ""
return msg
def generateMergeRequestMsg(data):
return 'new MergeRequest'
def generateWikiMsg(data):
return 'new wiki stuff'
def generatePipelineMsg(data):
return 'new pipeline stuff'
def generateBuildMsg(data):
return 'new build stuff'

21
ci.py Normal file
View File

@ -0,0 +1,21 @@
from configparser import RawConfigParser
from pyrogram import Client
from httpx import AsyncClient, get
# 读取配置文件
config = RawConfigParser()
config.read("config.ini")
bot_token: str = ""
admin_id: int = 0
bot_token = config.get("basic", "bot_token", fallback=bot_token)
admin_id = config.getint("basic", "admin", fallback=admin_id)
guess_time = 30 # 猜语音游戏持续时间
""" Init httpx client """
# 使用自定义 UA
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
}
client = AsyncClient(timeout=10.0, headers=headers)
me = get(f"https://api.telegram.org/bot{bot_token}/getme").json()
# 初始化客户端
app = Client("bot", bot_token=bot_token)

15
config.gen.ini Normal file
View File

@ -0,0 +1,15 @@
[pyrogram]
api_id = 12345
api_hash = 0123456789abc0123456789abc
[basic]
bot_token = 111:abc
admin = 777000
[plugins]
root = plugins
[proxy]
enabled = False
hostname = 127.0.0.1
port = 1080

9
main.py Normal file
View File

@ -0,0 +1,9 @@
import logging
from ci import app as bot
from app import app
# 日志记录
logging.basicConfig(level=logging.INFO)
bot.start()
app.run("0.0.0.0", port=10111)
bot.stop()

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
aioflask
Pyrogram>=1.3.5
Tgcrypto>=1.2.3
httpx