Difference between revisions of "Talk:Roll your own IRC bot"
Line 5: | Line 5: | ||
− | privmsgIO :: Handle -> String -> IO () |
+ | privmsgIO :: Handle -> String -> IO () |
− | privmsgIO h s = |
+ | privmsgIO h s = |
− | writeIO h "PRIVMSG" (botChan ++ " :" ++ s) |
+ | writeIO h "PRIVMSG" (botChan ++ " :" ++ s) |
− | -- Send a privmsg to the current chan + server |
+ | -- Send a privmsg to the current chan + server |
− | privmsg :: String -> IrcBot () |
+ | privmsg :: String -> IrcBot () |
− | privmsg s = do |
+ | privmsg s = do |
− | h <- asks socket |
+ | h <- asks socket |
− | liftIO $ privmsgIO h s |
+ | liftIO $ privmsgIO h s |
then when I need to forkIO, I do |
then when I need to forkIO, I do |
||
− | foo = do |
+ | foo = do |
− | h <- asks socket |
+ | h <- asks socket |
− | liftIO $ forkIO (privmsg h ...) |
+ | liftIO $ forkIO (privmsg h ...) |
and use the IO variant of privmsg. |
and use the IO variant of privmsg. |
||
--[[User:Nurpax|Nurpax]] 16:20, 8 April 2012 (UTC) |
--[[User:Nurpax|Nurpax]] 16:20, 8 April 2012 (UTC) |
||
+ | |||
+ | Btw, I would highly recommend modifying the <code>connect</code> to set the socket encoding to UTF-8: |
||
+ | |||
+ | <pre> |
||
+ | -- Connect to the server and return the initial bot state |
||
+ | connect :: IO IrcBotRO |
||
+ | connect = notify $ do |
||
+ | h <- connectTo server (PortNumber port) |
||
+ | hSetEncoding h utf8 |
||
+ | </pre> |
||
+ | |||
+ | It took me a while to debug why my UTF8 output was all messed up. |
||
+ | --[[User:Nurpax|Nurpax]] 16:21, 8 April 2012 (UTC) |
Revision as of 16:21, 8 April 2012
Thanks for this useful tutorial! I implemented the normal chat functionality using forkIO as suggested but I'm not happy with my solution. Where would you put the call to forkIO? And how would you write the function that you pass it? Is it possible to somehow use privmsg in this function or do I have to duplicate that code more or less? --Gog 19:09, 18 January 2007 (UTC)
I had the same problem. You can't really forkIO directly from inside the Reader monad, even with liftIO. I ended up with this but maybe there is a better way:
privmsgIO :: Handle -> String -> IO () privmsgIO h s = writeIO h "PRIVMSG" (botChan ++ " :" ++ s)
-- Send a privmsg to the current chan + server privmsg :: String -> IrcBot () privmsg s = do h <- asks socket liftIO $ privmsgIO h s
then when I need to forkIO, I do
foo = do h <- asks socket liftIO $ forkIO (privmsg h ...)
and use the IO variant of privmsg.
--Nurpax 16:20, 8 April 2012 (UTC)
Btw, I would highly recommend modifying the connect
to set the socket encoding to UTF-8:
-- Connect to the server and return the initial bot state connect :: IO IrcBotRO connect = notify $ do h <- connectTo server (PortNumber port) hSetEncoding h utf8
It took me a while to debug why my UTF8 output was all messed up. --Nurpax 16:21, 8 April 2012 (UTC)