message processing¶
Message processing is supported with core4 endpoint /core4/api/v1/event.
See also events processing. The default channel for chat messages is message.
The following example demonstrates the messaging system with two users _user1_ and _user2_. Two follow the example you will have to open two dedicated terminals and enter the core4 environment.
preparation¶
First you will have to create two users.
from requests import post, get
# login as admin
login = get("http://devops:5001/core4/api/v1/login?username=admin&password=hans")
token = login.json()["data"]["token"]
# create user1
rv = post("http://devops:5001/core4/api/v1/roles",
headers={"Authorization": "Bearer " + token},
json={
"name": "user1",
"realname": "Test User 1",
"email": "test1@plan-net.com",
"password": "very secret",
"role": ["standard_user"]
})
assert rv.status_code == 200
# create user2
rv = post("http://devops:5001/core4/api/v1/roles",
headers={"Authorization": "Bearer " + token},
json={
"name": "user2",
"realname": "Test User 2",
"email": "test2@plan-net.com",
"password": "very secret",
"role": ["standard_user"]
})
assert rv.status_code == 200
Verify both users exists with the role endpoint.
rv = get("http://devops:5001/core4/api/v1/roles",
headers={"Authorization": "Bearer " + token})
To simulate a chat between both users, open two terminals and start the simple terminal chat client script.
"""
chat - simple chat terminal client
Usage:
chat HOSTNAME USERNAME [PASSWORD] [--ssl]
Options:
-h --help Show this screen
--ssl use secure protocol (https/wss)
"""
import json
from getpass import getpass
from threading import Thread
from docopt import docopt
from requests import get
from websocket import create_connection
def listening(ws):
while True:
data = json.loads(ws.recv())
if "channel" in data:
print("{} > {}".format(data["author"], data["data"]))
def main():
args = docopt(__doc__, help=True)
if args["PASSWORD"] is None:
args["PASSWORD"] = getpass("enter password: ")
LOGIN_URL = "{HOSTNAME:s}/core4/api/v1/login" \
"?username={USERNAME:s}&password={PASSWORD:s}"
SOCKET_URL = "{HOSTNAME:s}/core4/api/v1/event?token={token:s}"
if args["--ssl"]:
url = "https://"
ws = "wss://"
else:
url = "http://"
ws = "ws://"
# login
login_url = url + LOGIN_URL.format(**args)
login = get(login_url, verify=False)
assert login.status_code == 200
token = login.json()["data"]["token"]
print("> login")
# connect
ws_url = ws + SOCKET_URL.format(token=token, **args)
ws = create_connection(ws_url)
assert ws.connected
print("> web socket connected")
# announce interest
ws.send(json.dumps({"type": "interest", "data": ["message", "system"]}))
thread = Thread(target=listening, args=(ws,))
thread.start()
while True:
message = input()
ws.send(json.dumps(
{"type": "message", "channel": "message", "text": message}))
if __name__ == '__main__':
main()
Before starting the script, ensure the CoreApiServer is up and
running with:
cd core4
source enter_env
python core4/api/v1/server.py
In a second terminal, start the script for _user1_ with:
cd docs/source/example
python chat.py devops:5001 user1 "very secret"
In a third terminal, start the script for _user2_ with:
cd docs/source/example
python chat.py devops:5001 user2 "very secret"