Last active
April 17, 2024 06:28
-
-
Save whatsmate/fc844214762e362d116960e2a03b89ac to your computer and use it in GitHub Desktop.
How to send a PDF file to a WhatsApp group in C# using Visual Studio 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Net; | |
| using System.Text.Json; | |
| using System.IO; | |
| using System.Text; | |
| class WaPdfGroupSender | |
| { | |
| // TODO: Replace the following with your gateway instance ID, client ID and secret! | |
| private static string INSTANCE_ID = "YOUR_INSTANCE_ID"; | |
| private static string CLIENT_ID = "YOUR_CLIENT_ID_HERE"; | |
| private static string CLIENT_SECRET = "YOUR_CLIENT_SECRET_HERE"; | |
| private static string DOCUMENT_GROUP_API_URL = "https://api.whatsmate.net/v3/whatsapp/group/document/message/" + INSTANCE_ID; | |
| static void Main(string[] args) | |
| { | |
| WaPdfGroupSender groupDocSender = new WaPdfGroupSender(); | |
| // TODO: Put down the unique name of your group here | |
| string group = "YOUR UNIQUE GROUP NAME HERE"; | |
| // TODO: Remember to copy the JPG from ..\assets to the TEMP directory! | |
| string base64Content = convertFileToBase64("C:\\TEMP\\subwaymap.pdf"); | |
| string fn = "anyname.pdf"; | |
| string caption = "You should find the map handy."; // optional; can be empty | |
| groupDocSender.sendGroupDocument(group, base64Content, fn, caption); | |
| Console.WriteLine("Press Enter to exit."); | |
| Console.ReadLine(); | |
| } | |
| // http://stackoverflow.com/questions/25919387/c-sharp-converting-file-into-base64string-and-back-again | |
| static public string convertFileToBase64(string fullPathToDoc) | |
| { | |
| Byte[] bytes = File.ReadAllBytes(fullPathToDoc); | |
| String base64Encoded = Convert.ToBase64String(bytes); | |
| return base64Encoded; | |
| } | |
| public bool sendGroupDocument(string group, string base64Content, string fn, string caption) | |
| { | |
| bool success = true; | |
| try | |
| { | |
| HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(DOCUMENT_GROUP_API_URL); | |
| httpRequest.Method = "POST"; | |
| httpRequest.Accept = "application/json"; | |
| httpRequest.ContentType = "application/json"; | |
| httpRequest.Headers["X-WM-CLIENT-ID"] = CLIENT_ID; | |
| httpRequest.Headers["X-WM-CLIENT-SECRET"] = CLIENT_SECRET; | |
| GroupDocumentPayload payloadObj = new GroupDocumentPayload() { group_name = group, caption = caption, document = base64Content, filename = fn}; | |
| string postData = JsonSerializer.Serialize(payloadObj); | |
| using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream())) | |
| { | |
| streamWriter.Write(postData); | |
| } | |
| var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); | |
| using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
| { | |
| var result = streamReader.ReadToEnd(); | |
| Console.WriteLine(result); | |
| } | |
| } | |
| catch (WebException webExcp) | |
| { | |
| Console.WriteLine("A WebException has been caught."); | |
| Console.WriteLine(webExcp.ToString()); | |
| WebExceptionStatus status = webExcp.Status; | |
| if (status == WebExceptionStatus.ProtocolError) | |
| { | |
| Console.Write("The REST API server returned a protocol error: "); | |
| HttpWebResponse? httpResponse = webExcp.Response as HttpWebResponse; | |
| Stream stream = httpResponse.GetResponseStream(); | |
| StreamReader reader = new StreamReader(stream); | |
| String body = reader.ReadToEnd(); | |
| Console.WriteLine((int)httpResponse.StatusCode + " - " + body); | |
| success = false; | |
| } | |
| } | |
| catch (Exception e) | |
| { | |
| Console.WriteLine("A general exception has been caught."); | |
| Console.WriteLine(e.ToString()); | |
| success = false; | |
| } | |
| return success; | |
| } | |
| public class GroupDocumentPayload | |
| { | |
| public string group_name { get; set; } | |
| public string caption { get; set; } | |
| public string document { get; set; } | |
| public string filename { get; set; } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write-up: https://whatsmate.github.io/2022-07-18-send-whatsapp-pdf-group-c-sharp-dot-net-vs2022/