I'm trying to implement Twilio to my WP8 application so I can send messages, I have downloaded the API helper and I have tried implement the c# just to test if it works. I am only a beginner at programming and I don't really understand why it wont work.
class sendmessages
{
private static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/user/account
string AccountSid = "......"; //My own details
string AuthToken = "......."; //My own details
var twilio = new TwilioRestClient(AccountSid, AuthToken);
var message = twilio.SendSmsMessage("From" "TO", "hello ");
Console.WriteLine(message.Sid);
}
}
Is there something I'm missing to do cause the Twilio Website is not very helpful, as the documentation is not very clear. I don't want anyone to do it for me, any tutorials that would help would be much appreciated.
Twilio evangelist here.
Are you getting a specific error?
To start debugging this I'd suggest checking to make sure that the RestException property is null:
var message = twilio.SendSmsMessage("[YOUR_FROM_NUMBER]","[YOUR_TO_NUMBER]","[MESSAGE_BODY]");
if (message.RestException != null) {
//An error happened calling the REST API
Debug.Writeline(message.RestException.Message);
}
If RestException is null, I'd suggest checking the logs in the Twilio developer portal to see if Twilio reports sending a message.
If neither of those two things work, my next suggestion would be to use Fiddler to watch the actual HTTP request the library is making to the REST API and see what the result is.
Hope that helps.
Related
I have a request I'm making to the Bing Ads API that looks like this:
var _campaignManagementService = new ServiceClient<ICampaignManagementService>(
_authorizationData,
apiEnvironment
);
var getCampaignRequest = new GetCampaignsByIdsRequest
{
AccountId = <the-accountid-is-here>,
CampaignIds = new List<long>() { <the-campaignid-is-here> }
};
var getCampaignResponse = (await _campaignManagementService.CallAsync((s, r) => s.GetCampaignsByIdsAsync(r), getCampaignRequest));
When I run this, an exception is thrown on the last line that reports:
"Invalid client data. Check the SOAP fault details for more information."
Unfortunately, using the API, I'm not seeing the details of the actual response. Is there a way to get at this info? (Short of rewriting the whole thing without using the API?)
Edit:
Thanks to Panagiotis Kanavos I found the solution i think. This is for a different api (reporting) but it should look the same.
The exception has an InnerException that is of type System.ServiceModel.FaultException<Microsoft.BingAds.V13.Reporting.AdApiFaultDetail> or something similar. This has the Detail property with additional information:
Original post:
Can't seem to be able to easily inspect the SOAP request. But you can sniff the request with Fiddler classic The classic version is free.
I am currently working on a OAuth2 implementation. However I am stuck on an Error 401. It seems like there is something wrong with my post request that is supposed to retrieve the access token from the Company the User logged in to. This is my code:
internal void RequestAccessToken(string code)
{
string requestBody = "grant_type="+ WebUtility.UrlEncode(GRANTTYPE)+ "&code=" + WebUtility.UrlEncode(code)+"&redirect_uri="+ WebUtility.UrlEncode(REDIRECT_URI);
WebClient client = new WebClient();
client.Headers.Add("Authorization",HeaderBase64Encode(CLIENT_ID, SECRETKEY));
var response = client.UploadString("https://thewebsiteiamcallingto.com/some/api", requestBody);
var responseString = client.OpenRead("https://thewebsiteiamcallingto.com/some/api");
}
My Questions are:
Is there anything wrong with the way I try to make the POST request ?
Is there a way to retrieve the whole string that is posted to the URI using UploadString?
P.S. I have seen this post regarding the POST creation. However I find the async part to be too complicated for my case.
Since we dont know the api documentation, I would suggest you to make a postman request and view the actual request sent and response received, and secondly make a request using your method and capture using a utility like wireshark and compare the difference.
I can't find an example of sending message by telegram protocol from C#. I tried to use this but failed.
Can you give me any examples?
TLSharp is basic implementation of Telegram API on C#. See it here https://github.com/sochix/TLSharp
You can use the WTelegramClient library to connect to Telegram Client API protocol (as a user, not a bot)
The library is very complete but also very easy to use. Follow the README on GitHub for an easy introduction.
To send a message to someone can be as simple as:
using TL;
using var client = new WTelegram.Client(); // or Client(Environment.GetEnvironmentVariable)
await client.LoginUserIfNeeded();
var result = await client.Contacts_ResolveUsername("USERNAME");
await client.SendMessageAsync(result.User, "Hello");
//or by phone number:
//var result = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
//client.SendMessageAsync(result.users[result.imported[0].user_id], "Hello");
For my bot I use Telegram.Bot nuget package. Full sample code is here.
Here is example of sending message in reply to incoming message.
// create bot instance
var bot = new TelegramBotClient("YourApiToken");
// test your api configured correctly
var me = await bot.GetMeAsync();
Console.WriteLine($"{me.Username} started");
// start listening for incoming messages
while (true)
{
//get incoming messages
var updates = await bot.GetUpdatesAsync(offset);
foreach (var update in updates)
{
// send response to incoming message
await bot.SendTextMessageAsync(message.Chat.Id,"The Matrix has you...");
}
}
The simplest way is to send http request directly to the Telegram BOT API as url string, you may test those url strings even in your browser, please see details in my another answer here:
https://stackoverflow.com/a/57341990/11687179
at the first step you have to generate a bot in botfather then use the code in bellow in C#
private void SendMessage(string msg)
{
string url = "https://api.telegram.org/{botid}:{botkey}/sendMessage?chat_id={#ChanalName}&text={0}";
WebClient Client = new WebClient();
/// If you need to use proxy
if (Program.UseProxy)
{
/// proxy elements are variable in Program.cs
Client.Proxy = new WebProxy(Program.ProxyUrl, Program.ProxyPort);
Client.Proxy.Credentials = new NetworkCredential("hjolany", "klojorasic");
}
Client.DownloadString(string.Format(url, msg));
}));
}
Telegram has an official API that can do exactly what you need, you will have to look into http requests though..
Here is the documentation on sending a message:
Function
messages.sendMessage
Params
peer InputPeer User or chat where a message will be sent
message string Message text
random_id long Unique client message ID required to prevent message resending
Query example
(messages.sendMessage (inputPeerSelf) "Hello, me!" 12345678901)
Return errors
Code Type Description
400 BAD_REQUEST PEER_ID_INVALID Invalid peer
400 BAD_REQUEST MESSAGE_EMPTY Empty or invalid UTF8 message was sent
400 BAD_REQUEST MESSAGE_TOO_LONG Message was too long.
Current maximum length is 4096 UTF8 characters
For the full documentation go here.
I tried this code, which actually made the call to my phone, when I answered I heard some default voice message:
var twilio = new TwilioRestClient(AccountSid, AuthToken);
Call call = twilio.InitiateOutboundCall("+97243000000", "+972547000000", "http://demo.twilio.com/welcome/voice");
if (call.RestException != null)
{
var error = call.RestException.Message;
// handle the error ...
}
I would like to upload my own recorded message which will be played when the call made.
The only related thing I found is how to consume the records, but not how to upload one: https://www.twilio.com/docs/api/rest/recording
Can anybody help me with that? I tried to find it on the Twilio manuals, but could not find anything that works with C#
Thanks
Twilio evangelist here.
You need to replace the URL parameter you've got in the InitiateOutboundCall method with your own URL:
twilio.InitiateOutboundCall("+97243000000", "+972547000000", "http://yourserver.com/play");
This URL should return to Twilio some TwiML that includes the Play verb. Play lets you specify a .wav or .mp3 file that Twilio should play to the caller:
http://yourserver.com/message.mp3
If you don't want to set up your own URL and you just need static TwiML, you could use a service like twimlbin.com to host some static TwiML for you.
Hope that helps.
I'm getting 404- Not Found error during google's authentication in windows phone 7.5 app.
public void login()
{
var cli = new RestClient("https://accounts.google.com/o/oauth2/auth");
cli.Authenticator = new HttpBasicAuthenticator(user, password);
var request = new RestRequest("token", Method.POST);
cli.ExecuteAsync(request, response =>
{
MessageBox.Show(response.Content);
});
}
I given valid login credential, but its shows the response as 404-NotFound. could please help me to resolve this issue
Thanks
For some reason the URI is probably not formed correctly.
If you check the documentation you'll notice an example URI which obviously works if you click on it. That means that the service exists and everything is OK with it. You are doing something wrong with RestSharp, and for that I recommend a detailed tutorial available on CodeProject called Google OAuth2 on Windows Phone