如何向电报机器人发送最近餐馆的位置信息?
How do I send a telegram bot a location about the nearest restaurants?
我正在尝试创建一个机器人来向用户显示附近的餐馆。我写了一个函数,但它给出了一个错误。它确定用户的位置,确定附近的地点,但不会将交互式地图上的标签输出到聊天中。
这是错误的代码和控制台:
def restaraunt(bot,update):
google_places = GooglePlaces(GMAPSAPI)
a = bot.message.location
b = a['latitude']
c = a['longitude']
d = {'lat': b, 'lng': c}
query_result = google_places.nearby_search(
lat_lng=d,
radius=500,
types=[types.TYPE_RESTAURANT])
for place in query_result.places:
print(place.geo_location['lat'])
lat = place.geo_location['lat']
lng = place.geo_location['lng']
bot.send_location(bot.message.from_user.id, latitude=lat, longitude=lng)
Traceback (most recent call last):
File "C:\Users\FuNkY\PycharmProjects\BotTG\venv\lib\site-packages\telegram\ext\dispatcher.py", line 555, in process_update
handler.handle_update(update, self, check, context)
File "C:\Users\FuNkY\PycharmProjects\BotTG\venv\lib\site-packages\telegram\ext\handler.py", line 198, in handle_update
return self.callback(update, context)
File "C:/Users/FuNkY/PycharmProjects/BotTG/123.py", line 70, in restaraunt
bot.send_location(bot.message.from_user.id, latitude=lat, longitude=lng)
AttributeError: 'Update' object has no attribute 'send_location'
发生这种情况是因为您使用的是旧式处理程序回调。要解决此问题,只需更改您的函数签名并更新引用:
def restaraunt(update, context):
google_places = GooglePlaces(GMAPSAPI)
a = update.message.location
b = a.latitude
c = a.longitude
d = {'lat': b, 'lng': c}
query_result = google_places.nearby_search(lat_lng=d, radius=500, types=[types.TYPE_RESTAURANT])
for place in query_result.places:
lat = float(str(place.geo_location['lat']))
lng = float(str(place.geo_location['lng']))
context.bot.send_location(update.effective_user.id, latitude=lat, longitude=lng)
如果您最近更新了 python-telegram-bot
,也可以查看 this transition guide
编辑:将 Decimal
对象转换为浮动对象,因为这也是一个错误。
我正在尝试创建一个机器人来向用户显示附近的餐馆。我写了一个函数,但它给出了一个错误。它确定用户的位置,确定附近的地点,但不会将交互式地图上的标签输出到聊天中。 这是错误的代码和控制台:
def restaraunt(bot,update):
google_places = GooglePlaces(GMAPSAPI)
a = bot.message.location
b = a['latitude']
c = a['longitude']
d = {'lat': b, 'lng': c}
query_result = google_places.nearby_search(
lat_lng=d,
radius=500,
types=[types.TYPE_RESTAURANT])
for place in query_result.places:
print(place.geo_location['lat'])
lat = place.geo_location['lat']
lng = place.geo_location['lng']
bot.send_location(bot.message.from_user.id, latitude=lat, longitude=lng)
Traceback (most recent call last):
File "C:\Users\FuNkY\PycharmProjects\BotTG\venv\lib\site-packages\telegram\ext\dispatcher.py", line 555, in process_update
handler.handle_update(update, self, check, context)
File "C:\Users\FuNkY\PycharmProjects\BotTG\venv\lib\site-packages\telegram\ext\handler.py", line 198, in handle_update
return self.callback(update, context)
File "C:/Users/FuNkY/PycharmProjects/BotTG/123.py", line 70, in restaraunt
bot.send_location(bot.message.from_user.id, latitude=lat, longitude=lng)
AttributeError: 'Update' object has no attribute 'send_location'
发生这种情况是因为您使用的是旧式处理程序回调。要解决此问题,只需更改您的函数签名并更新引用:
def restaraunt(update, context):
google_places = GooglePlaces(GMAPSAPI)
a = update.message.location
b = a.latitude
c = a.longitude
d = {'lat': b, 'lng': c}
query_result = google_places.nearby_search(lat_lng=d, radius=500, types=[types.TYPE_RESTAURANT])
for place in query_result.places:
lat = float(str(place.geo_location['lat']))
lng = float(str(place.geo_location['lng']))
context.bot.send_location(update.effective_user.id, latitude=lat, longitude=lng)
如果您最近更新了 python-telegram-bot
,也可以查看 this transition guide编辑:将 Decimal
对象转换为浮动对象,因为这也是一个错误。