Skip to content

Instantly share code, notes, and snippets.

@Dhole
Created July 5, 2017 08:42
Show Gist options
  • Save Dhole/6d9a274aef4796d1ddf94a7951fa3a19 to your computer and use it in GitHub Desktop.
Save Dhole/6d9a274aef4796d1ddf94a7951fa3a19 to your computer and use it in GitHub Desktop.
OLM_BASE_DIR = '/home/dev/git/olm'
MY_DEVICE_ID = 'MY_MATRIX_PYTHON_SDK_DEVICE'
MY_USER_ID = '@ray_test:matrix.org'
MY_PASSWORD = '...'
HOMIE_USER_ID = '@dhole:matrix.org'
HOMESERVER = 'https://matrix.org'
import os
import sys
sys.path.append(os.path.join(OLM_BASE_DIR, 'python'))
from matrix_client import client, api, crypto
cli = client.MatrixClient(HOMESERVER)
olm_device = crypto.OlmDevice.load_or_create_olm_device(cli.api, MY_USER_ID, MY_DEVICE_ID)
# Pass to the device_id to the login method otherwise Synapse will auto-generate
# a new one for us
print("Logging in...")
cli.login_with_password(MY_USER_ID, MY_PASSWORD, device_id=MY_DEVICE_ID)
print("Uploading device keys...")
olm_device.upload_device_keys()
print("Starting olm session...")
#sessions = olm_device.create_outbound_sessions_to_user(HOMIE_USER_ID)
sessions = olm_device.ensure_outbound_sessions_for_user(HOMIE_USER_ID)
print(sessions)
def get_first_session(d):
return list(d.values())[0] if isinstance(d, dict) else d
outbound_session = get_first_session(sessions[0])
print(outbound_session)
# room should be a room with encryption enabled via. the m.room.encryption event
# to do this create a room and an m.room_encryption event e.g.
room = cli.create_room('homes_are_invited7')
room.invite_user(HOMIE_USER_ID) # Accept Invite
cli.api.send_state_event(room.room_id, 'm.room.encryption',
{'algorithm': 'm.olm.v1.curve25519-aes-sha2'}
)
print("Sending encrypted message")
olm_device.send_encrypted_message_to_session(room.room_id, outbound_session, 'Hello via. e2e on matrix-python-sdk')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment