Using python script in Incoming Webhook


#1

I hope I posted this in the right place

I need helping to output a string from python script into the curl function of webhook

Note that I am new to python, never worked with json and in the process of configuring mattermost for the first time.

I have the following python script

users=['mark','john','steve']

text=''

for user in users:
    text+=str(user + " ")

print(text)

how can I use text variable which is a string in the curl function in the text option or any way that what I need works

curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": 'HERE'}' https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

I tried that curl --insecure -i -X POST -H ‘Content-Type: application/json’ -d ‘{“text”: ‘python /home/scripts/python-script.py’}’ https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

and got that error
{"id":"Unable to parse incoming data","message":"Unable to parse incoming data","detailed_error":"","request_id":"fpnmfds8zifziyc85oe5eyf3pa","status_code":400}

Any help appreciated Thank you


#2

Hi there,

You’re definitely in the right place. :slight_smile:

The best way to send requests from a Python script to Mattermost is to use one of the Python libraries available like this one. That way you don’t have to worry about outputting the request in a way that curl can understand.

Happy to help if you have any more questions!


#3

Hey paul,
thank you for your reply. took me some time to try it. I need some guidance with it. for starters I get this error ImportError: No module named mattermostdriver even though I installed it with pip3.


#4

Could you post your script so far?


#5
from mattermostdriver import Driver

foo = Driver({
'url':'10.10.10.6',
'scheme':'https',
'port':8065
})

foo.login()

users=['mark','john','steve']

text=' '.join(users)

foo.hooks.call_webhook('https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e', text)

When I run it I get this error

Traceback (most recent call last):
File “script.py”, line 1, in
from mattermostdriver import Driver
ImportError: No module named mattermostdriver


#6

Your script seems to be fine, the import error is a bit strange…
Are you running windows? If yes, maybe (I don’t much windows experience though) python can’t find the modulepath.
Can you run pip3 install mattermostdriver again and post the output?
Are you installing it globally on the system, or in a python virtual environment (I would recommend that btw)


#7

Oh waw, was importing before , now all of a sudden it ouputs that when I try t pip3

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    load_entry_point('pip==1.5.6', 'console_scripts', 'pip3')()
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 356, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2476, in load_entry_point
    return ep.load()
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2190, in load
    ['__name__'])
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 74, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/lib/python3/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/usr/lib/python3/dist-packages/pip/download.py", line 25, in <module>
    from requests.compat import IncompleteRead
ImportError: cannot import name 'IncompleteRead'

OK I solved the error that said:

Traceback (most recent call last):
File “script.py”, line 1, in 
from mattermostdriver import Driver
ImportError: No module named mattermostdriver 

Just by running the script with python3 instead of python

but now I get this new error when running with python3:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/connection.py", line 83, in create_connection
    raise err
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/connection.py", line 73, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 284, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7fadc21c95c0>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='10.10.10.6', port=8065): Max retries exceeded with url: /api/v4/users/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fadc21c95c0>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "kitchen-schedule.py", line 20, in <module>
    foo.login()
  File "/usr/local/lib/python3.4/dist-packages/mattermostdriver/driver.py", line 154, in login
    'token': self.options['mfa_token']
  File "/usr/local/lib/python3.4/dist-packages/mattermostdriver/endpoints/users.py", line 8, in login_user
    return self.client.make_request('post', self.endpoint + '/login', options)
  File "/usr/local/lib/python3.4/dist-packages/mattermostdriver/client.py", line 137, in make_request
    files=files
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='10.10.10.6', port=8065): Max retries exceeded with url: /api/v4/users/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fadc21c95c0>: Failed to establish a new connection: [Errno 111] Connection refused',))

#8

Are you using a ‘valid’ (meaning, it is trusted by your system) certificate for your server?
If not, you either need to add your certificate as a trusted one, or set the option
verify to False

See https://github.com/Vaelor/python-mattermost-driver/blob/master/README.rst#usage ‘optional Options’

Optional options
These options already have useful defaults or are just not needed in every case. In most cases, you won’t need to modify these, especially the basepath.
If you can only use a self signed/insecure certificate, you should set
verify to False. Please double check this if you have any errors while
using a self signed certificate!


#9

I am not using a certificate at all, I put the verify as False did not change anything


#10

Also I am using on Linux


#11

If you do not use a certificate, you don’t use https, so you should have scheme: http.
Also, if you do not use https, you don’t need to bother with verify - the option does nothing when http is used.


#12

sorry scatch that I was thinking of another mattermost server I have, I am using a certificate but it’s not trusted. Anyways I tried both and not working


#13

When I import with pip i get this error

Downloading/unpacking mattermostdriver
  Downloading mattermostdriver-4.4.0.tar.gz
  Running setup.py (path:/tmp/pip-build-rHSJk9/mattermostdriver/setup.py) egg_info for package mattermostdriver
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip-build-rHSJk9/mattermostdriver/setup.py", line 16, in <module>
        raise Exception("python-mattermost-driver requires Python >= 3.4.")
    Exception: python-mattermost-driver requires Python >= 3.4.
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/tmp/pip-build-rHSJk9/mattermostdriver/setup.py", line 16, in <module>

    raise Exception("python-mattermost-driver requires Python >= 3.4.")

Exception: python-mattermost-driver requires Python >= 3.4.

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-rHSJk9/mattermostdriver
Storing debug log for failure in /root/.pip/pip.log

#14

Well another problem could be your webserver setup.
Errno [111] Connection refused could also mean there is nothing listening on your specified location.

Looking at the curl request you had in your initial post, is the port 8065 correct?


#15

Pip points to the version for python2 in your setup. The mattermostdriver works only with Python >= 3.4 - that is why you get the exception

Exception: python-mattermost-driver requires Python >= 3.4.


#16

yes listen address in mattermost is 127.0.0.1:8065


#17

If the listen address is 127.0.0.1, your url would be incorrect
( ‘url’:‘10.10.10.6’ )

There are two possibilities I see there:
You have the mattermost server and the script running on the same server.
In this case, you should probably use 127.0.0.1 as the url, too.

Or, the python script is not running on the same server as mattermost with for example nginx in front, so you can access mattermost over port 80/443, and redirect everything to 127.0.0.1.
Then you ofc need to use 10.10.10.6 with port 443 (https) and not 8065.


#18

I did that, makes all the sense. Changed it to 127.0.0.1 because it is on the same server.
then got this error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 326, in connect
    ssl_context=context)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python3.4/ssl.py", line 364, in wrap_socket
    _context=self)
  File "/usr/lib/python3.4/ssl.py", line 577, in __init__
    self.do_handshake()
  File "/usr/lib/python3.4/ssl.py", line 804, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='127.0.0.1', port=8065): Max retries exceeded with url: /api/v4/users/login (Caused by SSLError(SSLError(1, '[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:600)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/scripts/kitchen-schedule.py", line 21, in <module>
    foo.login()
  File "/usr/local/lib/python3.4/dist-packages/mattermostdriver/driver.py", line 154, in login
    'token': self.options['mfa_token']
  File "/usr/local/lib/python3.4/dist-packages/mattermostdriver/endpoints/users.py", line 8, in login_user
    return self.client.make_request('post', self.endpoint + '/login', options)
  File "/usr/local/lib/python3.4/dist-packages/mattermostdriver/client.py", line 137, in make_request
    files=files
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='127.0.0.1', port=8065): Max retries exceeded with url: /api/v4/users/login (Caused by SSLError(SSLError(1, '[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:600)'),))

so I tried with http and then got this:

Traceback (most recent call last):
  File "script.py", line 27, in <module>
    foo.hooks.call_webhook('https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e', text)
AttributeError: 'Driver' object has no attribute 'hooks'

#19

Ah shit! That is an error in my documentation! I am sorry.
You need to call foo.webhooks.call_webhook not foo.hooks!

Will fix this ASAP, ty for finding it :wink:


#20

Beautiful :slight_smile:
No errors anymore but there is no output in the channel. I have a feeling it has something to do with the scheme . Because I have mattermost with untrusted certificate, but if I put scheme https with verify false or true it gives an error. I will try these steps when I step up my new mattermost vm