Skip to content

Implementation of SEND Location Telegram message #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/ESP32/Location/Location.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void handleNewMessages(int numNewMessages)
String message = "Long: " + String(bot.messages[i].longitude, 6) + "\n";
message += "Lat: " + String(bot.messages[i].latitude, 6) + "\n";
bot.sendMessage(chat_id, message, "Markdown");
bot.sendLocation(chat_id,bot.messages[i].latitude,bot.messages[i].longitude);
}
else if (text == "/start")
{
Expand Down
1 change: 1 addition & 0 deletions examples/ESP8266/Location/Location.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void handleNewMessages(int numNewMessages)
String message = "Long: " + String(bot.messages[i].longitude, 6) + "\n";
message += "Lat: " + String(bot.messages[i].latitude, 6) + "\n";
bot.sendMessage(chat_id, message, "Markdown");
bot.sendLocation(chat_id,bot.messages[i].latitude,bot.messages[i].longitude);
}
else if (text == "/start")
{
Expand Down
38 changes: 38 additions & 0 deletions src/UniversalTelegramBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ bool UniversalTelegramBot::processResult(JsonObject result, int messageIndex) {
messages[messageIndex].text = F("");
messages[messageIndex].from_id = F("");
messages[messageIndex].from_name = F("");
messages[messageIndex].username= F("");
messages[messageIndex].longitude = 0;
messages[messageIndex].latitude = 0;
messages[messageIndex].reply_to_message_id = 0;
Expand All @@ -455,6 +456,7 @@ bool UniversalTelegramBot::processResult(JsonObject result, int messageIndex) {
messages[messageIndex].type = F("message");
messages[messageIndex].from_id = message["from"]["id"].as<String>();
messages[messageIndex].from_name = message["from"]["first_name"].as<String>();
messages[messageIndex].username= message["from"]["username"].as<String>();
messages[messageIndex].date = message["date"].as<String>();
messages[messageIndex].chat_id = message["chat"]["id"].as<String>();
messages[messageIndex].chat_title = message["chat"]["title"].as<String>();
Expand Down Expand Up @@ -495,6 +497,7 @@ bool UniversalTelegramBot::processResult(JsonObject result, int messageIndex) {
messages[messageIndex].type = F("callback_query");
messages[messageIndex].from_id = message["from"]["id"].as<String>();
messages[messageIndex].from_name = message["from"]["first_name"].as<String>();
messages[messageIndex].username= message["from"]["username"].as<String>();
messages[messageIndex].text = message["data"].as<String>();
messages[messageIndex].date = message["date"].as<String>();
messages[messageIndex].chat_id = message["message"]["chat"]["id"].as<String>();
Expand All @@ -508,6 +511,7 @@ bool UniversalTelegramBot::processResult(JsonObject result, int messageIndex) {
messages[messageIndex].type = F("edited_message");
messages[messageIndex].from_id = message["from"]["id"].as<String>();
messages[messageIndex].from_name = message["from"]["first_name"].as<String>();
messages[messageIndex].username= message["from"]["username"].as<String>();
messages[messageIndex].date = message["date"].as<String>();
messages[messageIndex].chat_id = message["chat"]["id"].as<String>();
messages[messageIndex].chat_title = message["chat"]["title"].as<String>();
Expand Down Expand Up @@ -769,6 +773,40 @@ bool UniversalTelegramBot::sendChatAction(const String& chat_id, const String& t
return sent;
}

bool UniversalTelegramBot::sendLocation(const String& chat_id, const float latitude, const float longitude, uint16_t live_period) {

bool sent = false;
#ifdef TELEGRAM_DEBUG
Serial.println(F("SEND Location Message"));
#endif
unsigned long sttime = millis();

while (millis() - sttime < 8000ul) { // loop for a while to send the message
String command = BOT_CMD("sendLocation?chat_id=");
command += chat_id;
command += F("&latitude=");
command += String(latitude,6);
command += F("&longitude=");
command += String(longitude,6);
if ((live_period >= 60) && (live_period <= 86400)) {
command += F("&live_period=");
command += String(live_period);
}

String response = sendGetToTelegram(command);

#ifdef TELEGRAM_DEBUG
Serial.println(response);
#endif
sent = checkForOkResponse(response);

if (sent) break;
}

closeClient();
return sent;
}

void UniversalTelegramBot::closeClient() {
if (client->connected()) {
#ifdef TELEGRAM_DEBUG
Expand Down
4 changes: 3 additions & 1 deletion src/UniversalTelegramBot.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct telegramMessage {
String chat_title;
String from_id;
String from_name;
String username;
String date;
String type;
String file_caption;
Expand Down Expand Up @@ -94,7 +95,8 @@ class UniversalTelegramBot {
const String& parse_mode, const String& keyboard, int message_id = 0);

bool sendChatAction(const String& chat_id, const String& text);

bool sendLocation(const String& chat_id, const float latitude, const float longitude, const uint16_t live_period=0);

bool sendPostMessage(JsonObject payload, bool edit = false);
String sendPostPhoto(JsonObject payload);
String sendPhotoByBinary(const String& chat_id, const String& contentType, int fileSize,
Expand Down