-
Notifications
You must be signed in to change notification settings - Fork 12
/
osmbot_blueprint.py
123 lines (111 loc) · 4.8 KB
/
osmbot_blueprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from flask import Flask
from flask import request, current_app, Blueprint
import pynominatim
from osmapi import OsmApi
from configobj import ConfigObj
import gettext
import bot.user as u
from bot.osmbot import OsmBot
from telegram import Bot as TBot
from telegram import error
import sys
application = Flask(__name__)
application.debug = True
config = ConfigObj('bot.conf')
if config:
user = u.User(config.get('host', ''), config.get('database', ''), config.get('user', ''), config.get('password', ''))
osmbot = OsmBot(config)
telegram_api = TBot(config['token'])
api = OsmApi()
nom = pynominatim.Nominatim()
osmbot_blueprint = Blueprint(
'osmbot', __name__,
template_folder='templates',
static_folder='static'
)
@osmbot_blueprint.route('/osmbot/hook/<string:token>', methods=['POST'])
def attend_webhook(token):
user = u.User(config['host'], config['database'], config['user'], config['password'])
current_app.logger.debug('token:%s', token)
osmbot.set_group(False)
if token == config['token']:
try:
query = request.json
if 'edited_channel_post' in query:
return 'OK'
if 'channel_post' in query:
return 'OK'
if 'edited_message' in query:
return 'OK'
message_type = ''
if 'message' in query:
message_dict = query['message']
message_type = 'query'
if 'from' in message_dict and 'id' in message_dict['from']:
osmbot.set_group(query['message']['chat']['type'] == u'group')
if osmbot.get_group():
identifier = message_dict['chat']['id']
else:
identifier = message_dict['from']['id']
user_config = user.get_user(identifier, group=osmbot.get_group())
user_id = message_dict['from']['id']
elif 'inline_query' in query:
message_type = 'inline'
user_id = query['inline_query']['from']['id']
identifier = query['inline_query']['from']['id']
user_config = user.get_user(identifier, group=False)
message = query['inline_query']['query']
chat_id = 0
else:
user_config = user.get_defaultconfig()
user_id = 0
if 'message' in query:
if 'text' in query['message']:
message = query['message']['text']
else:
message = ''
chat_id = query['message']['chat']['id']
if osmbot.get_group() and (not user_config['onlymentions'] and user_config['onlymentions'] is not None)and '@osmbot' not in message.lower():
if message not in ['Yes', 'No', 'Language', 'Answer only when mention?'] and message not in osmbot.get_languages().keys():
return 'OK'
else:
message = message.replace('@osmbot', '')
message = message.replace('@OSMbot', '')
if not 'callback_query' in query:
message = osmbot.clean_message(message)
osmbot.load_language(user_config['lang'])
osmbot.answer_message(message, query, chat_id, user_id, user_config, user, message_type)
else:
osmbot.answer_callback(query)
return 'OK'
except error.Unauthorized:
return 'OK'
except Exception as e:
if sys.version_info.major ==2:
if e.message == 'Unauthorized':
return 'OK'
else:
if str(e) == 'Unauthorized':
return 'OK'
print(str(e))
import traceback
traceback.print_exc()
current_app.sentry.captureException()
try:
osmbot.load_language(user_config['lang'])
text = osmbot._get_template('error_message.md').render()
telegram_api.sendMessage(chat_id, text)
except Exception:
pass
return 'OK'
else:
return 'NOT ALLOWED'
if __name__ == '__main__':
application.run(host='0.0.0.0')
gettext.gettext('OpenStreetMap bot finds any location in world from the Nominatim OSM database and can send links and maps from OSM')
gettext.gettext('OpenStreetMap bot finds any location in the world from the Nominatim OSM database')
gettext.gettext('The bot can send links and maps (jpg, png or pdf) from OSM')
gettext.gettext('Data for all the world (cities and towns, shops -with phone number, email...-, Wikipedia links, etc)')
gettext.gettext('OSMbot is multilingual and speaks *your language here*')