Created
July 6, 2022 09:22
-
-
Save whatsmate/8bd5b4e60368ae5fea67dcc2e487632e to your computer and use it in GitHub Desktop.
How to send an MP3 file to a Telegram group in Visual Basic Script
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
| Sub Main_Routine() | |
| ''' The first parameter is the name of the group | |
| ''' The second parameter is the number of the group admin | |
| ''' The third parameter is the full path to the audio file (e.g. MP3 file) | |
| ''' The fourth parameter is the filename displayed to the recipient. | |
| ''' The fifth parameter is the caption for the audio file. | |
| TelegramAudio_GroupSend "Muscle Men Club", "19159876123", "C:\Users\Public\ocean-waves.mp3", "anyname.mp3", "Enjoy the nature" ''' TODO: Specify the recipient's number here. NOT the gateway number | |
| End Sub | |
| Sub TelegramAudio_GroupSend(ByRef group_name As String, ByRef group_admin As String, ByRef strFilename As String, ByRef strDisplayName As String, ByRef strCaption As String) | |
| Dim INSTANCE_ID As String, CLIENT_ID As String, CLIENT_SECRET As String, API_URL As String | |
| Dim strJson As Variant | |
| Dim sHTML As String | |
| Dim oHttp As Object | |
| Dim contentInBase64 As String | |
| ''' TODO: Replace the following with your gateway instance ID, your Premium Account client ID and secret: | |
| INSTANCE_ID = "YOUR_INSTANCE_ID_HERE" | |
| CLIENT_ID = "YOUR_CLIENT_ID_HERE" | |
| CLIENT_SECRET = "YOUR_CLIENT_SECRET_HERE" | |
| API_URL = "https://api.whatsmate.net/v3/telegram/group/audio/message/" & INSTANCE_ID | |
| contentInBase64 = ConvertFileToBase64(strFilename) | |
| strJson = "{""group_name"": """ & group_name & """, ""group_admin"": """ & group_admin & """, ""audio"": """ & contentInBase64 & """, ""filename"": """ & strDisplayName & """, ""caption"": """ & strCaption & """}" | |
| Set oHttp = CreateObject("Msxml2.XMLHTTP") | |
| oHttp.Open "POST", API_URL, False | |
| oHttp.setRequestHeader "Content-type", "application/json" | |
| oHttp.setRequestHeader "X-WM-CLIENT-ID", CLIENT_ID | |
| oHttp.setRequestHeader "X-WM-CLIENT-SECRET", CLIENT_SECRET | |
| oHttp.Send strJson | |
| sHTML = oHttp.ResponseText | |
| MsgBox sHTML | |
| End Sub | |
| ''' ================================================================= | |
| ''' You do not need to change anything below this line. | |
| ''' ================================================================= | |
| ''' The following function is taken from the page below. Author: Cain Hill | |
| ''' https://medium.com/cainhill/how-to-use-vba-to-convert-a-file-to-base-64-d124c9b2958a | |
| Public Function ConvertFileToBase64(strFilePath As String) As String | |
| Const UseBinaryStreamType = 1 | |
| Dim streamInput: Set streamInput = CreateObject("ADODB.Stream") | |
| Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM") | |
| Dim xmlElem: Set xmlElem = xmlDoc.createElement("tmp") | |
| streamInput.Open | |
| streamInput.Type = UseBinaryStreamType | |
| streamInput.LoadFromFile strFilePath | |
| xmlElem.DataType = "bin.base64" | |
| xmlElem.nodeTypedValue = streamInput.Read | |
| ConvertFileToBase64 = Replace(xmlElem.text, vbLf, "") | |
| Set streamInput = Nothing | |
| Set xmlDoc = Nothing | |
| Set xmlElem = Nothing | |
| End Function |
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-28-send-telegram-group-mp3-vba/