Git Product home page Git Product logo

matrix-nio's Introduction

nio

Build Status codecov license Documentation Status #nio

nio is a multilayered matrix client library. The underlying base layer doesn't do any IO on its own. On top of the base layer, a no-IO HTTP client implementation exists, as well as a full fledged batteries included asyncio layer using aiohttp.

Documentation

The full API documentation for nio can be found at https://matrix-nio.readthedocs.io

Installation

To install nio, simply use pip:

$ pip install matrix-nio

Note that this installs nio without end-to-end encryption support. For e2ee support, python-olm is needed which requires the libolm C library (version 3.x).

After libolm has been installed, the e2ee enabled version of nio can be installed using pip:

$ pip install "matrix-nio[e2e]"

Usage

Unless special requirements disallow the usage of asyncio, by far the easiest way to use nio is using the asyncio layer.

Please do note that these examples require python 3.5+ for the async/await syntax. nio on the other hand works with older python versions as well.

Sending a message

import asyncio
from nio import AsyncClient

async def main():
    client = AsyncClient("https://example.org", "@alice:example.org")
    
    await client.login("hunter1")
    await client.room_send(
        room_id="!test:example.org",
        message_type="m.room.message",
        content={
            "msgtype": "m.text",
            "body": "Hello World"
        }
    )
    await client.close()

asyncio.get_event_loop().run_until_complete(main())

Receiving messages

import asyncio
from nio import (AsyncClient, RoomMessageText)

async def message_cb(room, event):
    print(
        "Message received for room {} | {}: {}".format(
            room.display_name, room.user_name(event.sender), event.body
        )
    )

async def main():
    client = AsyncClient("https://example.org", "@alice:example.org")
    client.add_event_callback(message_cb, RoomMessageText)

    await client.login("hunter1")
    await client.sync_forever(timeout=30000)

asyncio.get_event_loop().run_until_complete(main())

matrix-nio's People

Contributors

anoadragon453 avatar arthurlutz avatar auscompgeek avatar clouetb avatar cy8aer avatar diabonas avatar dkasak avatar emma1969 avatar jcgruenhage avatar kechpaja avatar leftshift avatar mirukana avatar nullisnot0 avatar poljar avatar reivilibre avatar sblondon avatar spiritcroc avatar trygveaa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.