aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-20 01:55:20 -0800
committerclarkzjw <[email protected]>2023-02-20 01:55:20 -0800
commit3a230798be1bc63b363cf75b8b1cae3a508cca84 (patch)
tree6df81aa7028c130726cdf0bcc3abb4f6f106fdf3
parent48cd0ed8abf0e84fc5d013a126e84c65ef0ed87a (diff)
downloadswarm2fediverse-3a230798be1bc63b363cf75b8b1cae3a508cca84.tar.gz
4sq: add query poi example
-rw-r--r--.gitignore2
-rw-r--r--bot.py18
-rw-r--r--config.py30
-rw-r--r--foursquare/query_poi.py18
4 files changed, 41 insertions, 27 deletions
diff --git a/.gitignore b/.gitignore
index 0671e2e..fdccc84 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
1foursquare/location_example.json
1config.ini 2config.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]
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):
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 )
24from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update 24from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
25from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes 25from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, MessageHandler, filters
26
27from config import BOT_TOKEN
28from foursquare.query_poi import query_poi
29
26 30
27# Enable logging 31# Enable logging
28logging.basicConfig( 32logging.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
53async 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
49async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 60async 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
65def main() -> None: 76def 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
diff --git a/config.py b/config.py
index 7f8e2f0..377a6c3 100644
--- a/config.py
+++ b/config.py
@@ -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 3import configparser
4# config = configparser.ConfigParser() 4config = configparser.ConfigParser()
5# config['DEFAULT'] = {'ServerAliveInterval': '45', 5config.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() 7BOT_TOKEN = config["DEFAULT"]["BOT_TOKEN"]
19# config.sections() 8FSQ_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 @@
1import requests
2import json
3from config import FSQ_API_KEY
4
5POI_API_ENDPOINT = "https://api.foursquare.com/v3/places/nearby?ll={}%2C{}"
6
7
8def 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]
Powered by cgit v1.2.3 (git 2.41.0)