Slack Integration - How to notify user with # when posted to channel - c#

I have this code that notifies a slack channel after a build has completed. In certain cases, whenever the build fails, I want to add a notification to myself so I can quickly turn issues around. How can I update this to include a notification to someone using the # symbol?
Some examples I see online use the SlackMessage constructor to add a userName, but for some reason I can not overload this constructor.
public static void PostToSlack()
{
string url= "...";
string slackUrl = GlobalData.slackUrl;
string buildName = TestContext.Parameters["BuildName"];
string buildID = TestContext.Parameters["BuildID"];
string testName = TestContext.CurrentContext.Test.Name;
string outcome = TestContext.CurrentContext.Result.Outcome.ToString();
//If tests failed but the suite actually completed, set more understandable outcome message
if (outcome == "Failed(Child)")
{
outcome = "Completed with Issues";
}
//Build the text string to post to slack
string postText = "Build Completed";
SlackMessage message = new SlackMessage
{
text = postText
};
//Convert serializable object to JSON
string json = JsonConvert.SerializeObject(message, Newtonsoft.Json.Formatting.Indented);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(slackUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Send to Slack
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
//Error checking if Slack sends back a 404 or 400
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}

Related

Coinbase API returns 401 for POST

I'm trying to use Coinbase's API to sell crypto currency, and I keep getting 401 errors. The below code works for all of the GET methods I've tried so far, but I can't figure out where I'm going wrong with the POST.
private static string GetWebResponse(string url, string command, string path, string body)
{
var timeStamp = EpochSeconds;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = command;
request.ContentType = "application/json";
request.Headers.Add("CB-VERSION", VersionDate);
request.Headers.Add("CB-ACCESS-KEY", ApiKey);
request.Headers.Add("CB-ACCESS-SIGN", GetAccessSign(timeStamp, command, path, body));
request.Headers.Add("CB-ACCESS-TIMESTAMP", timeStamp);
if (command == "POST")
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(body);
streamWriter.Flush();
streamWriter.Close();
}
}
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
return reader.ReadToEnd();
}
}
private static string GetAccessSign(string timestamp, string command, string path, string body)
{
var hmacKey = Encoding.UTF8.GetBytes(ApiSecret);
string data = timestamp + command + path + body;
using (var signatureStream = new MemoryStream(Encoding.UTF8.GetBytes(data)))
{
return new HMACSHA256(hmacKey).ComputeHash(signatureStream).Aggregate(new StringBuilder(), (sb, b) => sb.AppendFormat("{0:x2}", b), sb => sb.ToString());
}
}
I figure something is wrong with the body of the POST request, but it looks right to me.
EDIT:
I'm going to show how the body is formed for clarification...
var body = JsonConvert.SerializeObject(
new
{
commit = "false",
amount = Math.Round(sellAmount, 8).ToString(),
currency = "BTC",
payment_method = fiatWalletId
});
EDIT 2:
I tried all the same stuff as before, but using RestSharp, and it returns a more specific error:
{\"errors\":[{\"id\":\"authentication_error\",\"message\":\"invalid signature\"}]}
EDIT 3:
This API key is set up for all permissions / scopes, including the ones I need for this request:
wallet:accounts:create
wallet:accounts:delete
wallet:accounts:read
wallet:accounts:update
wallet:addresses:create
wallet:addresses:read
wallet:buys:create
wallet:buys:read
wallet:checkouts:create
wallet:checkouts:read
wallet:contacts:read
wallet:deposits:create
wallet:deposits:read
wallet:notifications:read
wallet:orders:create
wallet:orders:read
wallet:orders:refund
wallet:payment-methods:delete
wallet:payment-methods:limits
wallet:payment-methods:read
wallet:sells:create
wallet:sells:read
wallet:transactions:read
wallet:transactions:request
wallet:transactions:send
wallet:transactions:transfer
wallet:user:email
wallet:user:read
wallet:user:update
wallet:withdrawals:create
wallet:withdrawals:read
The solution for me was that I forgot to put /v2 (/v2 + some path) in the path part of the signature and the signature was bad, the response was misleading though saying unauthorized.

Unable to Post on Pinterest using their API

I'm receiving a 400 Bad Request error message when posting a pin on Pinterest. It works using Postman, but doesn't work programmatically. Using C#, has anyone been able to successfully post a pin on Pinterest without using the pinsharp wrapper?
private void postPinterest(string messages, string id, string usertoken, string image, string boardname, string username)
{
string link = null;
boardname = boardname.Replace(" ", "-");
string board = username + "/" + boardname;
string url = "https://api.pinterest.com/v1/pins?access_token=" + usertoken;
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrEmpty(board))
sb.Append("&board=" + HttpUtility.UrlEncode(board));
if (!string.IsNullOrEmpty(messages))
sb.Append("&note=" + HttpUtility.UrlEncode(messages));
if (!string.IsNullOrEmpty(link))
sb.Append("&image_url=" + HttpUtility.UrlEncode(link));
string postdata = sb.ToString().Substring(1);
PostData(url, postdata);
}
private object PostData(string url, string postdata)
{
object json=null;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
// req.Accept = "application/json";
using (var stream = req.GetRequestStream())
{
byte[] bindata = Encoding.ASCII.GetBytes(postdata);
stream.Write(bindata, 0, bindata.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
string response = new StreamReader(resp.GetResponseStream()).ReadToEnd();
json = JsonConvert.DeserializeObject<dynamic>(response);
return json;
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
string error = reader.ReadToEnd();
return json;
}
}
}
}
return json;
}
EDIT:
It doesn't work using the JSON format or x-www-form-urlencoded format.
I changed the content type to application/x-www-form-urlencoded and now I'm receiving the error message below. I receive 400 Bad Request error using JSON format:
"{\n \"message\": \"405: Method Not Allowed\",\n \"type\": \"http\"\n}"
The problem is the the parameter that you are posting.
In the Api i could find board as a parameter but both note and image comes under field parameter which specifies the return type JSON.
As per documentation on this page you can post in this format
https://api.pinterest.com/v1/boards/anapinskywalker/wanderlust/pins/?
access_token=abcde&
limit=2&
fields=id,link,counts,note
So I tried the following and its getting response
https://api.pinterest.com/v1/boards/?access_token="YourTokenWithoutQuotes"&fields=id%2Ccreator
Would suggest you to first test the Api you are hitting putting a breakpoint inside the PostData function and check if the passed url is in the correct format and compare it with Pininterest API Explorer.
As you might have already received authorization code and access token so I am assuming your post function should be working fine.
public string postPinterest(string access_token,string boardname,string note,string image_url)
{
public string pinSharesEndPoint = "https://api.pinterest.com/v1/pins/?access_token={0}";
var requestUrl = String.Format(pinSharesEndPoint, accessToken);
var message = new
{
board = boardname,
note = note,
image_url = image_url
};
var requestJson = new JavaScriptSerializer().Serialize(message);
var client = new WebClient();
var requestHeaders = new NameValueCollection
{
{"Content-Type", "application/json" },
{"x-li-format", "json" }
};
client.Headers.Add(requestHeaders);
var responseJson = client.UploadString(requestUrl, "POST", requestJson);
var response = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(responseJson);
return response;
}

Sending an HTTP PUT request with a parameter in C#

I have a tika server open on my PC, and I need to send it a request with a string parameter which is a path for a file I want tika to process. The code I have so far is:
private void getFromServer(string fileName)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9998/rmeta");
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "PUT";
httpWebRequest.KeepAlive = true;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string postData = #"C:\Users\######\Downloads\elasticsearch\elasticsearch-1.7.1.zip";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
string json = postData;
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
console.write(responseText);
}
}
The thing is, the server gets the request, but it returns the string I sent it instead of the contents of the file in question, and I know it can work because I sent it the same request using a cURL command and it worked. Any ideas what I'm doing wrong?
PS the hashtags are instead of my username

Error in json post request for mailchimp api

I am trying to create a wrapper for:
this mailchimp method using C#.Net
I understand that there are already .Net wrappers available. I am using Mailchimp.Net by Dan Esparza. Method in the wrapper class is giving exception for the api method mentioned above.It is throwing internal server exception (500) which I am not sure why, so I decided to create my own wrapper for the particular method. I have following code:
private void CreateGrouping(string apiKey, string listId,string groupName,string groupValue)
{
string url = "https://us9.api.mailchimp.com/2.0/lists/interest-grouping-add";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
List<string> values = new List<string>();
values.Add(groupValue);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
apiKey = apiKey,
id = listId,
name = groupName,
type="radio",
groups = values
});
streamWriter.Write(json);
}
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
But on execution of var response = (HttpWebResponse)httpWebRequest.GetResponse(); is throwing same exception - internal server error (500).
It might be that I am passing data in wrong way ? Can someone please help me in finding out what am I missing here ?
As I suspected I was passing the data in wrong way: apiKey has to be apikey (k was in caps)
string json = new JavaScriptSerializer().Serialize(new
{
apikey = apiKey,
id = listId,
name = groupName,
type="radio",
groups = values
});
Other than this, I added:
streamWriter.Flush();
streamWriter.Close();
It might help someone save sometime.

stackoverflow search api

I would like to use the search method of stackoverflow API to return the json structure of results based on a search keyword and then display those results (title, description and the url) in the SearchResults div.
I am new to C# and my first attempt went something like this:
protected void searchStockOverflow(string y)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.stackoverflow.com/1.1/search?intitle="+y);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{ \"intitle\": \"" + y + "\"}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
SearchResults.InnerHtml += "<div style='border:1px solid blue;margin:5px;'>";
SearchResults.InnerHtml += responseText + "<br />";
SearchResults.InnerHtml += "</div><br style='clear:both;' />";
}
}
The issue is that what is returned looks like dingbats rubbish - i guess because it is serialized and need to be deserialized?
I would definitely say consider using the REST client; however, to look at the issues... generally you want to deserialize the data as JSON manually, then run that data through your UI code. For example:
static void SearchStackOverflow(string y)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.stackoverflow.com/1.1/search?intitle=" + Uri.EscapeDataString(y));
httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string responseText;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
var result = (SearchResult)new JavaScriptSerializer().Deserialize(responseText, typeof(SearchResult));
.... do something with result ...
}
class SearchResult
{
public List<Question> questions { get; set; }
}
class Question
{
public string title { get; set; }
public int answer_count { get; set; }
}
Which uses the JavaScriptSerializer from System.Web.Extensions.dll
Also Take a look at Stacky StackApps .Net Client Library which is REST-based API that provides access to stackoverflow family of websites.
Unfortunately I'm on my Mac and can't run a test on your code. You might want to check the character encoding of both your page and the response stream coming back. If they don't match; it could cause the characters coming from the response stream to be rendered incorrectly, hence the rubbish you're seeing.

Categories