-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
54 lines (40 loc) · 1.57 KB
/
bot.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
#!/usr/bin/env python
# pylint: disable=C0116,W0613
import what3words
import logging
from telegram.ext import Updater, CommandHandler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
geocoder = what3words.Geocoder("your w3w token")
tgtoken = "your telegram bot token"
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def w3w(update, context):
try:
number1 = str(context.args[0])
number2 = str(context.args[1])
result = geocoder.convert_to_3wa(what3words.Coordinates(number1, number2))
update.message.reply_text('The coordinates are: '+str(result))
except (IndexError, ValueError):
update.message.reply_text('There are not enough numbers')
def coords(update, context):
try:
word1 = str(context.args[0])
word2 = str(context.args[1])
word3 = str(context.args[2])
result1 = geocoder.convert_to_coordinates(f"{word1}.{word2}.{word3}")
update.message.reply_text('The 3 words are: '+str(result1))
except (IndexError, ValueError):
update.message.reply_text('There are not enough words')
def main():
updater = Updater(tgtoken, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("w3w", w3w))
dp.add_handler(CommandHandler("coords", coords))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()