forked from LlmKira/Openaibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request LlmKira#361 from LlmKira/dev
Patch Plugin System
- Loading branch information
Showing
10 changed files
with
431 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ docs | |
/llmkira/mongodb/ | ||
/docs/test/ | ||
/jobs.sqlite | ||
/config_dir/*.pem | ||
/config_dir/*.secret/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,5 @@ config_dir/*.db | |
/memray-*.html | ||
/docs/test_script/sticker/ | ||
/jobs.sqlite | ||
config_dir/*.pem | ||
config_dir/*.secret/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2023/11/15 下午9:49 | ||
# @Author : sudoskys | ||
# @File : note_github_bot.py | ||
# @Software: PyCharm | ||
import os | ||
|
||
import requests | ||
from dotenv import load_dotenv | ||
from flask import Flask, request | ||
from github import Github, GithubIntegration | ||
|
||
load_dotenv() | ||
|
||
__flask_app_name__ = 'github_bot' | ||
app = Flask(__flask_app_name__) | ||
app_id = int(os.environ['GITHUB_APP_ID']) | ||
with open( | ||
os.path.normpath(os.path.expanduser(os.getenv("GITHUB_PRIVATE_KEY_FILE", '~/.certs/github/bot_key.pem'))), | ||
'r' | ||
) as cert_file: | ||
app_key = cert_file.read() | ||
|
||
git_integration = GithubIntegration( | ||
app_id, | ||
app_key, | ||
) | ||
|
||
|
||
@app.route("/", methods=['POST']) | ||
def bot(): | ||
# Get the event payload | ||
payload = request.json | ||
# Check if the event is a GitHub PR creation event | ||
if not all(k in payload.keys() for k in ['action', 'pull_request']) and \ | ||
payload['action'] == 'opened': | ||
return "ok" | ||
owner = payload['repository']['owner']['login'] | ||
repo_name = payload['repository']['name'] | ||
# Get a git connection as our bot | ||
# Here is where we are getting the permission to talk as our bot and not | ||
# as a Python webservice | ||
git_connection = Github( | ||
login_or_token=git_integration.get_access_token( | ||
git_integration.get_installation(owner, repo_name).id | ||
).token | ||
) | ||
repo = git_connection.get_repo(f"{owner}/{repo_name}") | ||
|
||
issue = repo.get_issue(number=payload['pull_request']['number']) | ||
|
||
# Call meme-api to get a random meme | ||
response = requests.get(url='https://meme-api.herokuapp.com/gimme') | ||
if response.status_code != 200: | ||
return 'ok' | ||
|
||
# Get the best resolution meme | ||
meme_url = response.json()['preview'][-1] | ||
# Create a comment with the random meme | ||
issue.create_comment(f"![Alt Text]({meme_url})") | ||
return "ok" | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=True, port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2023/11/15 下午10:19 | ||
# @Author : sudoskys | ||
# @File : note_github_bot_test.py | ||
# @Software: PyCharm | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from github_bot_api import Event, Webhook | ||
from github_bot_api import GithubApp | ||
from github_bot_api.flask import create_flask_app | ||
|
||
load_dotenv() | ||
|
||
app_id = int(os.environ['GITHUB_APP_ID']) | ||
with open( | ||
os.path.normpath(os.path.expanduser(os.getenv("GITHUB_PRIVATE_KEY_FILE", '~/.certs/github/bot_key.pem'))), | ||
'r' | ||
) as cert_file: | ||
app_key = cert_file.read() | ||
|
||
app = GithubApp( | ||
user_agent='my-bot/0.0.0', | ||
app_id=app_id, | ||
private_key=app_key | ||
) | ||
|
||
webhook = Webhook(secret=None) | ||
|
||
|
||
@webhook.listen('issues') | ||
def on_pull_request(event: Event) -> bool: | ||
print(event.payload) | ||
client = app.installation_client(event.payload['installation']['id']) | ||
repo = client.get_repo(event.payload['repository']['full_name']) | ||
issue = repo.get_issue(number=event.payload['issue']['number']) | ||
issue.create_comment('Hello World') | ||
return True | ||
|
||
|
||
@webhook.listen('issue_comment') | ||
def on_issue_comment(event: Event) -> bool: | ||
print(event.payload) | ||
client = app.installation_client(event.payload['installation']['id']) | ||
repo = client.get_repo(event.payload['repository']['full_name']) | ||
issue = repo.get_issue(number=event.payload['issue']['number']) | ||
issue.edit( | ||
body=f"Hello World\n\n{issue.body}" | ||
|
||
) | ||
return True | ||
|
||
|
||
import os | ||
|
||
os.environ['FLASK_ENV'] = 'development' | ||
flask_app = create_flask_app(__name__, webhook) | ||
flask_app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.