How to get several inputs in one command in telegram bot api? - python-telegram-bot

For example, bot has command /set_coordinates. It accept latitude and longitude, like this:
Bot: Enter latitude
User: 16.4353
Bot: Enter longitude
User: 140.4325
The thing is that bot receives only one coordinate per message, and to understand either it need to request latitude or longitude, it should remember previous message. For example:
latitude_was_set = False
latitude, longitude = 0, 0
def set_coordinates_handler(update: Update, context: Context):
received_text = update.message.text
# First coordinate
if not self.latitude_was_set:
latitude = float(received_text)
self.latitude_was_set = True
else:
longitude = float(received_text)
It's simplified function without prints and other things to demonstrate that I need flag latitude_was_self. The problem is that I should store everything in a database,
because two user can have different latitude_was_set and variable inside python doesn't fit it. It seems to be inappropriate use of database to store a flag there. This problem becomes even bigger if I need to get more data in one command. Is there any other options instead of using database as a flag storage?
This question can be silly but it's my first bot :)

PTB provides two useful mechanisms for such usecases:
Storing data
ConversationHandler, see also the example
Update:
The correspondings links for v20.x of python-telegram-bot are:
Storing data
ConversationHandler, see also the example
Disclaimer: I'm currently the maintainer of python-telegram-bot

Related

Dialogflow CX $param not showing in agent text

I am getting into CX and have a simple agent going but I cannot get the agent to respond with my filled parameter.
I have a page that asks for #DrinkFrequency and #DrinkChoice, I see both get filled correctly when I test the agent and $page.params.status = FINAL takes me to my next page that says
So you drink $session.params.DrinkFrequency and prefer to drink $session.params.DrinkChoice
But the agent says exactly that, it does not replace $session.params.DrinkFrequency or $session.params.DrinkChoice with the values the user gave. I know it must be a simple issue but I have tried to write $session.params.DrinkFrequency in a few different ways and I looked at the existing tutorial bots and they also have it with just the $ sign. Am I not saving the user given value?
The page that gets the values has the two parameters and they ask for the value in their "Initial prompt fulfillment" field.
You're referring to the entity type, while the display name is the name of your parameter.
If you replace $session.params.DrinkFrequency with $session.params.frequency and $session.params.DrinkChoice with $session.params.preference it should work.

How to get geolocation from Eircode/Post Code accurately

I am trying to get latitude and longitude from Ireland Eircode by using google API but I am not getting data from some postcodes given below:
A96X7F2
E45XD68
When I am trying to get data from this, It's returning status ZERO_RESULTS
https://maps.googleapis.com/maps/api/geocode/json?address=A96X7F2&key=API_KEY
Note: In most cases, I am getting data by using this API.
Try specifying the region:
https://maps.googleapis.com/maps/api/geocode/json?address=A96X7F2&region=ie&key=API_KEY
Issues with postcodes are discussed on their issue tracker here:
https://issuetracker.google.com/issues/73030863?pli=1
They say in that link that for best results use region or component filtering. [Both are ways to specify region, but the former is a hint, the latter is a filter.]

Web Scrape returns N/A, not sure how to keep the data returning

totally new here & figured you guys will know the answer before I can even come near figuring this out.
I have a google form, feeding a live google sheet which users submit car reg numbers.
My goal is to have the reg number display the make, model besides the reg.
I have implemented an importXML function & the cell I expect to see the data loads up for a few minutes, then reverts to "N/A" or sometimes doesn't pull the data at all, but manually visiting the URL does return the data.
The import XML function uses a cell, made up of URL string, then adds the Reg/VIN input by form submission. That cell looks something like this "basicvehicledetails.com/reg" and returns the Class on the webpage relevant for Make/Model in separate cells.
I need the data to stay once it is returned, but don't know how to do that.
Another option is a car check website that requires a login, and then the reg to be input & searched before a webpage returns in-depth data on the car, is this something I can get to export to google sheet/excel spreadsheet?
I'm really stuck for this one, and would really appreciate any help as updating each car manually is painful.
try like:
=REGEXREPLACE(QUERY(ARRAY_CONSTRAIN(IMPORTDATA(
"https://www.motorcheck.ie/free-car-check/?vrm=152D1234"),5000,1),
"where Col1 contains 'dark-left'", 0), "</?\S+[^<>]*>", )
UPDATE:
=IMPORTHTML("https://www.cartell.ie/ssl/servlet/beginStarLookup?registration=152D1234",
"table", 1)

Cant get facebook date range working for events in ruby/koala

I am using ruby koala facebook graph API gem to retrieve events but cant restrict the events using a date range. I seem to be getting all events (even ones which have happened). What I am trying to do is get events is the next 200 days. I am using the following (ruby) code:
Koala.config.api_version = 'v2.10'
oauth = Koala::Facebook::OAuth.new app_id, app_secret
graph = Koala::Facebook::API.new oauth.get_app_access_token
from_date = Date.today
to_date = from_date + 200
fb_events = graph.get_object( fb_venue["url_listings"] + "?time_range={\"since\":#{from_date}, \"until\":#{to_date}" )
I am then getting the events 25 at once (the facebook default limit) using 'fb_events.next_page' to get them all.
I seem to get getting all events, including ones is the past.
Are you sure that the #get_object method wants a query string like that? Based on the docs[1], it seems that you should pass your object ID (which I assume is in fb_venue['url_listings'] for you as a first arg, and then metadata in a subsequent arg as a Hash. So I'd try something like:
fb_events = graph.get_object(fb_venue['url_listings'],
{since: from_date, until: to_date})
...according to the key/nesting structure FB expects.
Edit: forgot link to docs.
[1] -
http://www.rubydoc.info/github/arsduo/koala/Koala%2FFacebook%2FGraphAPIMethods%3Aget_object
According to the doc I don't think that you can request all the events, You can only select an event by using /{event-id} like this example or you can get user's events but you will need to ask him for the permission according to
permissions.
add user_events to the list of scope that you are permitting from user.
By this when user sign up using Facebook to your application it will take a permission for his events.
After that by using graph.get_object({facebook-user-id} + '/events') you will get 25 of his events as a default value and you can add {limit: 5} as a second parameter to the get_object function, You can use until and since but they should be in A Unix timestamp look at this to see the needed date format, hope this would help.

Lat/Lng Coordinate variables not recognized in ActiveRecord, forced to hard code

Hey guys I'm having the WEIRDEST issue while pairing on this rails app. We're using google maps api to grab lat and lng to do an ActiveRecord look up. For some odd reason the SomeModel.where("lat <=?", neLat) will not work BUT when I print neLat to the console and then copy the number and then hard code it to that query it works perfectly! I'm so stumped. Has anyone had any issues like this before?
This is an example of what I'm currently doing which doesn't work:
data = request.parameters
neLat = data['neLat'].to_f
#props = SomeModel.where("lat <=?", neLat)
47.6090933689332 is the number I see in my console/ActiveRecord query. One troubleshooting step I've taken was hard coding the number into a variable like this:
neLat = 47.6090933689332
#props = SomeModel.where("lat <=?", neLat)
I get the results I'm looking for this way, but I don't want to be hardcoding these coordinates. Another troubleshooting step I've taken was to make sure there wasn't a conflict with the float value so I went with this:
temp = "47.6090933689332"
neLat = temp.to_f
#props = SomeModel.where("lat <=?", neLat)
This didn't work either as it gave me the same results as the example just above.
EDIT:
I've provided more details as requested in the comments. Also, I realized I wasn't as clear as I could've been in my initial question. When I query I'm able to get my desired results back in the console, but the problem lies in the rendering of the results in my view. Any form of hardcoding will render correctly in my view, but when I use the values from my request.parameters is where the rendering fails. It will either render everything in the table or nothing in the table. So in the example below I can see that I'm getting 3 different properties from my properties table, but my view will render EVERYTHING in my table, which is like 7 properties.
Here is the raw sql query that ActiveRecord generates (this is the result of all 3 ways as I've done above):
SELECT `properties`.* FROM `properties` WHERE (lat >= 47.609093368933195)
#<Property:0x007fa333ac1880>
#<Property:0x007fa333ac1718>
#<Property:0x007fa333ac15b0>
As for the schema, it's just one table that I'm using and no joins.

Resources