Fetching total and remaining data on a Telia mobile subscription.
The following homeyscript will login and parse the total GB and remaining GB of your Telia mobile subscription. Name it telia.js, or similar:
constbetween=function(str, strf, strt) {returnstr.split(strf).pop().split(strt)[0].trim();}if (args.length===0) {thrownewError("Cannot run without arguments. Format is: mobilenumber;password")}let a = args[0].split(';')let mobile = a[0].trim()let password = a[1].trim()let postbody = {"Username": mobile,"Password": password}let cookie = "cookieConsentLevel=3; ROUTEID_CSS=.NCNO-1; ROUTEID_RECSS=.NCNO-1; _gcl_au=1.1.1071439771.1588404619; _gid=GA1.2.693156927.1588404620; _fbp=fb.1.1588404619754.1570612504; _dc_gtm_UA-3214365-1=1; _hjid=8ca343c8-c972-4794-8ff6-7156d0adb5d5; nmstat=1588404710200; _scid=bb633357-5e50-42b9-b694-ba82d0a707ac; _ga=GA1.1.1888154566.1588404619; _ga_75H3PZJWHY=GS1.1.1588404619.1.0.1588404621.58"
constresponse=awaitfetch("https://min-side.telia.no/re/api/mssa-proxy/no/rs/auth/basic?goToMinbedrift=false", {"headers": {"accept":"application/json","accept-language":"nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5","authorization":"Basic bXNzYS1wcm94eS11c2VyOnJmVGV0VzVQblA=","cache-control":"no-cache","client-id":"css","content-type":"application/json","sec-fetch-dest":"empty","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","cookie": cookie },"referrer":"https://min-side.telia.no/re/login?CT_ORIG_URL=https%3A%2F%2Fwww.telia.no%2Fmin-side","referrerPolicy":"no-referrer-when-downgrade","body":JSON.stringify(postbody),"method":"POST","mode":"cors"});//console.log("Response", response)let raw =response.headers.raw()['set-cookie']let rawd = raw[0].split(';')let session =nullrawd.forEach(el => {let dc =el.split('=')if (dc[0] ==='CTSESSION') { session = dc[1] }})cookie +='; CTSESSION='+ sessionconstresponse2=awaitfetch("https://www.telia.no/forbruk/?ts=1588404892199", {"headers": {"accept":"*/*","accept-language":"nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5","sec-fetch-dest":"empty","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","x-requested-with":"XMLHttpRequest","cookie": cookie },"referrer":"https://www.telia.no/forbruk/","referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"});constbody=awaitresponse2.text()let total =between(body,'<p class="donut-chart__value">','</p><p class="donut-chart__value-caption">igjen av totalt</p>')let remaining =between(body,'<p class="donut-chart__value-caption">','</p></div></div></div></div></div><div class="chart-legend__container">')// console.log(body)let tot =parseFloat(total.split(' ')[0] )let rem =parseFloat(remaining.split(' ')[0])console.log("total "+ tot)console.log("remaining "+ rem)awaitsetTagValue("telia_"+ mobile +"_tot", {type:'number', title:'Telia ['+ mobile +'] total'}, tot)awaitsetTagValue("telia_"+ mobile +"_rem", {type:'number', title:'Telia ['+ mobile +'] remaining'}, rem)returntrue
The script ignores unit, only reads the value. If your plan is 200MB, the unit that will be presented in Telia minside is in MB and not GB.
The script uses the minside, which is not a good way to fetch the data. The reason is that I did not find any proper API by Telia.
In order to update data, create a flow like this:
The homeyscript above takes one paramter in this format: 12345678;password
The number is your mobile number, and the password is your password in Telia minside. If you enter wrong password a few subsequent attempts, Telia will block your password, and you will need to reset the password.
The homeyscript will create a tag for the total available data for this month, and another tag for the remaining data for this month.
The flow above add a Logic Set numeric variable to store the results.