Recieve SMS using C#

  • 1
  • Question
  • Updated 2 weeks ago
How can I receive SMS using C#

So I can send SMS using the api. But can't seem to find a way to receive them. Is it even possible? At this point I think I need something like ngrok? But Im not sure where to start.

Thanks In Advance
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb

Posted 3 weeks ago

  • 1
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,492 Points 5k badge 2x thumb
If you want to receive sms in real time, check this: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/SubscripotionT... It is powered by PubNub, so you don't need ngrok.

Alternatively, you can check WebHook which does require a public address: https://github.com/ringcentral/ringcentral-csharp-client/blob/master/RingCentral.Test/WebHookTest.cs

Or if you don't need real time sms notification, you can retrieve sms via HTTP GET /restapi/v1.0/account/~/extension/~/message-store

Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler

Thanks again! I will try these.
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler

Hmm I just plugged it in and tried the PubNub variant, It has an error can't quite figure out.

 await subscription.Register(); //Throws Object Reference not set to instance of object...



Photo of Tyler Long

Tyler Long, Official Rep

  • 8,432 Points 5k badge 2x thumb
Throws Object Reference not set to instance of object
Which object did it mean?

Maybe you can provide more information, preferably the full code snippet.
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Thanks. It appears to be Subscription is the problem, here is the code

ublic async void MessageNotifications()
        {
            RestClient rc = new RestClient("XXXNNNN", "XXXNNN123");           
            var subscription = rc.Restapi().Subscription().New();
            subscription.EventFilters.Add("/restapi/v1.0/account/~/extension/~/message-store");
            var presenceCount = 0;
            subscription.PresenceEvent += (sender, args) => {
                presenceCount += 1;
                Console.WriteLine(args.Message);
            };
            var messageCount = 0;
            var messageNotificationCount = 0;
            subscription.NotificationEvent += (sender, args) => {
                messageCount += 1;
                var message = args.message;
                Console.WriteLine(message);
                dynamic jObject = JObject.Parse(message);
                var eventString = (string)jObject.@event;
                if (new Regex("/account/\\d+/extension/\\d+/message-store").Match(eventString).Success)
                {
                    messageNotificationCount += 1;
                    var bodyString = JsonConvert.SerializeObject(jObject.body);
                    var messageEvent = JsonConvert.DeserializeObject<MessageEvent>(bodyString);
                //For now just show it...  
               txtIncomingSMS.text =  bodyString;
                   
                }
            };
            var statusCount = 0;
            subscription.StatusEvent += (sender, args) => {
                statusCount += 1;
                Console.WriteLine(args.Message);
            };
            await subscription.Register(); //Throws Object Reference not set to instance of object
}


Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
At this point, and based on  almost non-existent documentation I am making wild guesses here;
I think what is missing from this is - PubNubMessaging.Core;  . Am I in the ball park? Where is this thing located I tried PubNub.com but could not really find any reference to Pubnubmessaging.core.

Thanks again?


Photo of Tyler Long

Tyler Long, Official Rep

  • 8,432 Points 5k badge 2x thumb
You code seems pretty fine to me because it is just the code from the test cases.

At the moment I have no idea why it doesn't work. Let me write a program to reproduce the issue.
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,432 Points 5k badge 2x thumb
I have written a sample project and it runs pretty smooth without the "Object Reference not set to instance of object" error. https://github.com/tylerlong/RingCentralPasswordFlow/tree/PubNub

Could you please have a try? Pay attention it is the PubNub branch instead of master branch.

The code is pretty simple: https://github.com/tylerlong/RingCentralPasswordFlow/blob/PubNub/RingCentralPasswordFlow/Program.cs#...
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler,

Thanks, It will compile, its not doing anything however. Not sure what Im doing wrong here? I think I have the ring central server URL wrong, what are you using, can't seem to find this information...

Using this: Must be wrong??
 Environment.GetEnvironmentVariable("/restapi/v1.0/account/~/extension/~/message-store")
            );

(Edited)
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,432 Points 5k badge 2x thumb
I use Environment.GetEnvironmentVariable in my code because I don't want to hard code my credentials there.  You don't have to copy my example as long as you have ways to protect your credentials.

Environment.GetEnvironmentVariable("/restapi/v1.0/account/~/extension/~/message-store");  is wrong.  "/restapi/v1.0/account/~/extension/~/message-store" is the event filter string itself.

Let me explain the env vars:

RINGCENTRAL_CLIENT_ID: client ID of your RingCentral App
RINGCENTRAL_CLIENT_SECRET: client secret of your RingCentral App
RINGCENTRAL_SERVER_URL: https://platform.devtest.ringcentral.com for sandbox and https://platform.ringcentral.com for production.
RINGCENTRAL_USERNAME: username, this is normally in the form of a phone number
RINGCENTRAL_EXTENSION: such as 101, if your username is a direct number, you can set this to an empty string
RINGCENTRAL_PASSWORD: password


Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Thanks I do, I was just going fast, dumb of me I know ;{ Anyway Its now  throwing NullReferenceException

"FlurlHttpException was null"



Photo of Tyler Long

Tyler Long, Official Rep

  • 8,432 Points 5k badge 2x thumb
Do you mind posting a simple project to reproduce the issue to GitHub? Like the one I posted above?   Remember hide your credentials in environment variables.