52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*
|
|
|
|
from matrix_client.client import MatrixClient
|
|
from matrix_client.api import MatrixRequestError
|
|
|
|
import os
|
|
import sys
|
|
|
|
if len(sys.argv) > 1:
|
|
config = __import__(sys.argv[1])
|
|
else:
|
|
import config
|
|
|
|
class Bot:
|
|
def __init__(self):
|
|
self.client = MatrixClient(config.BOT_SERVER)
|
|
try:
|
|
self.client.login_with_password(config.BOT_USERNAME, config.BOT_PASSWORD)
|
|
except MatrixRequestError as e:
|
|
print(e)
|
|
if e.code == 403:
|
|
print("Bad username/password")
|
|
sys.exit(1)
|
|
|
|
self.plugin_manager = PluginManager(self, config)
|
|
self.rooms = {}
|
|
for room_id, room_data in config.rooms.items():
|
|
self.rooms[room_id] = self.client.join_room(room_id)
|
|
self.rooms[room_id].add_listener(self.callback)
|
|
|
|
def run(self):
|
|
self.client.start_listener_thread()
|
|
#return client.sync_thread
|
|
|
|
while True:
|
|
input()
|
|
|
|
def callback(self, room, event):
|
|
if "@" + config.BOT_USERNAME in event['sender']:
|
|
return
|
|
|
|
if event['type'] == "m.room.message":
|
|
self.plugin_manager.notify_message(room, event)
|
|
|
|
def main():
|
|
bot = Bot()
|
|
bot.run()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|