Skip to content

Instantly share code, notes, and snippets.

@tetiana12345678
Last active December 23, 2021 09:28
Show Gist options
  • Save tetiana12345678/def2d4043c9ddc9212ff5f4268e702da to your computer and use it in GitHub Desktop.
Save tetiana12345678/def2d4043c9ddc9212ff5f4268e702da to your computer and use it in GitHub Desktop.
Building simple chat bot with Elixir (part 2 of building a chat app with Phoenix Channels)

This is part of building a chat app with Phoenix Channels workshop. In this part we will use Elixir to build a chat bot.

Create a new Bot application

Remember in part 1 we generated root for the umbrella application? Generate new application for chat bot:

$ cd chatter/apps
$ mix new bot

Using umbrella applications provided by Elixir we can desing for deletability, one of the great ideas Greg Young was talking about. Let's stop here for a second and think about boundaries, microservices and how to get it right.

Now we are ready to move on to a bot implementation. Let's write a "code I wish I had" in our ChatterWeb application to identify our interface and then TDD the implementation.

In chatter/apps/chatter_web/lib/chatter_web/channels/room_channel.ex

def handle_in("new_message", payload, socket) do
  IO.inspect payload  # IO.inspect will print out to a console payload, which we receive
  # Bot.answer(payload) # The code I wish I had
  broadcast! socket, "new_message", payload
  {:noreply, socket}
end

TDDing Chat Bot saying hello to the user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment