Created
July 7, 2022 08:48
-
-
Save whatsmate/bdd23ecc3cf5ba52cf20b246ffa25ad4 to your computer and use it in GitHub Desktop.
How to send a voice note file to a Telegram group in C#
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.Web.Script.Serialization; // requires the reference 'System.Web.Extensions' | |
| using System.IO; | |
| using System.Text; | |
| class TelegramGroupOpusSender | |
| { | |
| // 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 VOICE_GROUP_API_URL = "https://api.whatsmate.net/v3/telegram/group/voice_note/message/" + INSTANCE_ID; | |
| static void Main(string[] args) | |
| { | |
| TelegramGroupOpusSender opusSender = new TelegramGroupOpusSender(); | |
| // TODO: Put down group name and group admin below | |
| string group_name = "Muscle Men Club"; | |
| string group_admin = "19159876123"; | |
| // TODO: Remember to copy the OPUS file from ..\assets to the TEMP directory! | |
| string base64Content = convertFileToBase64("C:\\TEMP\\martin-luther-king.opus"); | |
| string fn = "anyname.opus"; | |
| string caption = "I have a dream"; | |
| opusSender.sendVoiceNoteToGroup(group_name, group_admin, 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 sendVoiceNoteToGroup(string group_name, string group_admin, string base64Content, string fn, string caption) | |
| { | |
| bool success = true; | |
| try | |
| { | |
| using (WebClient client = new WebClient()) | |
| { | |
| client.Headers[HttpRequestHeader.ContentType] = "application/json"; | |
| client.Headers["X-WM-CLIENT-ID"] = CLIENT_ID; | |
| client.Headers["X-WM-CLIENT-SECRET"] = CLIENT_SECRET; | |
| GroupVoicePayload payloadObj = new GroupVoicePayload() { group_name = group_name, group_admin = group_admin, voice_note = base64Content, filename = fn, caption = caption}; | |
| string postData = (new JavaScriptSerializer()).Serialize(payloadObj); | |
| client.Encoding = Encoding.UTF8; | |
| string response = client.UploadString(VOICE_GROUP_API_URL, postData); | |
| Console.WriteLine(response); | |
| } | |
| } | |
| catch (WebException webEx) | |
| { | |
| Console.WriteLine(((HttpWebResponse)webEx.Response).StatusCode); | |
| Stream stream = ((HttpWebResponse)webEx.Response).GetResponseStream(); | |
| StreamReader reader = new StreamReader(stream); | |
| String body = reader.ReadToEnd(); | |
| Console.WriteLine(body); | |
| success = false; | |
| } | |
| return success; | |
| } | |
| public class GroupVoicePayload | |
| { | |
| public string group_name { get; set; } | |
| public string group_admin { get; set; } | |
| public string voice_note { get; set; } | |
| public string filename { get; set; } | |
| public string caption { 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-06-29-send-telegram-group-voice-note-c-sharp-dot-net/