From 680af79fd86deef32f3cff2a7a82d80d2b9f253f Mon Sep 17 00:00:00 2001 From: Ville Ranki Date: Wed, 11 Dec 2019 00:04:24 +0200 Subject: [PATCH] Start modules in run(), loc tuning --- bot.py | 20 ++++++++++---------- modules/loc.py | 14 +++++--------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/bot.py b/bot.py index 19259ac..0c8e136 100755 --- a/bot.py +++ b/bot.py @@ -156,16 +156,6 @@ class Bot: self.join_on_invite = os.getenv('JOIN_ON_INVITE') self.get_modules() - - print(f'Starting {len(self.modules)} modules..') - - for modulename, moduleobject in self.modules.items(): - print('Starting', modulename, '..') - if "matrix_start" in dir(moduleobject): - try: - moduleobject.matrix_start(bot) - except: - traceback.print_exc(file=sys.stderr) def stop(self): print(f'Stopping {len(self.modules)} modules..') @@ -183,6 +173,16 @@ class Bot: print("Logged in with password, access token:", self.client.access_token) await self.client.sync() + + print(f'Starting {len(self.modules)} modules..') + for modulename, moduleobject in self.modules.items(): + print('Starting', modulename, '..') + if "matrix_start" in dir(moduleobject): + try: + moduleobject.matrix_start(bot) + except: + traceback.print_exc(file=sys.stderr) + self.poll_task = asyncio.get_event_loop().create_task(self.poll_timer()) if self.client.logged_in: diff --git a/modules/loc.py b/modules/loc.py index dae7f51..a7cc85c 100644 --- a/modules/loc.py +++ b/modules/loc.py @@ -10,7 +10,6 @@ class MatrixModule: async def unknown_cb(self, room, event): if event.msgtype != 'm.location': return - location_text = event.content['body'] # Fallback if body is empty @@ -29,15 +28,14 @@ class MatrixModule: osm_link = 'https://www.openstreetmap.org/?mlat=' + latlon[0] + "&mlon=" + latlon[1] - plain = sender + ' - ' + osm_link - html = f'{sender} - {location_text}' + plain = sender + ' 🚩 ' + osm_link + html = f'{sender} 🚩 {location_text}' await self.bot.send_html(room, html, plain) async def matrix_message(self, bot, room, event): args = event.body.split() args.pop(0) - if len(args) == 0: await bot.send_text(room, 'Usage: !loc ') if len(args) == 1: @@ -46,13 +44,11 @@ class MatrixModule: location = geolocator.geocode(query) if location: locationmsg = { - "body": "Tampere, Finland", - "geo_uri": "geo:61.5,23.766667", + "body": str(location.address), + "geo_uri": 'geo:' + str(location.latitude) + ',' + str(location.longitude), "msgtype": "m.location", } - locationmsg['body'] = location.address - locationmsg['geo_uri'] = 'geo:' + str(location.latitude) + ',' + str(location.longitude) - await bot.client.room_send(bot.get_room_id(room), 'm.room.message', locationmsg) + await bot.client.room_send(room.room_id, 'm.room.message', locationmsg) else: await bot.send_text(room, "Can't find " + query + " on map!")