diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | bot.py | 18 | ||||
-rw-r--r-- | config.py | 30 | ||||
-rw-r--r-- | foursquare/query_poi.py | 18 |
4 files changed, 41 insertions, 27 deletions
@@ -1,4 +1,6 @@ | |||
1 | foursquare/location_example.json | ||
1 | config.ini | 2 | config.ini |
3 | .idea/ | ||
2 | # Byte-compiled / optimized / DLL files | 4 | # Byte-compiled / optimized / DLL files |
3 | __pycache__/ | 5 | __pycache__/ |
4 | *.py[cod] | 6 | *.py[cod] |
@@ -22,7 +22,11 @@ if __version_info__ < (20, 0, 0, "alpha", 1): | |||
22 | f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" | 22 | f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" |
23 | ) | 23 | ) |
24 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update | 24 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update |
25 | from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes | 25 | from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters |
26 | |||
27 | from config import BOT_TOKEN | ||
28 | from foursquare.query_poi import query_poi | ||
29 | |||
26 | 30 | ||
27 | # Enable logging | 31 | # Enable logging |
28 | logging.basicConfig( | 32 | logging.basicConfig( |
@@ -46,6 +50,13 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |||
46 | await update.message.reply_text("Please choose:", reply_markup=reply_markup) | 50 | await update.message.reply_text("Please choose:", reply_markup=reply_markup) |
47 | 51 | ||
48 | 52 | ||
53 | async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
54 | print(update.message.location.latitude) | ||
55 | print(update.message.location.longitude) | ||
56 | poi = query_poi(update.message.location.latitude, update.message.location.longitude) | ||
57 | await update.message.reply_text("Your location received: {}".format(poi)) | ||
58 | |||
59 | |||
49 | async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | 60 | async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
50 | """Parses the CallbackQuery and updates the message text.""" | 61 | """Parses the CallbackQuery and updates the message text.""" |
51 | query = update.callback_query | 62 | query = update.callback_query |
@@ -65,10 +76,13 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No | |||
65 | def main() -> None: | 76 | def main() -> None: |
66 | """Run the bot.""" | 77 | """Run the bot.""" |
67 | # Create the Application and pass it your bot's token. | 78 | # Create the Application and pass it your bot's token. |
68 | application = Application.builder().token("TOKEN").build() | 79 | application = Application.builder().token(BOT_TOKEN).build() |
69 | 80 | ||
70 | application.add_handler(CommandHandler("start", start)) | 81 | application.add_handler(CommandHandler("start", start)) |
82 | application.add_handler(CommandHandler("checkin", checkin)) | ||
71 | application.add_handler(CallbackQueryHandler(button)) | 83 | application.add_handler(CallbackQueryHandler(button)) |
84 | # on non command i.e message - echo the message on Telegram | ||
85 | application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin)) | ||
72 | application.add_handler(CommandHandler("help", help_command)) | 86 | application.add_handler(CommandHandler("help", help_command)) |
73 | 87 | ||
74 | # Run the bot until the user presses Ctrl-C | 88 | # Run the bot until the user presses Ctrl-C |
@@ -1,28 +1,8 @@ | |||
1 | # https://docs.python.org/3/library/configparser.html | 1 | # https://docs.python.org/3/library/configparser.html |
2 | 2 | ||
3 | # import configparser | 3 | import configparser |
4 | # config = configparser.ConfigParser() | 4 | config = configparser.ConfigParser() |
5 | # config['DEFAULT'] = {'ServerAliveInterval': '45', | 5 | config.read("config.ini") |
6 | # 'Compression': 'yes', | ||
7 | # 'CompressionLevel': '9'} | ||
8 | # config['forge.example'] = {} | ||
9 | # config['forge.example']['User'] = 'hg' | ||
10 | # config['topsecret.server.example'] = {} | ||
11 | # topsecret = config['topsecret.server.example'] | ||
12 | # topsecret['Port'] = '50022' # mutates the parser | ||
13 | # topsecret['ForwardX11'] = 'no' # same here | ||
14 | # config['DEFAULT']['ForwardX11'] = 'yes' | ||
15 | # with open('example.ini', 'w') as configfile: | ||
16 | # config.write(configfile) | ||
17 | 6 | ||
18 | # config = configparser.ConfigParser() | 7 | BOT_TOKEN = config["DEFAULT"]["BOT_TOKEN"] |
19 | # config.sections() | 8 | FSQ_API_KEY = config["DEFAULT"]["FOURSQUARE_API_KEY"] |
20 | # config.read('example.ini') | ||
21 | # config.sections() | ||
22 | # topsecret = config['topsecret.server.example'] | ||
23 | # topsecret['ForwardX11'] | ||
24 | # topsecret['Port'] | ||
25 | # for key in config['forge.example']: | ||
26 | # print(key) | ||
27 | |||
28 | # config['forge.example']['ForwardX11'] | ||
diff --git a/foursquare/query_poi.py b/foursquare/query_poi.py new file mode 100644 index 0000000..64ded55 --- /dev/null +++ b/foursquare/query_poi.py | |||
@@ -0,0 +1,18 @@ | |||
1 | import requests | ||
2 | import json | ||
3 | from config import FSQ_API_KEY | ||
4 | |||
5 | POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}" | ||
6 | |||
7 | |||
8 | def query_poi(latitude, longitude): | ||
9 | url = POI_API_ENDPOINT.format(latitude, longitude) | ||
10 | |||
11 | headers = { | ||
12 | "accept": "application/json", | ||
13 | "Authorization": FSQ_API_KEY | ||
14 | } | ||
15 | |||
16 | response = requests.get(url, headers=headers) | ||
17 | print(response.text) | ||
18 | return json.loads(response.text)["results"][:2] | ||