Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Garmin LiveTrack per Telegram

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Garmin LiveTrack per Telegram

    This topic has been deleted. Only users with topic management privileges can see it.
    • Stuebi
      Stuebi last edited by

      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:
      Bildschirmfoto 2020-03-05 um 21.00.56.png

      Telegram:
      Bildschirmfoto 2020-03-05 um 21.05.01.png

      VG
      Stübi

      1 Reply Last reply Reply Quote 0
      • First post
        Last post

      Support us

      ioBroker
      Community Adapters
      Donate

      774
      Online

      31.8k
      Users

      80.0k
      Topics

      1.3m
      Posts

      1
      1
      606
      Loading More Posts
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      Community
      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
      The ioBroker Community 2014-2023
      logo