integrations.standard_io.parser

integrations/standard_io/parser.py

 1"""
 2integrations/standard_io/parser.py
 3"""
 4
 5from datetime import datetime
 6from typing import cast
 7
 8from integrations.base.interface import (MessageParserDataMixin,
 9                                         MessageParserInterface)
10from integrations.protocols import MsgData, PostData, StatusData
11
12
13class MessageParser(MessageParserDataMixin, MessageParserInterface):
14    """メッセージ解析クラス"""
15
16    def __init__(self):
17        MessageParserDataMixin.__init__(self)
18        self.data: MsgData = MsgData()
19        self.post: PostData = PostData()
20        self.status: StatusData = StatusData()
21
22    def parser(self, body: dict):
23        self.data.status = "message_append"
24        self.data.channel_id = "dummy"
25        self.data.event_ts = str(datetime.now().timestamp())
26        self.data.thread_ts = self.data.event_ts
27        self.status.source = "standard_io"
28
29        if body.get("event"):
30            body = cast(dict, body["event"])
31
32        if body.get("text"):
33            self.data.text = str(body.get("text", ""))
34        else:
35            self.data.text = ""
36
37        if body.get("channel_name") == "directmessage":  # スラッシュコマンド扱い
38            self.status.command_flg = True
39            self.data.channel_type = "im"
40            self.data.channel_id = body.get("channel_id", "")
41
42    def set_command_flag(self, flg: bool):
43        """スラッシュコマンドフラグを上書き
44
45        Args:
46            flg (bool): フラグ
47        """
48
49        self.status.command_flg = flg
50
51    @property
52    def in_thread(self) -> bool:
53        return False
54
55    @property
56    def is_bot(self) -> bool:
57        return False
58
59    @property
60    def check_updatable(self) -> bool:
61        return True
62
63    @property
64    def ignore_user(self) -> bool:
65        return False
14class MessageParser(MessageParserDataMixin, MessageParserInterface):
15    """メッセージ解析クラス"""
16
17    def __init__(self):
18        MessageParserDataMixin.__init__(self)
19        self.data: MsgData = MsgData()
20        self.post: PostData = PostData()
21        self.status: StatusData = StatusData()
22
23    def parser(self, body: dict):
24        self.data.status = "message_append"
25        self.data.channel_id = "dummy"
26        self.data.event_ts = str(datetime.now().timestamp())
27        self.data.thread_ts = self.data.event_ts
28        self.status.source = "standard_io"
29
30        if body.get("event"):
31            body = cast(dict, body["event"])
32
33        if body.get("text"):
34            self.data.text = str(body.get("text", ""))
35        else:
36            self.data.text = ""
37
38        if body.get("channel_name") == "directmessage":  # スラッシュコマンド扱い
39            self.status.command_flg = True
40            self.data.channel_type = "im"
41            self.data.channel_id = body.get("channel_id", "")
42
43    def set_command_flag(self, flg: bool):
44        """スラッシュコマンドフラグを上書き
45
46        Args:
47            flg (bool): フラグ
48        """
49
50        self.status.command_flg = flg
51
52    @property
53    def in_thread(self) -> bool:
54        return False
55
56    @property
57    def is_bot(self) -> bool:
58        return False
59
60    @property
61    def check_updatable(self) -> bool:
62        return True
63
64    @property
65    def ignore_user(self) -> bool:
66        return False

メッセージ解析クラス

def parser(self, body: dict):
23    def parser(self, body: dict):
24        self.data.status = "message_append"
25        self.data.channel_id = "dummy"
26        self.data.event_ts = str(datetime.now().timestamp())
27        self.data.thread_ts = self.data.event_ts
28        self.status.source = "standard_io"
29
30        if body.get("event"):
31            body = cast(dict, body["event"])
32
33        if body.get("text"):
34            self.data.text = str(body.get("text", ""))
35        else:
36            self.data.text = ""
37
38        if body.get("channel_name") == "directmessage":  # スラッシュコマンド扱い
39            self.status.command_flg = True
40            self.data.channel_type = "im"
41            self.data.channel_id = body.get("channel_id", "")

メッセージ解析

Arguments:
  • body (Any): 解析データ
def set_command_flag(self, flg: bool):
43    def set_command_flag(self, flg: bool):
44        """スラッシュコマンドフラグを上書き
45
46        Args:
47            flg (bool): フラグ
48        """
49
50        self.status.command_flg = flg

スラッシュコマンドフラグを上書き

Arguments:
  • flg (bool): フラグ
in_thread: bool
52    @property
53    def in_thread(self) -> bool:
54        return False

元メッセージへのリプライとなっているか

Returns:

bool: 真偽値

  • True : リプライの形(リプライ/スレッドなど)
  • False : 通常メッセージ
is_bot: bool
56    @property
57    def is_bot(self) -> bool:
58        return False

botのポストかチェック

Returns:

bool: 真偽値

  • True : botのポスト
  • False : ユーザのポスト
check_updatable: bool
60    @property
61    def check_updatable(self) -> bool:
62        return True

DB操作の許可チェック

Returns:

bool: 真偽値

  • True : 許可
  • False : 禁止
ignore_user: bool
64    @property
65    def ignore_user(self) -> bool:
66        return False

ignore_useridに存在するユーザかチェック

Returns:

bool: 真偽値

  • True : 存在する(操作禁止ユーザ)
  • False : 存在しない