nextjs-assistant.mdx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Fork the Embedchain repo on [Github](https://github.com/embedchain/embedchain) to create your own NextJS discord and slack bot powered by Embedchain.
  2. If you run into problems with forking, please refer to [github docs](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo) for forking a repo.
  3. We will work from the `examples/nextjs` folder so change your current working directory by running the command - `cd <your_forked_repo>/examples/nextjs`
  4. # Installation
  5. First, lets start by install all the required packages and dependencies.
  6. - Install all the required python packages by running ```pip install -r requirements.txt```
  7. - We will use [Fly.io](https://fly.io/) to deploy our embedchain app, discord and slack bot. Follow the step one to install [Fly.io CLI](https://docs.embedchain.ai/deployment/fly_io#step-1-install-flyctl-command-line)
  8. # Developement
  9. ## Embedchain App
  10. First, we need an Embedchain app powered with the knowledge of NextJS. We have already created an embedchain app using FastAPI in `ec_app` folder for you. Feel free to ingest data of your choice to power the App.
  11. <Note>
  12. Navigate to `ec_app` folder and create `.env` file in this folder and set your OpenAI API key as shown in `.env.example` file. If you want to use other open-source models, feel free to use the app config in `app.py`. More details for using custom configuration for Embedchain app is [available here](https://docs.embedchain.ai/api-reference/advanced/configuration).
  13. </Note>
  14. Before running the ec commands to develope the app, open `fly.toml` file and update the `name` variable to something unique. This is important as `fly.io` requires users to provide a globally unique deployment app names.
  15. Now, we need to launch this application with fly.io. You can see your app on [fly.io dashboard](https://fly.io/dashboard). Run the following command to launch your app on fly.io:
  16. ```bash
  17. fly launch --no-deploy
  18. ```
  19. To run the app in development, run the following command:
  20. ```bash
  21. ec dev
  22. ```
  23. Run `ec deploy` to deploy your app on Fly.io. Once you deploy your app, save the endpoint on which our discord and slack bot will send requests.
  24. ## Discord bot
  25. For discord bot, you will need to create the bot on discord developer portal and get the discord bot token and your discord bot name.
  26. While keeping in mind the following note, create the discord bot by following the instructions from our [discord bot docs](https://docs.embedchain.ai/examples/discord_bot) and get discord bot token.
  27. <Note>
  28. You do not need to set `OPENAI_API_KEY` to run this discord bot. Follow the remaining instructions to create a discord bot app. We recommend you to give the following sets of bot permissions to run the discord bot without errors:
  29. ```
  30. (General Permissions)
  31. Read Message/View Channels
  32. (Text Permissions)
  33. Send Messages
  34. Create Public Thread
  35. Create Private Thread
  36. Send Messages in Thread
  37. Manage Threads
  38. Embed Links
  39. Read Message History
  40. ```
  41. </Note>
  42. Once you have your discord bot token and discord app name. Navigate to `nextjs_discord` folder and create `.env` file and define your discord bot token, discord bot name and endpoint of your embedchain app as shown in `.env.example` file.
  43. To run the app in development:
  44. ```bash
  45. python app.py
  46. ```
  47. Before deploying the app, open `fly.toml` file and update the `name` variable to something unique. This is important as `fly.io` requires users to provide a globally unique deployment app names.
  48. Now, we need to launch this application with fly.io. You can see your app on [fly.io dashboard](https://fly.io/dashboard). Run the following command to launch your app on fly.io:
  49. ```bash
  50. fly launch --no-deploy
  51. ```
  52. Run `ec deploy` to deploy your app on Fly.io. Once you deploy your app, your discord bot will be live!
  53. ## Slack bot
  54. For Slack bot, you will need to create the bot on slack developer portal and get the slack bot token and slack app token.
  55. ### Setup
  56. - Create a workspace on Slack if you don't have one already by clicking [here](https://slack.com/intl/en-in/).
  57. - Create a new App on your Slack account by going [here](https://api.slack.com/apps).
  58. - Select `From Scratch`, then enter the Bot Name and select your workspace.
  59. - Go to `App Credentials` section on the `Basic Information` tab from the left sidebar, create your app token and save it in your `.env` file as `SLACK_APP_TOKEN`.
  60. - Go to `Socket Mode` tab from the left sidebar and enable the socket mode to listen to slack message from your workspace.
  61. - (Optional) Under the `App Home` tab you can change your App display name and default name.
  62. - Navigate to `Event Subscription` tab, and enable the event subscription so that we can listen to slack events.
  63. - Once you enable the event subscription, you will need to subscribe to bot events to authorize the bot to listen to app mention events of the bot. Do that by tapping on `Add Bot User Event` button and select `app_mention`.
  64. - On the left Sidebar, go to `OAuth and Permissions` and add the following scopes under `Bot Token Scopes`:
  65. ```text
  66. app_mentions:read
  67. channels:history
  68. channels:read
  69. chat:write
  70. emoji:read
  71. reactions:write
  72. reactions:read
  73. ```
  74. - Now select the option `Install to Workspace` and after it's done, copy the `Bot User OAuth Token` and set it in your `.env` file as `SLACK_BOT_TOKEN`.
  75. Once you have your slack bot token and slack app token. Navigate to `nextjs_slack` folder and create `.env` file and define your slack bot token, slack app token and endpoint of your embedchain app as shown in `.env.example` file.
  76. To run the app in development:
  77. ```bash
  78. python app.py
  79. ```
  80. Before deploying the app, open `fly.toml` file and update the `name` variable to something unique. This is important as `fly.io` requires users to provide a globally unique deployment app names.
  81. Now, we need to launch this application with fly.io. You can see your app on [fly.io dashboard](https://fly.io/dashboard). Run the following command to launch your app on fly.io:
  82. ```bash
  83. fly launch --no-deploy
  84. ```
  85. Run `ec deploy` to deploy your app on Fly.io. Once you deploy your app, your slack bot will be live!