Start modules in run(), loc tuning

This commit is contained in:
Ville Ranki 2019-12-11 00:04:24 +02:00
parent a773efb7af
commit 680af79fd8
2 changed files with 15 additions and 19 deletions

20
bot.py
View File

@ -156,16 +156,6 @@ class Bot:
self.join_on_invite = os.getenv('JOIN_ON_INVITE') self.join_on_invite = os.getenv('JOIN_ON_INVITE')
self.get_modules() 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): def stop(self):
print(f'Stopping {len(self.modules)} modules..') print(f'Stopping {len(self.modules)} modules..')
@ -183,6 +173,16 @@ class Bot:
print("Logged in with password, access token:", self.client.access_token) print("Logged in with password, access token:", self.client.access_token)
await self.client.sync() 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()) self.poll_task = asyncio.get_event_loop().create_task(self.poll_timer())
if self.client.logged_in: if self.client.logged_in:

View File

@ -10,7 +10,6 @@ class MatrixModule:
async def unknown_cb(self, room, event): async def unknown_cb(self, room, event):
if event.msgtype != 'm.location': if event.msgtype != 'm.location':
return return
location_text = event.content['body'] location_text = event.content['body']
# Fallback if body is empty # Fallback if body is empty
@ -29,15 +28,14 @@ class MatrixModule:
osm_link = 'https://www.openstreetmap.org/?mlat=' + latlon[0] + "&mlon=" + latlon[1] osm_link = 'https://www.openstreetmap.org/?mlat=' + latlon[0] + "&mlon=" + latlon[1]
plain = sender + ' - ' + osm_link plain = sender + ' 🚩 ' + osm_link
html = f'{sender} - <a href={osm_link}>{location_text}</a>' html = f'{sender} 🚩 <a href={osm_link}>{location_text}</a>'
await self.bot.send_html(room, html, plain) await self.bot.send_html(room, html, plain)
async def matrix_message(self, bot, room, event): async def matrix_message(self, bot, room, event):
args = event.body.split() args = event.body.split()
args.pop(0) args.pop(0)
if len(args) == 0: if len(args) == 0:
await bot.send_text(room, 'Usage: !loc <location name>') await bot.send_text(room, 'Usage: !loc <location name>')
if len(args) == 1: if len(args) == 1:
@ -46,13 +44,11 @@ class MatrixModule:
location = geolocator.geocode(query) location = geolocator.geocode(query)
if location: if location:
locationmsg = { locationmsg = {
"body": "Tampere, Finland", "body": str(location.address),
"geo_uri": "geo:61.5,23.766667", "geo_uri": 'geo:' + str(location.latitude) + ',' + str(location.longitude),
"msgtype": "m.location", "msgtype": "m.location",
} }
locationmsg['body'] = location.address await bot.client.room_send(room.room_id, 'm.room.message', locationmsg)
locationmsg['geo_uri'] = 'geo:' + str(location.latitude) + ',' + str(location.longitude)
await bot.client.room_send(bot.get_room_id(room), 'm.room.message', locationmsg)
else: else:
await bot.send_text(room, "Can't find " + query + " on map!") await bot.send_text(room, "Can't find " + query + " on map!")