DEV Community

Cover image for Request location and telegram bot
Antonov Mike
Antonov Mike

Posted on • Updated on

Request location and telegram bot

Back in the spring/summer of sad 2022 I made a bot for an advertising agency in Tbilisi. (I did the work for free) This bot sends the user 3 nearest cafes. This bot needs to be simplified by making one button labeled "Send your location please". The Telegram API allows that. It took me a long time to solve that problem. Why? Let's figure it out.



[dependencies]
carapax = "0.12.0"


Enter fullscreen mode Exit fullscreen mode

How do we create a button for some telegram bot with carapax? A simple button with text like this:



api.execute(SendMessage::new(
    chat_id.clone(),
    "Please send your location to the chat room ☺️",
)).await?;


Enter fullscreen mode Exit fullscreen mode

Button with the function, for example, to go to the google map would be look something like this:



api.execute(SendMessage::new(
        chat_id.clone(), &museum.description).reply_markup(vec![vec![
        InlineKeyboardButton::with_url("Open google map",  &museum.google),
    ]]),
).await?;


Enter fullscreen mode Exit fullscreen mode

Notice anything in common? How would you create a button to query a user's geo location? Same way. In Python, by the way, it's about the same logic for creating a button requesting geo location.
But carapax will resent that you didn't give it "self"... I.e. the button should refer to itself. And this is what it should look like in the end:



let send_location = KeyboardButton::new("Send location");

api.execute(SendMessage::new(
        chat_id.clone(), "Hi! To find the nearest museum, please send your geo-location to the chat.").reply_markup(vec![vec![
        KeyboardButton::request_location(send_location),
    ]]),
).await?;


Enter fullscreen mode Exit fullscreen mode

Not



KeyboardButton::request_location("Send location"),


Enter fullscreen mode Exit fullscreen mode

Not



KeyboardButton::request_location("Send location", true),


Enter fullscreen mode Exit fullscreen mode

And that's pretty weird.

Now that you've seen how it's done, it doesn't look too difficult. But when you don't know how to do it, especially when you've been studying programming for less than half a year like me, you feel like you're at a dead end. And the documentation doesn't come to your aid.

Why don't I swap the carapax library for some other library? The others are no better.

Top comments (0)