-
Notifications
You must be signed in to change notification settings - Fork 0
/
cerate.txt
153 lines (98 loc) · 3.72 KB
/
cerate.txt
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Telegraf } from "telegraf";
import User from "./src/User.js";
import Event from "./src/Event.js";
import connectDb from "./src/config/Db.js";
import {message} from "telegraf/filters"
import OpenAI from 'openai';
import { model } from "mongoose";
var bot = new Telegraf(process.env.TELEGRAM_API);
const openai = new OpenAI({
apiKey: process.env['OPEN_AI'], // This is the default and can be omitted
});
try{
connectDb();
console.log('Database connected successfully');
}
catch(err){
console.log(err);
process.kill(process.pid, 'SIGTERM');
}
bot.start( async (ctx) => {
const form = ctx.message.from;
console.log('form', form );
try{
await User.findOneAndUpdate({tgId: form.id}, {
name: form.first_name,
username: form.username,
isBot: form.is_bot
}, {upsert: true, new: true});
await ctx.reply(`Welcome ${form.first_name}, I WILL WRITING HIGHLY ENGAGING CONTENT FOR YOU!, PLEASE SEND ME A PROMPT TO GET STARTED!`);
}
catch(err){
console.log(err);
await ctx.reply('An error occured while trying to save your data, please try again later');
}
});
bot.help((ctx) => ctx.reply('Send me a message @Exthi'));
bot.command('DO', async (ctx) => {
const from = ctx.update.message.from;
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date();
endOfDay.setHours(23, 59, 59, 999);
try {
const events = await Event.find({
tgId: from.id,
createdAt: {
$gte: startOfDay,
$lt: endOfDay
}
});
if (events.length === 0){
await ctx.reply('You have not sent any prompt for today, please send a prompt to get started!');
return;
}
console.log('events', events);
const completion = await openai.chat.completions.create({
message: [
{
role: 'system',
content: 'Act as a Senior Content Writer for a Company, you need to write highly engaging content about the upcoming events, and caption for the social media post.'
},
{
role: 'user',
content: `Write like a human, for humans. Message should sound formal and engaging: ${events.map(event => event.text).join(', ')}`
},
],
model: process.env.OPEN_AI_MODEL,
});
console.log('completion', completion);
await User.findOneAndUpdate({tgId: from.id}, {
$inc: {promptTokens: completion.usage.prompts, completionTokens: completion.usage.completions}
});
console.log('completion', completion.choices.message.content);
await ctx.reply(completion.choices.message.content);
} catch (err) {
console.error('Error:', err);
await ctx.reply('An error occurred while processing your request.');
}
});
bot.on(message("text"), async (ctx) => {
const text = ctx.update.message.text;
console.log('text', text);
try{
await Event.create({
text: messages,
tgId: ctx.update.message.from.id
});
await ctx.reply(`I have received your prompt, please wait while I generate a highly engaging content for you!, to genrate now please enter /DO`);
}
catch(err){
console.log(err);
await ctx.reply('An error occured while trying to save your data, please try again later');
}
});
// IT WILL GENERATE BTWEEN 24 HRS
bot.launch()
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))