1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import axios from "npm:axios";
import formData from "npm:form-data";
export const telegramSendMessage = async (botToken: string, options: {
chat_id: string | number;
text: string;
message_thread_id?: number;
parse_mode?: string;
entities?: any[];
disable_web_page_preview?: boolean;
disable_notification?: boolean;
protect_content?: boolean;
reply_to_message_id?: number;
allow_sending_without_reply?: boolean;
reply_markup?: any[];
}) =>
fetchJSON(
`https://api.telegram.org/bot${botToken}/sendMessage`,
{
method: "POST",
body: JSON.stringify({ ...options }),
},
);
export async function telegramSendAudioMessage(chatId: string, audioData: Uint8Array, botToken: string): Promise<void> {
const url = `https://api.telegram.org/bot${botToken}/sendAudio`;
// Assuming FormData is available or correctly polyfilled
let data = new FormData();
data.append("chat_id", chatId);
// Directly use the Uint8Array with Blob constructor
data.append("audio", new Blob([audioData], { type: "audio/mpeg" }), "feedback.mpga");
try {
// Replace axios with Deno's fetch API, adapted for your setup
const response = await fetch(url, {
method: "POST",
body: data, // FormData instance directly as the body
// Headers are set automatically by the browser, so they're omitted here
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
console.log(result);
} catch (error) {
console.error("Error sending audio message:", error.message);
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 11, 2024