Telegram: how to send location

Hi all,

I'n using the node-red-contrib-telegrambot and would like to know how to send a location? When I use the type: "message" all is fine. For location type: "location" should be used but I'm not quite sure how to configure this.

Anyone done this before?

Kind regards,
Peter Hunt.

On the node's page there are a lot of examples, including how to send locations:

Well, not that much examples on the location. That is why I was asking id anyone has actually done this before......

There is info there, have you give it a go?

Sending live locations
Locations can be send to the chat as described above and then updated afterwards: live location update. To achieve this you have to provide the live_period in seconds in the options when sending the location.

msg.payload.type = 'location';
msg.payload.content = {
    latitude : lat,
    longitude : lng
};

What have you tried & where are you stuck?

You said "

So you know how to send a message.

What happens when you set type to location?

By reading the docs, it should be a simple case of putting the lon/lat in the payload (as I copied for you above)

1 Like

I'm sending the messages via a template, as I found as example a while ago and is working fine. I set msg.payload = "My message" and return msg in a function. This function is wired to a template like :

{
"chatId": "-mychatId",
"type": "message",
"content": "{{{ payload }}}",
"options": {
"parse_mode": "MarkdownV2"
}
}

This is working fine for messages. When I set the type to "location", I really have no clue how to send the long/lat. I really tried so many options but don't know what format the BOT is expecting me to send. Maybe the template is bugging me in some way, but if I remove/change the template, I have to change lots of functions in several flows. That is why I was looking for a working example in stead of "how to" :slight_smile:

In the code above you’re setting msg.payload.content as a string containing the incoming msg.payload. Have you tried wiring a different/template node up to it with an object for msg.payload.content instead with the coordinates like shown above?
If you haven’t done so before, reading Working with Messages from the docs is worth it for the understanding of what you are doing.

1 Like

Ok, when I put this straight into the template, it's working fine :

"content": { "longitude": "4", "latitude": "52" }

Now I need to get this into the payload function, before the template :

msg.payload = ""longitude": "4", "latitude": "52"";

But the debugger keeps showing;

object
chatId: "-419956486"
type: "location"
content: ""longitude": "4", "latitude": "52""

So somehow the quotes don't get through properly...

Peter, please read the link I put above, based on your answer you're trying to put in a string, rather than an object. It's important to understand the difference. In short, your template as it is now, won't work with what you're trying to do. You will have to find a solution for that. I am aware of what example you're referring to as for why you use this template, I read that topic as well (and if I remember correctly offered you suggestions there as well on how to do it). For this to work out, you need to know the difference between objects and strings, and understand why the function node code shown above by @Steve-Mcl does work, and your template does not.

Hi. I would suggest some online learning around JavaScript objects. I'm not talking weeks or even days, just an hour or 2.

For now, here is some clues....

var s = "";// a new empty string
var a = [];// a new empty array
var o1 = {}; // a new empty object
var o1.latitude = 54.123;// I just added a number property to the empty object 
// Below I create and object with properties in already
var payload = { 
  name: "fred",
  content : {}
};

//The above object payload has a .name string and .content object. You can change it like this...
payload.content.longitude = -1.454321

The reason you're struggling is you need to learn a bit. The above clues may or may not be enough to push you in the right direction. But I definitely recommend a bit online learning around JavaScript objects.

Edit... And definitely read the link @afelix posted. There are some very useful tips and info that will make most of this make sense.

3 Likes

Hi all,

I'm trying together with Peter to get this thing going. As mentioned in the tickets we indeed have a learning curve....

From the suggested approaches I've tried two paths. One in a regular function, and the other a 'change' node.
However, we still struggle with the result.

But let me first write down some facts.

Our data is coming from a SQL query. This query results in an array looking like this:
[{"zoektekst":"A1 XXXX YYY : 15146","city_id":15023,"lat":52.0533,"lon":4.48868}]
also:
[
{
"zoektekst": "A1 XXXX YYY : 15146",
"city_id": 15023,
"lat": 52.0533,
"lon": 4.48868
}
]

My first approach was a function:

if (msg.payload[0].zoektekst !== undefined)
{
	var bla_lon = msg.payload[0].lon;
	var bla_lat = msg.payload[0].lat;
	
	msg.payload = {
	    name : "latitude",
	    content : {}
	};
     msg.payload = {
	    name : "longitude",
	    content : {}
	};
	
	msg.payload.content.longitude = bla_lon;
    msg.payload.content.latitude = bla_lat;
    
	return msg;
}

However, we could not get that filled properly.

Tried with a 'change node' to get the variables filled with the following 'SET' rules:

msg.payload.content.longitude
to
msg.payload[0].lat

msg.payload.content.latitude
to
msg.payload[0].lon

msg.payload.zoektekst
to
msg.payload[0].zoektekst

The last item 'msg.payload.zoektekst' does get properly set, and this value we can use within the Telegram template using:
{
"chatId": "-000000",
"type": "message",
"content": "{{{ payload.zoektekst }}}",
"options": {
"parse_mode": "MarkdownV2"
}
}

however, using the same approach, the lon/lat do not get filled if we use another Telegram template with:

{
    "chatId": "-419956486",
    "type": "location",
    "content": "{{{ payload.content }}}"
}

resulting in:
{"chatId":"-419956486","type":"location","content":"[object Object]"}

So if I / we interpreted the suggestions correctly to get the required values in their proper positions with correct properties we should be in the correct direction, but we are missing something...

Please have a quick look, and if we are missing something obvious please let us know... (from our code you guys most probably can guess we are no programmers...)

Many thanks in advance,

Ron

This is close...

But you overwrite payload by setting it a second time.

And if you're in a function node, you don't need to use a template node. Just do everything in the function.

The document says use...

msg.payload.type = 'location';
msg.payload.content = {
    latitude : lat,
    longitude : lng
};

So you could do something like...

msg.payload = {  content : {} }; 
msg.payload.type = 'location';
msg.payload.content.longitude = bla_lon; 
msg.payload.content.latitude = bla_lat;
1 Like

Thanks all, we managed to work it out. We got rid of the template and are now defining everything in the function which makes more sense anyway. Still a lot to figure out but for now we are able to send location, edit/delete messages and I'm only struggling with buttons, but that's not on topic in this thread :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.