- Notifications
You must be signed in to change notification settings - Fork6
hedwig-im/hedwig_xmpp
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An XMPP Adapter forHedwig
Let's generate a new Elixir application with a supervision tree:
λ mix new alfred --sup* creating README.md* creating .gitignore* creating mix.exs* creating config* creating config/config.exs* creating lib* creating lib/alfred.ex* creating test* creating test/test_helper.exs* creating test/alfred_test.exsYour Mix project was created successfully.You can use "mix" to compile it, test it, and more: cd alfred mix testRun "mix help" for more commands.
Change into our new application directory:
λ cd alfred
Addhedwig_xmpp
to your list of dependencies inmix.exs
:
defdepsdo[{:hedwig_xmpp,"~> 1.0"}]end
Ensurehedwig_xmpp
is started before your application:
defapplicationdo[applications:[:hedwig_xmpp]]end
λ mix hedwig.gen.robotWelcome to the Hedwig Robot Generator!Let's get started.What would you like to name your bot?: alfredAvailable adapters1. Hedwig.Adapters.XMPP2. Hedwig.Adapters.Console3. Hedwig.Adapters.TestPlease select an adapter: 1* creating lib/alfred* creating lib/alfred/robot.ex* updating config/config.exsDon't forget to add your new robot to your supervision tree(typically in lib/alfred.ex): worker(Alfred.Robot, [])
We'll want Alfred to be supervised and started when we start our application.Let's add it to our supervision tree. Open uplib/alfred.ex
and add thefollowing to thechildren
list:
worker(Alfred.Robot,[])
The next thing we need to do is configure our bot for our XMPP server. Open upconfig/config.exs
and let's take a look at what was generated for us:
useMix.Configconfig:alfred,Alfred.Robot,adapter:Hedwig.Adapters.XMPP,name:"alfred",aka:"/",responders:[{Hedwig.Responders.Help,[]},{Hedwig.Responders.Ping,[]}]
So we have theadapter
,name
,aka
, andresponders
set. Theadapter
isthe module responsible for handling all of the XMPP details like connecting andsending and receiving messages over the network. Thename
is the name that ourbot will respond to. Theaka
(also known as) field is optional, but it allowsus to address our bot with an alias. By default, this alias is set to/
.
Finally we haveresponders
. Responders are modules that provide functions thatmatch on the messages that get sent to our bot. We'll discuss this further ina bit.
We'll need to provide a few more things in order for us to connect to our XMPPserver. We'll need to provide our bot'sjid
andpassword
as well as a listof rooms we want our bot to join once connected. Let's see what that looks like:
useMix.Configconfig:alfred,Alfred.Robot,adapter:Hedwig.Adapters.XMPP,name:"alfred",aka:"/",# fill in the appropriate jid for your botjid:"alfred@localhost",# fill in the appropriate password for your botpassword:"password",rooms:[# fill in the appropriate rooms for your XMPP server{"lobby@conference.localhost",[]}],responders:[{Hedwig.Responders.Help,[]},{Hedwig.Responders.Ping,[]}]
Great! We're ready to start our bot. From the root of our application, let's runthe following:
λ mix run --no-halt
This will start our application along with our bot. Our bot should connect tothe server and join the configured room(s). From there, we can connect with ourfavourite XMPP client and begin sending messages to our bot.
Since we have theHelp
responder installed, we can sayalfred help
and weshould see a list of usage for all of the installed responders.
Well, that's it for now. Make sure to read theHedwig Documentation for moredetails on writing responders and other exciting things!
Copyright (c) 2015, Sonny Scroggin.
Hedwig XMPP source code is licensed under theMIT License.
About
XMPP Adapter for Hedwig