Saturday, January 6, 2018

C# Azure Function sending SMS message

Overview

An example using an Azure Function written in C# (scripting) to send text message using Twilio. The full source code is on github.

One of the key issue was to reference the Twilio.Api.dll.

From the platform Feature Dialog of the Azure Function, in the Azure Portal
  • Open KUDU Powershell console 
  • Create a ref folder in the main source folder
  • Drag and drop the Twilio.Api.dll from your desktop to the top file system in the browser





To reference the DLL from the main source file use the following syntax.
#r "D:\home\site\wwwroot\RequestSmsSending\ref\Twilio.Api.dll"



Azure Function Run Method 


#load "SendSmsHelper.csx"
#load "AzureFunctionHelper.csx"

using System.Net;

public static async Task&tl;HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{    
    var ah = new AzureFunctionHelper(req, log);

    ah.Log(log, $"-- SendSMS --");

    string smsID    = null;
    var smsText     = await ah.GetParameter("smsText");
    var responseMsg = $"Invalid parameters";
    var to          = await ah.GetParameter("to", SendSmsHelper.FRED);
   
    if(smsText == null) { 

        ah.Log(log, responseMsg);
    }
    else {

        var finalSmsText = $"[{DateTime.Now}][{smsText}]";
        smsID            = SendSmsHelper.SendSMS(finalSmsText, to);
        responseMsg      = $"To:{to}, SmsID:{smsID}, SmsText:'{finalSmsText}'";
        ah.Log(log, responseMsg);
    }
    return await ah.GetResponse(smsID != null, responseMsg);
}


Wednesday, December 27, 2017

JavaScript ES6 filter(), take() and range() with iterator



      //

      let filter = function * (items, predicate) {

          for(let item of items)
              if(predicate(item))
                  yield item;
      }

      let take = function * (items, number) {

          let count = 0;
          if(number < 1) return;
          for(let item of items) {

              yield item;
              count += 1;
              if(count == number) return;
          }
      }
      
      let range = function * (start, end) { 
        
        let current = start;
        while(current <= end) {

          let delta = yield current;
          current += delta || 1;
        }
      }

      let count   = 0;
      let found   = null;
      let names   = ["Joy","Sue","Bob","Tom", "Sophie"];

      for(let e of take( filter( names, e => e.startsWith("S")), 1 ) ) {

          count += 1;
          found = e;
      }
 
      
      let result    = [];
      let iterator  = range(1, 10);
      let next      = iterator.next();

      while(!next.done) {

        result.push(next.value);
        next = iterator.next(2);
      }

JavaScript Class With Iterator (ES6)

//
//
      class Company {
          
          constructor() {
              this._employees = [];
          }
          addEmployees(...names) {
              this._employees = this._employees.concat(names);
          }
          *[Symbol.iterator]() {
              for(let e of this._employees)
                yield e;
          }
      }

      let count   = 0;
      let company = new Company();
      company.addEmployees("Fred","Joe","Tommy","Tom", "Sophie");

      for(let employee of company) {

          console.log(employee);
      }

How to run JavaScript unit tests interactively from any text editor

Overview 


This blog describes the procedure to install gulp-jasmine-livereload-task, to be able to execute on the fly JavaScript Jasmine unit tests from any text editor.

With this gulp task, you can update a Jasmine unit test file and once saved on the disk, it is executed in a browser, giving the result.

Steps


Installation 

C:\>md test
C:\>cd test
C:\test>md src
C:\test>npm install --save-dev jasmine-core gulp-jasmine-livereload-task
C:\test>npm install --save-dev gulp-debounced-watch
C:\test>npm install gulp

Gulpscript

In the test folder create a a file name gulpfile.js. Copy paste the following
var gulp    = require('gulp'),
    jasmine = require('gulp-jasmine-livereload-task');
 
gulp.task('default', jasmine({
    files: ['./src/**/*.js', './spec/**/*.js'],
    watch: {
        options: {
            debounceTimeout: 1000, //The number of milliseconds to debounce. 
            debounceImmediate: true //This option when set will issue a callback on the first event. 
        }
    }
}));


JavaScript unit test files

Create the file test.js in src folder and copy this content
describe("Suite", function() {
  
    it("Test", function() {

      expect(true).toBe(true);
    });
});


Execution

How to run the scripts

From the command line:
C:\test>gulp
This will load the default browser with the output of the unit test(s) execution

Thursday, September 28, 2017

ASP.NET Web Api, Twilio Web Hook and Error - 12300 Invalid Content Type

When using Twilio web hook implemented in ASP.NET Web Api, you may get this error

12300 Invalid Content Type

The following page TWIML MESSAGE: YOUR RESPONSE, lead me to the right way to implement the web hook.

[HttpGet]
public HttpResponseMessage SmsAnswerCallBack(string id)
{
    var res     = Request.CreateResponse(HttpStatusCode.OK);
    res.Content = new StringContent("<Response/>", Encoding.UTF8, "text/xml");
    return res;
}

Thursday, May 18, 2017

About pentesteracademy.com


  • $99 the first month, $39 per month after that. If you cancel and re subscribe you still have to pay $99.
  • Only 100 videos play per month. Each video is about 15 minutes. If you watch a video twice, it is counted twice (Yes).
  • The course "USB Forensics and Pentesting" which is the reason I signed for is good but from 2014. The hardware presented is old, does not work on Windows 64 bit. And there is no update with the new FTDI MCU now released (2017) and mentioned in the video.
  • You also pay for Dr. Philip Polstra sarcasms about Windows versus Linux. I paying for the course, I do not need to hear that.
  • Too much Linux not enough Windows. 
I downloaded up 70 videos about different courses, still have to watch them. So Jury is still out.

But generally speaking too expensive.

Wednesday, April 27, 2016

Friday, April 15, 2016

How to block the windows 10 upgrade

Difficult to believe, but the Microsoft Windows team seems it is ok to trick you to upgrade your Windows 7 machine to Windows 10 without you giving your consent.

 This article from PCWorld and utility from http://ultimateoutsider.com should let you configure your machine to avoid the upgrade.

How to block the windows 10 upgrade