From 3a230798be1bc63b363cf75b8b1cae3a508cca84 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Mon, 20 Feb 2023 01:55:20 -0800 Subject: 4sq: add query poi example --- .gitignore | 2 ++ bot.py | 18 ++++++++++++++++-- config.py | 30 +++++------------------------- foursquare/query_poi.py | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 27 deletions(-) create mode 100644 foursquare/query_poi.py diff --git a/.gitignore b/.gitignore index 0671e2e..fdccc84 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +foursquare/location_example.json config.ini +.idea/ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/bot.py b/bot.py index 517f810..e010516 100644 --- a/bot.py +++ b/bot.py @@ -22,7 +22,11 @@ if __version_info__ < (20, 0, 0, "alpha", 1): f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" ) from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update -from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes +from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters + +from config import BOT_TOKEN +from foursquare.query_poi import query_poi + # Enable logging logging.basicConfig( @@ -46,6 +50,13 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Please choose:", reply_markup=reply_markup) +async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + print(update.message.location.latitude) + print(update.message.location.longitude) + poi = query_poi(update.message.location.latitude, update.message.location.longitude) + await update.message.reply_text("Your location received: {}".format(poi)) + + async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Parses the CallbackQuery and updates the message text.""" query = update.callback_query @@ -65,10 +76,13 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No def main() -> None: """Run the bot.""" # Create the Application and pass it your bot's token. - application = Application.builder().token("TOKEN").build() + application = Application.builder().token(BOT_TOKEN).build() application.add_handler(CommandHandler("start", start)) + application.add_handler(CommandHandler("checkin", checkin)) application.add_handler(CallbackQueryHandler(button)) + # on non command i.e message - echo the message on Telegram + application.add_handler(MessageHandler(filters.LOCATION & ~filters.COMMAND, checkin)) application.add_handler(CommandHandler("help", help_command)) # Run the bot until the user presses Ctrl-C diff --git a/config.py b/config.py index 7f8e2f0..377a6c3 100644 --- a/config.py +++ b/config.py @@ -1,28 +1,8 @@ # https://docs.python.org/3/library/configparser.html -# import configparser -# config = configparser.ConfigParser() -# config['DEFAULT'] = {'ServerAliveInterval': '45', -# 'Compression': 'yes', -# 'CompressionLevel': '9'} -# config['forge.example'] = {} -# config['forge.example']['User'] = 'hg' -# config['topsecret.server.example'] = {} -# topsecret = config['topsecret.server.example'] -# topsecret['Port'] = '50022' # mutates the parser -# topsecret['ForwardX11'] = 'no' # same here -# config['DEFAULT']['ForwardX11'] = 'yes' -# with open('example.ini', 'w') as configfile: -# config.write(configfile) +import configparser +config = configparser.ConfigParser() +config.read("config.ini") -# config = configparser.ConfigParser() -# config.sections() -# config.read('example.ini') -# config.sections() -# topsecret = config['topsecret.server.example'] -# topsecret['ForwardX11'] -# topsecret['Port'] -# for key in config['forge.example']: -# print(key) - -# config['forge.example']['ForwardX11'] +BOT_TOKEN = config["DEFAULT"]["BOT_TOKEN"] +FSQ_API_KEY = config["DEFAULT"]["FOURSQUARE_API_KEY"] 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 @@ +import requests +import json +from config import FSQ_API_KEY + +POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}" + + +def query_poi(latitude, longitude): + url = POI_API_ENDPOINT.format(latitude, longitude) + + headers = { + "accept": "application/json", + "Authorization": FSQ_API_KEY + } + + response = requests.get(url, headers=headers) + print(response.text) + return json.loads(response.text)["results"][:2] -- cgit v1.2.3