-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
230 lines (205 loc) · 6.82 KB
/
bot.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const express = require('express');
const expressApp = express();
const path = require('path');
const axios = require('axios');
const port = process.env.PORT || 8080;
const { default: OpenAI } = require('openai');
const fs = require("fs");
const token = require('dotenv').config();
const { Console } = require("console");
const { error } = require('console');
const myLog = new Console({
stdout: fs.createWriteStream("errStdErr.txt"),
stderr: fs.createWriteStream("errStdErr.txt")
})
const { Telegraf } = require('telegraf');
const bot = new Telegraf(process.env.BOT_TOKEN)
expressApp.use(express.static('static'));
expressApp.use(express.json());
expressApp.get('/', (req, res)=>{
res.sendFile(path.join(__dirname + '/index.json'))
});
// expressApp.listen(port, ()=> myLog.log(`Listening on ${port}`));
//start process
bot.command('start', ctx =>{
myLog.log(ctx.from)
bot.telegram.sendMessage(ctx.chat.id, `Yo!!😜 This is the oXe-🤖.\nClick /features to see what i can do.`,{
})
});
//features
bot.command('features', ctx =>{
myLog.log(ctx.from)
bot.telegram.sendMessage(ctx.chat.id, `/eth For ETH price\n/btc For BTC price\n/weather For weather\n/sol For SOL price`)
});
//check ethereum price
bot.command('eth', ctx =>{
var rate;
myLog.log(ctx.from);
axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd`)
.then(response => {
myLog.log(response.data);
rate = response.data.ethereum
const message = `Ethereum is $${rate.usd}`
bot.telegram.sendMessage(ctx.chat.id, message, {
})
})
});
//check bitcoin price
bot.command('btc', ctx =>{
var rate;
myLog.log(ctx.from);
axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd`)
.then(response => {
myLog.log(response.data);
rate = response.data.bitcoin
const message = `Bitcoin is $${rate.usd}`
bot.telegram.sendMessage(ctx.chat.id, message, {
})
})
})
//check sol price
bot.command('sol', ctx =>{
var rate;
myLog.log(ctx.from);
axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd`)
.then(response => {
myLog.log(response.data);
rate = response.data.solana
const message = `Solana is $${rate.usd}`
bot.telegram.sendMessage(ctx.chat.id, message, {
})
})
});
// check weather
const appID = (process.env.APP_ID);
const appURL = (city) => (
`http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&&appid=${appID}`
);
const weatherFeedback = (name, main, weather, wind, clouds) => (
`Weather in <b>${name}</b>\n
${weather.main} - ${weather.description}\n
Temperature: <b>${main.temp}°C</b>\n
Pressure: <b>${main.pressure}hpa</b>\n
Humidity: <b>${main.humidity}%</b>\n
Wind: <b>${wind.speed}m/s</b>\n
Clouds: <b>${clouds.all}%</b>\n
`
);
const getCityWeather = (chatId, city) =>{
const endpoint = appURL(city);
axios.get(endpoint).then((resp) => {
const { name, main, weather, wind, clouds } = resp.data;
myLog.log("API Endpoint:", endpoint);
bot.telegram.sendMessage(
chatId,
weatherFeedback(name, main, weather[0], wind, clouds), {
parse_mode: "HTML"
}
);},
error => {
myLog.log("error", error);
bot.telegram.sendMessage(
chatId, `Weather for <b>${city}</b> unavailable🤨`, {
parse_mode: "HTML"
}
);
});
}
bot.command('weather', ctx =>{
myLog.log(ctx.from)
const chatId = ctx.chat.id;
const city = ctx.message.text.split(' ')[1];
if (city === undefined) {
bot.telegram.sendMessage(
chatId, `Please provide city name as \n/weather 'city'`
);
return;
} else {
getCityWeather(chatId, city);
}});
//add clear feature: that clears all messages
bot.command('clear', ctx => {
myLog.log(ctx.from, 'Successfully cleared all messages');
const messageId = ctx.message.message_id;
for (let i = messageId - 1000; i <= 1000; i++){
try { bot.telegram.deleteMessage(ctx.chat.id, i)
}
catch(error){
myLog.log(`Error`)
}
}})
//add mention feature
// bot.textMention((ctx)=> {
// myLog.log(ctx.from),
// bot.telegram.sendMessage(ctx.chat.id, `${userInfo.username} spotted. FBI open up!!!!\n\nKidding😶, send /features.`)
// })
//add health advice
//add meme feature
//add openai(chat) feature
const openai = new OpenAI({apiKey: process.env.OPEN_AI})
bot.command('ai', async (ctx) =>{
const chatId = ctx.chat.id
const user = ctx.chat.username
const text = ctx.text
const response = await openai.chat.completions.create({
model:'gpt-3.5-turbo',
prompt:`Using Domain Expansion'Infinite Wisdom' to explain: ${text}\n\n YA:'`,
"temperature": 0.5,
})
const res = response.data.choices[0].text
bot.telegram.sendMessage(chatId, res, `You 👉🏾 ${user} no go use AI ke🌚\nUse /ai "What you need to be explained"`)
} )
//anime feature: bring up manga panels OR a RANDOM anime Image.
const vog = (search) => (`https://api.panelsdesu.com/v1/search?q=${search}`);
const des = (panels) => {
`${panels.description}`
};
const getPhotoUrl = (panels) => `${panels.image_url}`;
const getRandomPanel = (panels) => {
const randomIndex = Math.floor(Math.random() * panels.length);
return panels[randomIndex];
};
// myLog.log(des);
const aniP = (search, chatId) => {
const fig = vog(search);
axios.get(fig).then((resp) => {
const { panels }= resp.data;
myLog.log("API Endpoint:", fig);
// myLog.log("API Response:", panels);
if (panels && panels.length > 0) {
const randomPanel = getRandomPanel(panels);
const photoUrl = getPhotoUrl(randomPanel);
const description = des(randomPanel);
bot.telegram.sendPhoto(
chatId, photoUrl,
des(panels[0]), {
caption: description,
parse_mode: "HTML"
}
);} else {
bot.telegram.sendMessage(
chatId, `No theme for <b>${search}</b>🤨`, {
parse_mode: "HTML"
}
);
} error => {
myLog.log("error", error);
bot.telegram.sendMessage(
chatId, `Theme for <b>${search}</b> unavailable 🤨`, {
parse_mode: "HTML"
})}
})};
bot.command('manga', ctx => {
myLog.log(ctx.from)
const chatId = ctx.chat.id;
const search = ctx.message.text.split(' ')[1];
if(search === undefined) {
bot.telegram.sendMessage(
chatId, `What would you want to see😏\n/manga 'theme'`
);
return;
} else {
aniP(search, chatId);
}
})
bot.launch();