Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misskey対応 #2386

Merged
merged 34 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
eb802ca
Misskey対応
massongit Mar 5, 2023
3e149c8
READMEを更新してあげたよ!
github-actions[bot] Mar 5, 2023
d6bba7b
Merge pull request #2387 from dev-hato/update-readme-add_misskey
massongit Mar 5, 2023
458951d
formatが間違ってたので直してあげたよ!
github-actions[bot] Mar 5, 2023
d44043e
Merge pull request #2388 from dev-hato/fix-format-add_misskey
massongit Mar 5, 2023
1c8b96e
順序修正
massongit Mar 5, 2023
5110bbe
クライアント周り修正
massongit Mar 5, 2023
8be5bdc
フォーマット修正
massongit Mar 5, 2023
260a8e0
Revert "フォーマット修正"
massongit Mar 5, 2023
6a50250
フォーマット修正
massongit Mar 5, 2023
7cbcc6c
formatが間違ってたので直してあげたよ!
github-actions[bot] Mar 5, 2023
03203e1
Merge pull request #2390 from dev-hato/fix-format-add_misskey
massongit Mar 5, 2023
7f6ec1a
フォーマット修正
massongit Mar 5, 2023
cb51dcf
formatが間違ってたので直してあげたよ!
github-actions[bot] Mar 5, 2023
7e3c4a5
Merge pull request #2391 from dev-hato/fix-format-add_misskey
massongit Mar 5, 2023
e5e3e58
Merge branch 'massongit-patch-1' into add_misskey
massongit Mar 5, 2023
e53d949
ignore E1101
massongit Mar 5, 2023
32436d8
外部サーバーからのメンションは受け付けない
massongit Mar 6, 2023
3e2a599
README修正
massongit Mar 6, 2023
9b25c48
formatが間違ってたので直してあげたよ!
github-actions[bot] Mar 6, 2023
17e5dce
Merge pull request #2397 from dev-hato/fix-format-add_misskey
massongit Mar 6, 2023
1bd32ee
READMEを更新してあげたよ!
github-actions[bot] Mar 6, 2023
c9f66a4
Merge pull request #2398 from dev-hato/update-readme-add_misskey
massongit Mar 6, 2023
572f11e
Merge branch 'develop' into add_misskey
massongit Mar 6, 2023
489812a
Merge branch 'develop' into add_misskey
massongit Mar 6, 2023
dcbfb6e
formatが間違ってたので直してあげたよ!
github-actions[bot] Mar 6, 2023
ded97de
Merge pull request #2403 from dev-hato/fix-format-add_misskey
massongit Mar 6, 2023
0467e25
Merge branch 'develop' into add_misskey
massongit Mar 7, 2023
a87afa5
formatが間違ってたので直してあげたよ!
github-actions[bot] Mar 7, 2023
c43aa00
Merge pull request #2406 from dev-hato/fix-format-add_misskey
massongit Mar 7, 2023
13e20d6
README修正
massongit Mar 7, 2023
f429c0b
MISSKEY_URL -> MISSKEY_DOMAIN
massongit Mar 7, 2023
fe17fa1
READMEを更新してあげたよ!
github-actions[bot] Mar 7, 2023
de2629c
Merge pull request #2407 from dev-hato/update-readme-add_misskey
massongit Mar 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
DATABASE_URL=postgres://postgres:password@postgres:5432/
DISCORD_API_TOKEN=
MISSKEY_API_TOKEN=
MISSKEY_DOMAIN=
MODE=
OPENAI_API_KEY=
SLACK_API_TOKEN=
Expand Down
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ pandas = "==1.5.3"
matplotlib = "==3.7.1"
openai = "==0.27.0"
discord = "==2.2.2"
"misskey.py" = "==4.1.0"
websockets = "==10.4"
84 changes: 83 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@

DISCORD_API_TOKENには `Read Messages/View Channels` と、 `Send Messages` の権限が必要です。

MODEに `misskey` を指定すると、自分のサーバーからのメンションに限って反応するMisskeyのBotとして動作します。

MISSKEY_URLにBotのいるMisskeyサーバーのドメインを指定します。

MISSKEY_API_TOKENにMisskeyのBotのアクセストークンを指定します。

6. docker composeで鳩botとPostgreSQLを起動します。

```sh
Expand Down
6 changes: 6 additions & 0 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@

DISCORD_API_TOKENには `Read Messages/View Channels` と、 `Send Messages` の権限が必要です。

MODEに `misskey` を指定すると、自分のサーバーからのメンションに限って反応するMisskeyのBotとして動作します。

MISSKEY_URLにBotのいるMisskeyサーバーのドメインを指定します。

MISSKEY_API_TOKENにMisskeyのBotのアクセストークンを指定します。

6. docker composeで鳩botとPostgreSQLを起動します。

```sh
Expand Down
37 changes: 37 additions & 0 deletions library/clientclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,40 @@ def get_send_user_name(self):
def get_type():
"""discord"""
return "discord"


class MisskeyClient(BaseClient):
"""
Misskeyを操作するClient
"""

def __init__(self, misskey_client, message):
self.client = misskey_client
self.message = message

def post(self, text):
"""Discordにポストする"""
self._post(text=text)

def upload(self, file, filename=None):
"""ファイルを投稿する"""
with open(file, "rb") as f:
drive_file = self.client.drive_files_create(file=f)
self._post(file_ids=[drive_file["id"]])

def _post(self, text=None, file_ids=None):
self.client.notes_create(
text=text, reply_id=self.message["id"], file_ids=file_ids
)

def get_send_user(self):
"""botを呼び出したユーザーを返す"""
return self.message["user"]["username"]

def get_send_user_name(self):
return self.message["user"]["name"]

@staticmethod
def get_type():
"""misskey"""
return "misskey"
44 changes: 43 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
"""
BotのMain関数
"""
import asyncio
import json
import logging
import logging.config
import sys
from concurrent.futures import ThreadPoolExecutor
from typing import Callable, List

import discord
import websockets
from flask import Flask, escape, jsonify, request
from misskey import Misskey
from slackeventsapi import SlackEventAdapter

import slackbot_settings as conf
from library.clientclass import ApiClient, DiscordClient, SlackClient
from library.clientclass import (
ApiClient,
DiscordClient,
MisskeyClient,
SlackClient,
)
from library.database import Database
from plugins import analyze

Expand Down Expand Up @@ -172,6 +181,39 @@ def main():

if conf.MODE == "discord":
discordClient.run(token=conf.DISCORD_API_TOKEN)
elif conf.MODE == "misskey":
misskey_client = Misskey(conf.MISSKEY_DOMAIN, i=conf.MISSKEY_API_TOKEN)

async def discord_runner():
# pylint: disable=E1101
async with websockets.connect(
"wss://"
+ misskey_client.address
+ "/streaming"
+ "?i="
+ misskey_client.token
) as ws:
await ws.send(
json.dumps(
{"type": "connect", "body": {"channel": "main", "id": "main"}}
)
)
while True:
data = json.loads(await ws.recv())
if data["type"] == "channel" and data["body"]["type"] == "mention":
note = data["body"]["body"]
host = note["user"].get("host")
mentions = note.get("mentions")
if (
(host is None or host == conf.MISSKEY_DOMAIN)
and mentions
and misskey_client.i()["id"] in mentions
):
analyze.analyze_message(
note["text"].replace("\xa0", " ").split(" ", 1)[1]
)(MisskeyClient(misskey_client, note))

asyncio.get_event_loop().run_until_complete(discord_runner())
else:
app.run(host="0.0.0.0", port=conf.PORT)

Expand Down
4 changes: 4 additions & 0 deletions slackbot_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
# Discord用の設定
DISCORD_API_TOKEN = str(os.environ["DISCORD_API_TOKEN"])

# Misskey用の設定
MISSKEY_DOMAIN = str(os.environ["MISSKEY_DOMAIN"])
MISSKEY_API_TOKEN = str(os.environ["MISSKEY_API_TOKEN"])

MODE = str(os.environ["MODE"])

GIT_COMMIT_HASH = os.environ.get("GIT_COMMIT_HASH")
Expand Down