NEWS
Garmin LiveTrack per Telegram
-
Leider schicken die Garmin Sportuhren LiveTracks nur per Email und Twitter tweets. Ich wollte aber gerne den LiveTrack als Telegram Nachricht erhalten.
Aus diesem Grund habe ich ein kleines Script geschrieben welches Emails nach einem LiveTrack durchsucht und die URL dann als Telegram Nachricht verschickt. Der LiveTrack wird auch noch in einem Datenpunkt gespeichert.Vielleicht hat ja jemand Interesse an dem Script.
const MailListener = require('mail-listener2'); const username = 'my@email.foo'; // Garmin LiveTrack Emailaddress const password = 'XXXXXXXXXXXX; // Password for IMAP const hostname = 'imap.1und1.de'; // IMAP Server const idUrl = '0_userdata.0.Garmin.url'; // Datapoint /** * Function to create new objects */ function createDp(id, common) { if (!getObject(id)) { let obj = {}; obj.type = 'state'; obj.common = common; setObject(id, obj, (err) => { if (err) log('Cannot write object: ' + err) else { var init = null; if (common.def === undefined) { if (common.type === 'number') init = 0; if (common.type === 'boolean') init = false; if (common.type === 'string') init = ''; } else init = common.def; setState(id, init, true); } }); } } /** * parsing body of mail for livetrack link */ function getCarminLiveTrack(body) { const regex = /(https:\/\/livetrack\.garmin\.com\/session\/[A-Za-z0-9-\/]+)/gm; let m = regex.exec(body); if (m) { return m[1]; } else { return undefined; } } /** * send link to telgram GarminLiveTrackBot */ function sendTelegramMessage(date, link) { try { if (date && link) { let text = 'I am on Tour!* You can follow me, if you click on ' + link + '. Track started at ' + date; console.log(text); sendTo('telegram.0', { text: text, parse_mode: 'Markdown' }); } } catch (e) { } } function checkMails(username, password, hostname, callback) { try { let mailListener = new MailListener({ username: username, password: password, host: hostname, port: 993, // imap port tls: true, connTimeout: 10000, // Default by node-imap authTimeout: 5000, // Default by node-imap, debug: null, // Or your custom function with only one incoming argument. Default: null tlsOptions: { rejectUnauthorized: false }, mailbox: "INBOX", // mailbox to monitor // searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved markSeen: true, // all fetched email willbe marked as seen and not fetched next time fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`, mailParserOptions: { streamAttachments: false }, // options to be passed to mailParser lib. attachments: false, // download attachments as they are encountered to the project directory attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments }); mailListener.start(); // start listening mailListener.on("server:connected", () => { console.log("Garmin imapConnected"); }); mailListener.on("server:disconnected", () => { console.log("Garmin imapDisconnected"); }); mailListener.on("error", (error) => { console.log('Garmin Livetrack error ' + error); }); mailListener.on("mail", (mail, seqno, attributes) => { let livetrack = getCarminLiveTrack(mail.html); if (livetrack) { console.log('Garmin Livetrack from ' + mail.date + ' - ' + livetrack); sendTelegramMessage(mail.date, livetrack); setState(idUrl, { val: livetrack, ack: false }); } }); } catch (error) { console.log('Garmin Livetrack error ' + error); } } /** * Create Datapoint */ createDp(idUrl, { 'name': 'Garmin URL', 'type': 'string', 'role': 'state', 'read': true, 'write': false }); /** * Start Checkmails */ checkMails(username, password, hostname);
Datapoint:
Telegram:
VG
Stübi