How I built my portfolio as a chatbot using Next.js
Tags: react, nextjs, javascript, webdev
Published: 2022-03-31 10:08:09

Many developer portfolios out there are boring, so are my previous ones. I think a new portfolio as a chat bot would be interesting, as it creates interactive user experience and adds more fun by talking to "me". The website is [live](https://jeffreyyu.dev/) and I'd to share how I built it using Next.js.

## react-chatbot-kit
I used [react-chatbot-kit](https://www.npmjs.com/package/react-chatbot-kit) to implement chatbot messages and follow-up options. I followed this [tutorial](https://www.freecodecamp.org/news/how-to-build-a-chatbot-with-react/) to set it up.

In react-chatbot-kit, `config` configures settings like initial messages and widgets (custom components that come with a message). `ActionProvider` creates bot messages with widgets. `MessageParser` parses user messages and calls certain actions in ActionProvider.

```javascript
// index.js
import Chatbot from 'react-chatbot-kit';
...
<Chatbot
  config={config}
  actionProvider={ActionProvider}
  messageParser={MessageParser}
/>
```

## Greetings & Jokes

First, the chatbot greets and asks how's the user doing today. The good mood option leads to my portfolio sections and the bad mood one leads to a joke.

![joke message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bf0yltbxzy7ztwlbqmul.gif)

```javascript
// config.js

const getMoodOptions = (actionProvider) => {
  return [
    {
      text: 'Doing great! Tell me about yourself',
      handler: () => actionProvider.handleGoodMood(),
      id: 1,
    },
    {
      text: 'Having a bad day...',
      handler: () => actionProvider.handleBadMood(),
      id: 2,
    },
  ];
};

const config = {
  initialMessages: [
    createChatBotMessage(
      "Hi, I'm Jeffrey. Nice to meet you! I How are you doing today?",
      { widget: 'moodOptions' }
    ),
  ],
widgets: [
    {
      widgetName: 'moodOptions',
      widgetFunc: ({ actionProvider }) => (
        <Options actionProvider={actionProvider} getOptions={getMoodOptions} />
      ),
    },
  ]
}
```

```javascript
// ActionProvider.js

class ActionProvider {
  constructor(createChatBotMessage, setStateFunc) {
    this.createChatBotMessage = createChatBotMessage;
    this.setState = setStateFunc;
  }

  handleGoodMood() {
    const message = this.createChatBotMessage(intro, {
      widget: 'personalOptions',
    });
    this.updateChatbotState(message);
  }

  async handleBadMood() {
    const jokeData = await (
      await fetch('https://v2.jokeapi.dev/joke/Any?type=single')
    ).json();
    const message = this.createChatBotMessage(
      `Let me tell you a joke: ${jokeData.joke}`,
      {
        widget: 'jokeOptions',
      }
    );
    this.updateChatbotState(message);
  }
}
```

## Portfolio sections

After the user is entertained by jokes, he or she can choose to see my experience, projects, skills, or blogs. I built widget cards for each section and add them to config and ActionProvider like the previous greeting.

![portfolio sections message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6r1xzqp53xjhk7bahtc.gif)

I add the section cards in the sidebar so that the user can browse my portfolio even without using the chatbot. I'm also adding AI response to user message add more fun to the website :) 

You can check out the code for this website [here](https://github.com/JeffreytheCoder/chat-portfolio).