Personal tools

Talk:Roll your own IRC bot

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(question about forkIO stuff)
 
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
 
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?
 
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?
 
--[[User:Gog|Gog]] 19:09, 18 January 2007 (UTC)
 
--[[User:Gog|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.
  +
  +
This works for some things but I'm not sure what'd be the cleanest way to get information back to the main thread from the forkIO'd commands.
  +
  +
--[[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)
  +
  +
I added this after the `connectTo`:
  +
<pre>forkIO (forever (getLine >>= hPrintf h "%s\r\n"))</pre>
  +
It isn't the prettiest, but it was enough to get me going and take any input and send it to the server. --[[User:Nullren|Nullren]] 17:15, 15 August 2012 (UTC)

Latest revision as of 17:15, 15 August 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.

This works for some things but I'm not sure what'd be the cleanest way to get information back to the main thread from the forkIO'd commands.

--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)

I added this after the `connectTo`:

forkIO (forever (getLine >>= hPrintf h "%s\r\n"))

It isn't the prettiest, but it was enough to get me going and take any input and send it to the server. --Nullren 17:15, 15 August 2012 (UTC)