-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.gs
132 lines (112 loc) · 3.96 KB
/
code.gs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* It's a open source software.
* It's under MIT License as published.
* You can modify and/or redistribute the code.
*
* <ZhenniOpenAI_Bot>
* Copyright (C) 2023 Jenxieny Alviana Christin Olivia
* Github: https://github.com/jnxnyanna
* Telegram: https://t.me/jnxnyanna
* Telegram: https://t.me/ZhenniOpenAI_Bot
*/
// Base API URL for OpenAI.
// Change the admin id with your telegramid.
// You can get it from @userinfobot.
// You can add more than 1 id.
// Example: ["1234567890", "5577901126"];
const BASE_URL = "https://api.openai.com/v1/completions";
const ADMIN_LIST = ["1234567890"]; // Change this with your telegram id
// Defined the property in Settings > Script Properties.
// Add properties with name "bot_token" & "api_key_openai".
// Get your "bot_token" from t.me/BotFather.
// Get your "api_key_openai" from https://beta.openai.com/account/api-keys.
// https://developers.google.com/apps-script/reference/properties?hl=en
const properties = PropertiesService.getScriptProperties();
const BOT_TOKEN = properties.getProperty("bot_token");
const OPENAI_API_KEY = properties.getProperty("api_key_openai");
// Replace <YOUR_APP_URL> with the URL of your deployed app.
// Go to Apply > New deployments > Choose a type > Web application
// Enter with this detail
// Version: New version
// Description: Your description (optional)
// Under the web application
// Run as: Me (yourname@gmail.com)
// Who has access: Anyone
const APP_URL = "<YOUR_APP_URL>";
// Init the project.
// Define variable bot with lumpia.
// Define variable client with bot.telegram or bot.tg.
const bot = new lumpia.init(BOT_TOKEN);
const client = bot.telegram;
function doPost(e) {
// Only POST data will be processed.
// Bot will be processing this POST data.
bot.doPost(e);
}
function setWebhook() {
// Set webhook for the bot.
// If the bot receive for new updates, the data will be processed at doPost() function.
let result = client.setWebhook(APP_URL);
console.log(result);
}
function requestToOpenAi(text) {
try {
var options = {
"method": "POST",
"headers": {
"Authorization": "Bearer " + OPENAI_API_KEY,
"Content-Type": "application/json"
},
"payload": JSON.stringify({
model: "text-davinci-003",
prompt: text,
max_tokens: 1000,
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
})
}
// Send the request to OpenAI and print the result on log.
// Initialize the response text.
var result = JSON.parse(UrlFetchApp.fetch(BASE_URL, options).getContentText());
return result.choices[0].text;
} catch (e) {
// Catching error.
// Print error to the console.
console.log(e);
}
}
/*function replyMsg(chatId, text, msgId, options) {
// Send answer from OpenAI to user.
return client.sendMessage(chatId, text, { reply_to_message_id: msgId, ...options });
}*/
function sendErrorToAdmin(error = Object) {
for (var i in ADMIN_LIST) {
client.sendMessage(ADMIN_LIST[i], "New error: " + error.message);
}
}
// Let the bot to hear all messages.
bot.on("message", async (ctx) => {
const text = ctx.message.text; // It's a text.
// Only text allowed.
// Check if user send another message types, like photo, document or etc.
// Delete the message if the message types is not a text.
if (!text) return ctx.deleteMessage();
// Start command.
// This command will be executed first.
// Don't ask this text to OpenAI, so I put return on the end of block.
if (text == "/start") return ctx.replyIt("Hello! I'm AI Bot. You can ask me anything.");
try {
// Ask to OpenAI.
// Reply the result to user
// Let the bot to send action with typing.
// Use ctx.replyWithChatAction("typing");
ctx.replyWithChatAction("typing");
var resultText = await requestToOpenAi(text);
ctx.replyIt(resultText);
} catch (e) {
sendErrorToAdmin(e);
ctx.replyIt("Sorry, but I can't process your request right now. :'(");
}
});