Repository: opsdroid/opsdroid (language: python)

Issue:
Adding user id Parmeter in Event class
# Description

_Adding user_id keyword in Event class. This is generally implemented as the human readable name of the user. For a number of events, we also need the machine readable user id for the event._

Fixes #1082


## Status
 **Ready**


## Type of change

- New feature (non-breaking change which adds functionality)

# How Has This Been Tested?

_Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration._

- Test A
- Test B


# Checklist:

- [ ] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation (if applicable)
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes


Mattermost connector is broken
# Description
The Mattermost connector is broken. It can connect to a Mattermost instance, but when sending a message to OpsDroid (using the Hello skill) you get:

```
ERROR opsdroid.core: Exception when running skill 'hello'.
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/opsdroid/core.py", line 465, in run_skill
    return await skill(event)
  File "/root/.local/share/opsdroid/opsdroid-modules/skill/hello/__init__.py", line 13, in hello
    await message.respond(text)
  File "/usr/local/lib/python3.8/site-packages/opsdroid/events.py", line 278, in respond
    "thinking-delay" in self.connector.configuration
AttributeError: 'NoneType' object has no attribute 'configuration'
WARNING mattermostdriver.websocket: Failed to establish websocket connection: 'NoneType' object has no attribute 'configuration'
```

## Steps to Reproduce
Configure the Mattermost connector and the Hello skill, start Opsdroid and send a message to the bot in Mattermost.


## Expected Functionality
A reply form the Hello skill.


## Experienced Functionality
No reply, and the above error in the Opsdroid logs.

## Versions
- **Opsdroid version:** 0.22.0
- **Python version:** 3.8
- **OS/Docker version:** N/A

## Configuration File
Please include your version of the configuration file below.

```yaml
welcome-message: false

connectors:
  ## Mattermost (core)
  mattermost:
    # Required
    token: "<redacted>"
    url: "<redacted>"
    team-name: "<redacted>"
    # Optional
    scheme: "https" # default: https
    port: 443 # default: 8065
    ssl-verify: true # default: true
    connect-timeout: 30 # default: 30

skills:
  ## Hello (https://github.com/opsdroid/skill-hello)
  hello: {}
  ## Seen (https://github.com/opsdroid/skill-seen)
  seen: {}
```

## Additional Details
Looks like this the Mattermost connector was missed in #1116 -- I'll submit a PR shortly to correct this.


Discussion / hints:
# [Codecov](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=h1) Report
> Merging [#1116](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=desc) into [master](https://codecov.io/gh/opsdroid/opsdroid/commit/12b38930d8e317583ff29e57ff826847048badd3?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/graphs/tree.svg?width=650&token=9qJfZL4bOO&height=150&src=pr)](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1116   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          48      48           
  Lines        2617    2619    +2     
======================================
+ Hits         2617    2619    +2
```


| [Impacted Files](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [opsdroid/connector/matrix/create\_events.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL21hdHJpeC9jcmVhdGVfZXZlbnRzLnB5) | `100% <ø> (ø)` | :arrow_up: |
| [opsdroid/connector/github/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL2dpdGh1Yi9fX2luaXRfXy5weQ==) | `100% <ø> (ø)` | :arrow_up: |
| [opsdroid/connector/rocketchat/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL3JvY2tldGNoYXQvX19pbml0X18ucHk=) | `100% <ø> (ø)` | :arrow_up: |
| [opsdroid/connector/slack/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL3NsYWNrL19faW5pdF9fLnB5) | `100% <ø> (ø)` | :arrow_up: |
| [opsdroid/connector/gitter/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL2dpdHRlci9fX2luaXRfXy5weQ==) | `100% <ø> (ø)` | :arrow_up: |
| [opsdroid/connector/webexteams/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL3dlYmV4dGVhbXMvX19pbml0X18ucHk=) | `100% <ø> (ø)` | :arrow_up: |
| [opsdroid/message.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvbWVzc2FnZS5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [opsdroid/connector/telegram/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1116/diff?src=pr&el=tree#diff-b3BzZHJvaWQvY29ubmVjdG9yL3RlbGVncmFtL19faW5pdF9fLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=footer). Last update [12b3893...6997835](https://codecov.io/gh/opsdroid/opsdroid/pull/1116?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@Cadair  Everything changed as suggested thanks for suggestion
@himanshupathak21061998 Sorry I haven't looked at this in a while, can you resolve the merge conflicts and I will review it and get it merged.
@Cadair  all conflicts have been resolved :grinning: 
Thanks @FabioRosado for reviewing. It's my pleasure to work on this. :smile:  
This looks really good. I just have one question (which doesn't need to be addressed to merge this).

Should we make it so that all the connectors put `user_id=` in all their events? i.e. even if it's a duplication of the `user` field should we make it so that it's populated. This would mean that I could always access it irrespective of the connector.

One way we could do this which would be minimally disruptive, is to have the default in the `Event` class be `self.user_id = user_id or user` so that it defaults to `user`?
@himanshupathak21061998 are you planning on picking this up again?
@jacobtomlinson yes I will.
Please suggest if you want any changes in this.
Thanks @himanshupathak21061998 :tada: 


Fix patch:
diff --git a/opsdroid/connector/mattermost/__init__.py b/opsdroid/connector/mattermost/__init__.py
index 9f6f90a77..a04d2b5cc 100644
--- a/opsdroid/connector/mattermost/__init__.py
+++ b/opsdroid/connector/mattermost/__init__.py
@@ -95,10 +95,10 @@ async def process_message(self, raw_message):
             post = json.loads(data["post"])
             await self.opsdroid.parse(
                 Message(
-                    post["message"],
-                    data["sender_name"],
-                    data["channel_name"],
-                    self,
+                    text=post["message"],
+                    user=data["sender_name"],
+                    target=data["channel_name"],
+                    connector=self,
                     raw_event=message,
                 )
             )


Test patch:
diff --git a/tests/test_connector_mattermost.py b/tests/test_connector_mattermost.py
index 8236c2b6d..1bcf5b8a9 100644
--- a/tests/test_connector_mattermost.py
+++ b/tests/test_connector_mattermost.py
@@ -170,6 +170,8 @@ async def test_send_message(self):
         )
         connector.mm_driver.posts = mock.Mock()
         connector.mm_driver.posts.create_post = mock.MagicMock()
-        await connector.send(Message("test", "user", "room", connector))
+        await connector.send(
+            Message(text="test", user="user", target="room", connector=connector)
+        )
         self.assertTrue(connector.mm_driver.channels.get_channel_by_name_and_team_name)
         self.assertTrue(connector.mm_driver.posts.create_post.called)