To implement an IRC bot with gen_ircclient
the following four callback functions need to be implemented:
init/1
initializes the bot's statehandle_privmsg/4
defines the reaction to a given message.handle_join/2
defines the reaction to the joining of a new user.handle_part/2
handles the leaving of a user.
-callback init( Arg :: _ ) -> State :: _.
-callback handle_privmsg( Mode :: private | public, Sender :: string(), Content :: string(), State :: _ ) ->
{noreply, NewState :: _}
| {reply, Reply :: string(), NewState :: _}
| {spawn, F :: fun( () -> string() ), NewState :: _}.
-callback handle_join( User :: string(), State :: _ ) -> _.
-callback handle_part( User :: string(), State :: _ ) -> _.
-module( example_bot ).
-export( [init/1, handle_privmsg/4, handle_join/2, handle_part/2] ).
init( _Arg ) ->
[].
handle_privmsg( public, _Sender, "hello", State ) ->
{reply, "Hi there.", State};
handle_privmsg( private, _Sender, "hello", State ) ->
{reply, "Psst!", State};
handle_privmsg( _, _, _, State ) ->
{noreply, State}.
handle_join( _User, State ) ->
State.
handle_part( _User, State ) ->
State.
Server = "irc.freenode.net",
Port = 6667,
NickName = "adam0815",
UserName = "adam",
RealName = "Adam Canopy".
ConnInfo = {conn_info, Server, Port, NickName, UserName, RealName},
Channel = "#botwar",
UsrMod = example_bot.
gen_ircclient:start_link( ConnInfo, Channel, UsrMod, [] ).