Skip to content

Commit c02ce81

Browse files
authored
Merge pull request #709 from ArtBlocks/linds/integrate-insights-endpoint
Integrate Insights API
2 parents bb35b45 + 15a2e0d commit c02ce81

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/Classes/InsightsBot.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as dotenv from 'dotenv'
2+
dotenv.config()
3+
import { EmbedBuilder, Message } from 'discord.js'
4+
import axios from 'axios'
5+
import { randomColor } from '../Utils/smartBotResponse'
6+
7+
export class InsightsBot {
8+
async getInsightsApiResponse(msg: Message): Promise<EmbedBuilder> {
9+
try {
10+
// strip out !artBot from the message
11+
const messageContent = msg.content.replace('!artBot', '').trim()
12+
13+
const insightsResponse = await axios.post(
14+
'https://qhjte7logh.execute-api.us-east-1.amazonaws.com/production-stage/insights',
15+
{
16+
query: messageContent,
17+
},
18+
{
19+
headers: {
20+
'x-api-key': process.env.INSIGHTS_API_KEY ?? '',
21+
'Content-Type': 'application/json',
22+
},
23+
}
24+
)
25+
26+
const answer = insightsResponse?.data?.[0]?.content ?? ''
27+
28+
if (!answer.length) {
29+
throw new Error('No answer from Insights API')
30+
}
31+
32+
const answerWithFeedback = `${answer}\n\nThis response is AI-generated. Let us know what you think in <#769251416778604562>!`
33+
34+
const embed = new EmbedBuilder()
35+
.setTitle('Artbot AI (Beta)')
36+
.setColor(randomColor())
37+
.setDescription(answerWithFeedback)
38+
39+
return embed
40+
} catch (error) {
41+
console.error('Error getting insights API response:', error)
42+
return new EmbedBuilder()
43+
.setTitle('Error')
44+
.setColor('#FF0000')
45+
.setDescription(
46+
'Sorry, I encountered an error while processing your request. Please try again later.'
47+
)
48+
.setFooter({
49+
text: 'If this persists, please contact the bot administrator.',
50+
})
51+
}
52+
}
53+
}

src/Utils/smartBotResponse.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EmbedBuilder, ColorResolvable, Message } from 'discord.js'
22
import * as dotenv from 'dotenv'
3-
import { artIndexerBot, projectConfig, triviaBot } from '..'
3+
import { artIndexerBot, projectConfig, triviaBot, insightsBot } from '..'
44
dotenv.config()
55
const fetch = require('node-fetch')
66

@@ -162,6 +162,7 @@ const HELP_MESSAGE = new EmbedBuilder()
162162
**staysafe?**: Tips on avoiding scams
163163
**aliases?**: A handy list of aliases that can be used in \`#\` commands.
164164
**hashtag?**: A handy list of all \`#\` functionalities
165+
**artBot**: Ask about Art Blocks artists and projects by tagging @artbot at the start of your question. This AI-powered feature is in beta.
165166
`
166167
)
167168
// Custom message shown when someone asks about ArtBlocks
@@ -308,6 +309,7 @@ export async function smartBotResponse(
308309
}
309310
return null
310311
}
312+
311313
// Some shared helper variables.
312314
const inHelpChannel: boolean = channelID == CHANNEL_HELP
313315
const mentionedArtBot: boolean =
@@ -398,6 +400,11 @@ export async function smartBotResponse(
398400
)
399401
}
400402

403+
if (channelID == CHANNEL_BLOCK_TALK && containsQuestion && mentionedArtBot) {
404+
// Handle messages to the Insights API for AI responses
405+
return insightsBot.getInsightsApiResponse(msg)
406+
}
407+
401408
if (
402409
(channelID == CHANNEL_FOR_SALE_LISTINGS ||
403410
channelID == CHANNEL_TRADE_SWAPS) &&

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
getEngineProjects,
2222
getStudioContracts,
2323
} from './Data/queryGraphQL'
24+
import { InsightsBot } from './Classes/InsightsBot'
2425
import { TriviaBot } from './Classes/TriviaBot'
2526
import {
2627
waitForEngineContracts,
@@ -204,6 +205,8 @@ discordClient.on('disconnect', () => {
204205
})
205206

206207
export const triviaBot = new TriviaBot(discordClient)
208+
export const insightsBot = new InsightsBot()
209+
207210
new ScheduleBot(discordClient.channels.cache, projectConfig)
208211

209212
discordClient.on(Events.MessageCreate, async (msg) => {

0 commit comments

Comments
 (0)