prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Infinite self-responses in Mattermost connector
  After fixing the Mattermost connector with PR #1774 it turns out it suffers from the same infinite self-response problem  (#1691) as was fixed for the Gitter connector in #1692.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `opsdroid/connector/mattermost/__init__.py`
  Content:
  ```
  1 """A connector for Mattermost."""
  2 import logging
  3 import json
  4 
  5 from mattermostdriver import Driver, Websocket
  6 from voluptuous import Required
  7 
  8 from opsdroid.connector import Connector, register_event
  9 from opsdroid.events import Message
  10 
  11 _LOGGER = logging.getLogger(__name__)
  12 CONFIG_SCHEMA = {
  13     Required("token"): str,
  14     Required("url"): str,
  15     Required("team-name"): str,
  16     "scheme": str,
  17     "port": int,
  18     "ssl-verify": bool,
  19     "connect-timeout": int,
  20 }
  21 
  22 
  23 class ConnectorMattermost(Connector):
  24     """A connector for Mattermost."""
  25 
  26     def __init__(self, config, opsdroid=None):
  27         """Create the connector."""
  28         super().__init__(config, opsdroid=opsdroid)
  29         _LOGGER.debug(_("Starting Mattermost connector"))
  30         self.name = "mattermost"
  31         self.token = config["token"]
  32         self.url = config["url"]
  33         self.team_name = config["team-name"]
  34         self.scheme = config.get("scheme", "https")
  35         self.port = config.get("port", 8065)
  36         self.verify = config.get("ssl-verify", True)
  37         self.timeout = config.get("connect-timeout", 30)
  38         self.request_timeout = None
  39         self.mfa_token = None
  40         self.debug = False
  41         self.listening = True
  42 
  43         self.mm_driver = Driver(
  44             {
  45                 "url": self.url,
  46                 "token": self.token,
  47                 "scheme": self.scheme,
  48                 "port": self.port,
  49                 "verify": self.verify,
  50                 "timeout": self.timeout,
  51                 "request_timeout": self.request_timeout,
  52                 "mfa_token": self.mfa_token,
  53                 "debug": self.debug,
  54             }
  55         )
  56 
  57     async def connect(self):
  58         """Connect to the chat service."""
  59         _LOGGER.info(_("Connecting to Mattermost"))
  60 
  61         login_response = self.mm_driver.login()
  62 
  63         _LOGGER.info(login_response)
  64 
  65         if "id" in login_response:
  66             self.bot_id = login_response["id"]
  67         if "username" in login_response:
  68             self.bot_name = login_response["username"]
  69 
  70         _LOGGER.info(_("Connected as %s"), self.bot_name)
  71 
  72         self.mm_driver.websocket = Websocket(
  73             self.mm_driver.options, self.mm_driver.client.token
  74         )
  75 
  76         _LOGGER.info(_("Connected successfully"))
  77 
  78     async def disconnect(self):
  79         """Disconnect from Mattermost."""
  80         self.listening = False
  81         self.mm_driver.logout()
  82 
  83     async def listen(self):
  84         """Listen for and parse new messages."""
  85         await self.mm_driver.websocket.connect(self.process_message)
  86 
  87     async def process_message(self, raw_message):
  88         """Process a raw message and pass it to the parser."""
  89         _LOGGER.info(raw_message)
  90 
  91         message = json.loads(raw_message)
  92 
  93         if "event" in message and message["event"] == "posted":
  94             data = message["data"]
  95             post = json.loads(data["post"])
  96             await self.opsdroid.parse(
  97                 Message(
  98                     text=post["message"],
  99                     user=data["sender_name"],
  100                     target=data["channel_name"],
  101                     connector=self,
  102                     raw_event=message,
  103                 )
  104             )
  105 
  106     @register_event(Message)
  107     async def send_message(self, message):
  108         """Respond with a message."""
  109         _LOGGER.debug(
  110             _("Responding with: '%s' in room  %s"), message.text, message.target
  111         )
  112         channel_id = self.mm_driver.channels.get_channel_by_name_and_team_name(
  113             self.team_name, message.target
  114         )["id"]
  115         self.mm_driver.posts.create_post(
  116             options={"channel_id": channel_id, "message": message.text}
  117         )
  118
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/opsdroid/connector/mattermost/__init__.py b/opsdroid/connector/mattermost/__init__.py
  --- a/opsdroid/connector/mattermost/__init__.py
  +++ b/opsdroid/connector/mattermost/__init__.py
  @@ -39,6 +39,7 @@
           self.mfa_token = None
           self.debug = False
           self.listening = True
  +        self.bot_id = None
   
           self.mm_driver = Driver(
               {
  @@ -66,8 +67,7 @@
               self.bot_id = login_response["id"]
           if "username" in login_response:
               self.bot_name = login_response["username"]
  -
  -        _LOGGER.info(_("Connected as %s"), self.bot_name)
  +            _LOGGER.info(_("Connected as %s"), self.bot_name)
   
           self.mm_driver.websocket = Websocket(
               self.mm_driver.options, self.mm_driver.client.token
  @@ -93,15 +93,18 @@
           if "event" in message and message["event"] == "posted":
               data = message["data"]
               post = json.loads(data["post"])
  -            await self.opsdroid.parse(
  -                Message(
  -                    text=post["message"],
  -                    user=data["sender_name"],
  -                    target=data["channel_name"],
  -                    connector=self,
  -                    raw_event=message,
  +            # don't parse our own messages (https://github.com/opsdroid/opsdroid/issues/1775)
  +            # (but also parse if somehow our bot_id is unknown, like in the unit tests)
  +            if self.bot_id is None or self.bot_id != post["user_id"]:
  +                await self.opsdroid.parse(
  +                    Message(
  +                        text=post["message"],
  +                        user=data["sender_name"],
  +                        target=data["channel_name"],
  +                        connector=self,
  +                        raw_event=message,
  +                    )
                   )
  -            )
   
       @register_event(Message)
       async def send_message(self, message):
  
verification_info: {"golden_diff": "diff --git a/opsdroid/connector/mattermost/__init__.py b/opsdroid/connector/mattermost/__init__.py\n--- a/opsdroid/connector/mattermost/__init__.py\n+++ b/opsdroid/connector/mattermost/__init__.py\n@@ -39,6 +39,7 @@\n         self.mfa_token = None\n         self.debug = False\n         self.listening = True\n+        self.bot_id = None\n \n         self.mm_driver = Driver(\n             {\n@@ -66,8 +67,7 @@\n             self.bot_id = login_response[\"id\"]\n         if \"username\" in login_response:\n             self.bot_name = login_response[\"username\"]\n-\n-        _LOGGER.info(_(\"Connected as %s\"), self.bot_name)\n+            _LOGGER.info(_(\"Connected as %s\"), self.bot_name)\n \n         self.mm_driver.websocket = Websocket(\n             self.mm_driver.options, self.mm_driver.client.token\n@@ -93,15 +93,18 @@\n         if \"event\" in message and message[\"event\"] == \"posted\":\n             data = message[\"data\"]\n             post = json.loads(data[\"post\"])\n-            await self.opsdroid.parse(\n-                Message(\n-                    text=post[\"message\"],\n-                    user=data[\"sender_name\"],\n-                    target=data[\"channel_name\"],\n-                    connector=self,\n-                    raw_event=message,\n+            # don't parse our own messages (https://github.com/opsdroid/opsdroid/issues/1775)\n+            # (but also parse if somehow our bot_id is unknown, like in the unit tests)\n+            if self.bot_id is None or self.bot_id != post[\"user_id\"]:\n+                await self.opsdroid.parse(\n+                    Message(\n+                        text=post[\"message\"],\n+                        user=data[\"sender_name\"],\n+                        target=data[\"channel_name\"],\n+                        connector=self,\n+                        raw_event=message,\n+                    )\n                 )\n-            )\n \n     @register_event(Message)\n     async def send_message(self, message):\n", "issue": "Infinite self-responses in Mattermost connector\nAfter fixing the Mattermost connector with PR #1774 it turns out it suffers from the same infinite self-response problem  (#1691) as was fixed for the Gitter connector in #1692.\n", "before_files": [{"content": "\"\"\"A connector for Mattermost.\"\"\"\nimport logging\nimport json\n\nfrom mattermostdriver import Driver, Websocket\nfrom voluptuous import Required\n\nfrom opsdroid.connector import Connector, register_event\nfrom opsdroid.events import Message\n\n_LOGGER = logging.getLogger(__name__)\nCONFIG_SCHEMA = {\n    Required(\"token\"): str,\n    Required(\"url\"): str,\n    Required(\"team-name\"): str,\n    \"scheme\": str,\n    \"port\": int,\n    \"ssl-verify\": bool,\n    \"connect-timeout\": int,\n}\n\n\nclass ConnectorMattermost(Connector):\n    \"\"\"A connector for Mattermost.\"\"\"\n\n    def __init__(self, config, opsdroid=None):\n        \"\"\"Create the connector.\"\"\"\n        super().__init__(config, opsdroid=opsdroid)\n        _LOGGER.debug(_(\"Starting Mattermost connector\"))\n        self.name = \"mattermost\"\n        self.token = config[\"token\"]\n        self.url = config[\"url\"]\n        self.team_name = config[\"team-name\"]\n        self.scheme = config.get(\"scheme\", \"https\")\n        self.port = config.get(\"port\", 8065)\n        self.verify = config.get(\"ssl-verify\", True)\n        self.timeout = config.get(\"connect-timeout\", 30)\n        self.request_timeout = None\n        self.mfa_token = None\n        self.debug = False\n        self.listening = True\n\n        self.mm_driver = Driver(\n            {\n                \"url\": self.url,\n                \"token\": self.token,\n                \"scheme\": self.scheme,\n                \"port\": self.port,\n                \"verify\": self.verify,\n                \"timeout\": self.timeout,\n                \"request_timeout\": self.request_timeout,\n                \"mfa_token\": self.mfa_token,\n                \"debug\": self.debug,\n            }\n        )\n\n    async def connect(self):\n        \"\"\"Connect to the chat service.\"\"\"\n        _LOGGER.info(_(\"Connecting to Mattermost\"))\n\n        login_response = self.mm_driver.login()\n\n        _LOGGER.info(login_response)\n\n        if \"id\" in login_response:\n            self.bot_id = login_response[\"id\"]\n        if \"username\" in login_response:\n            self.bot_name = login_response[\"username\"]\n\n        _LOGGER.info(_(\"Connected as %s\"), self.bot_name)\n\n        self.mm_driver.websocket = Websocket(\n            self.mm_driver.options, self.mm_driver.client.token\n        )\n\n        _LOGGER.info(_(\"Connected successfully\"))\n\n    async def disconnect(self):\n        \"\"\"Disconnect from Mattermost.\"\"\"\n        self.listening = False\n        self.mm_driver.logout()\n\n    async def listen(self):\n        \"\"\"Listen for and parse new messages.\"\"\"\n        await self.mm_driver.websocket.connect(self.process_message)\n\n    async def process_message(self, raw_message):\n        \"\"\"Process a raw message and pass it to the parser.\"\"\"\n        _LOGGER.info(raw_message)\n\n        message = json.loads(raw_message)\n\n        if \"event\" in message and message[\"event\"] == \"posted\":\n            data = message[\"data\"]\n            post = json.loads(data[\"post\"])\n            await self.opsdroid.parse(\n                Message(\n                    text=post[\"message\"],\n                    user=data[\"sender_name\"],\n                    target=data[\"channel_name\"],\n                    connector=self,\n                    raw_event=message,\n                )\n            )\n\n    @register_event(Message)\n    async def send_message(self, message):\n        \"\"\"Respond with a message.\"\"\"\n        _LOGGER.debug(\n            _(\"Responding with: '%s' in room  %s\"), message.text, message.target\n        )\n        channel_id = self.mm_driver.channels.get_channel_by_name_and_team_name(\n            self.team_name, message.target\n        )[\"id\"]\n        self.mm_driver.posts.create_post(\n            options={\"channel_id\": channel_id, \"message\": message.text}\n        )\n", "path": "opsdroid/connector/mattermost/__init__.py"}], "after_files": [{"content": "\"\"\"A connector for Mattermost.\"\"\"\nimport logging\nimport json\n\nfrom mattermostdriver import Driver, Websocket\nfrom voluptuous import Required\n\nfrom opsdroid.connector import Connector, register_event\nfrom opsdroid.events import Message\n\n_LOGGER = logging.getLogger(__name__)\nCONFIG_SCHEMA = {\n    Required(\"token\"): str,\n    Required(\"url\"): str,\n    Required(\"team-name\"): str,\n    \"scheme\": str,\n    \"port\": int,\n    \"ssl-verify\": bool,\n    \"connect-timeout\": int,\n}\n\n\nclass ConnectorMattermost(Connector):\n    \"\"\"A connector for Mattermost.\"\"\"\n\n    def __init__(self, config, opsdroid=None):\n        \"\"\"Create the connector.\"\"\"\n        super().__init__(config, opsdroid=opsdroid)\n        _LOGGER.debug(_(\"Starting Mattermost connector\"))\n        self.name = \"mattermost\"\n        self.token = config[\"token\"]\n        self.url = config[\"url\"]\n        self.team_name = config[\"team-name\"]\n        self.scheme = config.get(\"scheme\", \"https\")\n        self.port = config.get(\"port\", 8065)\n        self.verify = config.get(\"ssl-verify\", True)\n        self.timeout = config.get(\"connect-timeout\", 30)\n        self.request_timeout = None\n        self.mfa_token = None\n        self.debug = False\n        self.listening = True\n        self.bot_id = None\n\n        self.mm_driver = Driver(\n            {\n                \"url\": self.url,\n                \"token\": self.token,\n                \"scheme\": self.scheme,\n                \"port\": self.port,\n                \"verify\": self.verify,\n                \"timeout\": self.timeout,\n                \"request_timeout\": self.request_timeout,\n                \"mfa_token\": self.mfa_token,\n                \"debug\": self.debug,\n            }\n        )\n\n    async def connect(self):\n        \"\"\"Connect to the chat service.\"\"\"\n        _LOGGER.info(_(\"Connecting to Mattermost\"))\n\n        login_response = self.mm_driver.login()\n\n        _LOGGER.info(login_response)\n\n        if \"id\" in login_response:\n            self.bot_id = login_response[\"id\"]\n        if \"username\" in login_response:\n            self.bot_name = login_response[\"username\"]\n            _LOGGER.info(_(\"Connected as %s\"), self.bot_name)\n\n        self.mm_driver.websocket = Websocket(\n            self.mm_driver.options, self.mm_driver.client.token\n        )\n\n        _LOGGER.info(_(\"Connected successfully\"))\n\n    async def disconnect(self):\n        \"\"\"Disconnect from Mattermost.\"\"\"\n        self.listening = False\n        self.mm_driver.logout()\n\n    async def listen(self):\n        \"\"\"Listen for and parse new messages.\"\"\"\n        await self.mm_driver.websocket.connect(self.process_message)\n\n    async def process_message(self, raw_message):\n        \"\"\"Process a raw message and pass it to the parser.\"\"\"\n        _LOGGER.info(raw_message)\n\n        message = json.loads(raw_message)\n\n        if \"event\" in message and message[\"event\"] == \"posted\":\n            data = message[\"data\"]\n            post = json.loads(data[\"post\"])\n            # don't parse our own messages (https://github.com/opsdroid/opsdroid/issues/1775)\n            # (but also parse if somehow our bot_id is unknown, like in the unit tests)\n            if self.bot_id is None or self.bot_id != post[\"user_id\"]:\n                await self.opsdroid.parse(\n                    Message(\n                        text=post[\"message\"],\n                        user=data[\"sender_name\"],\n                        target=data[\"channel_name\"],\n                        connector=self,\n                        raw_event=message,\n                    )\n                )\n\n    @register_event(Message)\n    async def send_message(self, message):\n        \"\"\"Respond with a message.\"\"\"\n        _LOGGER.debug(\n            _(\"Responding with: '%s' in room  %s\"), message.text, message.target\n        )\n        channel_id = self.mm_driver.channels.get_channel_by_name_and_team_name(\n            self.team_name, message.target\n        )[\"id\"]\n        self.mm_driver.posts.create_post(\n            options={\"channel_id\": channel_id, \"message\": message.text}\n        )\n", "path": "opsdroid/connector/mattermost/__init__.py"}]}