3

I am currently successfully able to send an SMS to my Twilio number which I got with my account and reply to it with a standard response.

What I want is to parse the received text in my Twilio number and paste it int a .txt file.

Here the code for Receiving an SMS and replying to it.

using Twilio.TwiML;
using Twilio.AspNet.Common;
using Twilio.AspNet.Mvc;


namespace Twilio.Controllers
{
    public class SmsController : TwilioController
    {
        public TwiMLResult Index(SmsRequest incomingMessage)
        {
            var messagingResponse = new MessagingResponse();
            messagingResponse.Message("The copy cat says: " +
                                        incomingMessage.Body);
            Console.WriteLine(incomingMessage.Body);
            return TwiML(messagingResponse);
        }
    }
}

Also, How can i use it inside a windows service? I am new to this and having searched a lot for a quite sometime, seeking some help or direction

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • May I know are you Still looking for answer ? – Shaiju T May 14 '19 at 13:03
  • 2
    @stom, I have been looking for a way for a very long time. I tried once again. If you google "how to parse a Twilio SMS into a text file c#", all you receive are a bunch of standard guides from Twilo themselves and every other way to send/retrieve/modify them. – Ayush Singhania May 14 '19 at 14:06
  • ok good, Actually you have to google only the necessary parts, like `save data in text file c#` , Because you already have the data to be saved in text file. By the way you have received an answer check if that works. – Shaiju T May 14 '19 at 14:15
  • 1
    @stom, Can you please suggest a way to hide the browser chrome which is opened when I Run (IIS Express) the program? I just want it hidden in the background? – Ayush Singhania May 14 '19 at 14:31
  • As Suggested create asp.net web api project instead and put the same code inside api action method. Also accept the answer if it was useful. – Shaiju T May 15 '19 at 14:10

1 Answers1

1

Something like this:

using System;
using System.IO;

namespace Twilio.Controllers
{
    public class SmsController : TwilioController
    {
        public TwiMLResult Index(SmsRequest incomingMessage)
        {
            var messagingResponse = new MessagingResponse();
            messagingResponse.Message("The copy cat says: " +
                                        incomingMessage.Body);
            Console.WriteLine(incomingMessage.Body);


            using (StreamWriter w = File.AppendText("log.txt"))
            {
                Log(incomingMessage.Body, w);
            }

            return TwiML(messagingResponse); 

        }

        public static void Log(string logMessage, TextWriter w)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
            w.WriteLine("  :");
            w.WriteLine($"  :{logMessage}");
            w.WriteLine ("-------------------------------");
        }


    }
}


Creating Simple Service with ASP.NET MVC
Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Thanks a lot, @Alex. Can I also somehow hide the browser which is opened when I run the program?? – Ayush Singhania May 14 '19 at 14:22
  • 1
    You're welcome. You would need to run your asp.net-mvc as a service, https://support.microsoft.com/en-ca/help/2778398/creating-simple-service-with-asp-net-mvc-web-api-to-be-accessed-by-win – Alex Baban May 14 '19 at 14:50