PoshCode Logo PowerShell Code Repository

PowerBot 2.0 (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/728"></script>download | new post

PowerBot is my IRC bot written in PowerShell script using SmartIrc4Net There’s a bit more to it than this, but this is the basic script, and all you have to do is add your own commands! Of course, you could also add your own additional message handlers and make a chatter-bot or whatever you like. Please share your mods back here!

  1. ## PowerBot 2.0
  2. ## A simple framework to get you started writing your own IRC bots in PowerShell
  3. ####################################################################################################
  4. ## Requires Meebey.SmartIrc4net.dll to be in your ...\WindowsPowerShell\Libraries\
  5. ## You can get Meebey.SmartIrc4net.dll from
  6. ## http://voxel.dl.sourceforge.net/sourceforge/smartirc4net/SmartIrc4net-0.4.0.bin.tar.bz2
  7. ####################################################################################################
  8. ## Add-Type -path $ProfileDir\Libraries\Meebey.SmartIrc4net.dll
  9. $null = [Reflection.Assembly]::LoadFrom("$ProfileDir\Libraries\Meebey.SmartIrc4net.dll")
  10.  
  11. function Start-PowerBot {
  12. PARAM( $server = "irc.freenode.net", [string[]]$channels = @("#PowerShell"), [string[]]$nick     = @(Read-Host 'You must provide a nickname'), [string]$password, $realname = "PowerShell Bot", $port               = 6667)
  13.  
  14.    if(!$global:irc) {
  15.       $global:irc = New-Object Meebey.SmartIrc4net.IrcClient
  16.       $irc.ActiveChannelSyncing = $true # $irc will track channels for us
  17.       # $irc.Encoding = [Text.Encoding]::UTF8
  18.       # $irc.Add_OnError( {Write-Error $_.ErrorMessage} )
  19.       $irc.Add_OnQueryMessage( {PrivateMessage} )
  20.       $irc.Add_OnChannelMessage( {ChannelMessage} )
  21.    }
  22.    
  23.    $irc.Connect($server, $port)
  24.    $irc.Login($nick, $realname, 0, $nick, $password)
  25.    ## $channels | % { $irc.RfcJoin( $_ ) }
  26.    foreach($channel in $channels) { $irc.RfcJoin( $channel ) }
  27.    Resume-PowerBot # Shortcut so starting this thing up only takes one command
  28. }
  29.  
  30. ## Note that PowerBot stops listening if you press a key ...
  31. ## You'll have to re-run Resume-Powerbot to get him to listen again
  32. function Resume-PowerBot {
  33.    while(!$Host.UI.RawUI.KeyAvailable) { $irc.ListenOnce($false) }
  34. }
  35.  
  36. function Stop-PowerBot {
  37.    $irc.RfcQuit("If people listened to themselves more often, they would talk less.")
  38.    $irc.Disconnect()
  39. }
  40.  
  41. ####################################################################################################
  42. ## Event Handlers
  43. ####################################################################################################
  44. ## Event handlers in powershell have TWO automatic variables: $This and $_
  45. ##   In the case of SmartIrc4Net:
  46. ##   $This  - usually the connection, and such ...
  47. ##   $_     - the IrcEventArgs, which just has the Data member:
  48. ##
  49.  
  50. function PrivateMessage {
  51.    $Data = $_.Data
  52.    # Write-Verbose $Data.From  
  53.    # Write-Verbose $Data.Message
  54.    # Write-Verbose $($Data | Out-String)
  55.    
  56.    $command, $params = $Data.MessageArray
  57.    if($PowerBotCommands.ContainsKey($command)) {
  58.       &$PowerBotCommands[$command] $params $Data |  Out-String -width (510 - $Data.From.Length - $nick.Length - 3) | % { $_.Trim().Split("`n") | %{ $irc.SendMessage("Message", $Data.Channel, $_.Trim() ) }}
  59.    }
  60. }
  61.  
  62. function ChannelMessage {
  63.    $Data = $_.Data
  64.    # Write-Verbose $Data.From
  65.    # Write-Verbose $Data.Channel
  66.    # Write-Verbose $Data.Message
  67.    # Write-Verbose $($Data | Out-String)
  68.    
  69.    $command, $params = $Data.MessageArray
  70.    if($PowerBotCommands.ContainsKey($command)) {
  71.       &$PowerBotCommands[$command] $params $Data | Out-String -width (510 - $Data.Channel.Length - $nick.Length - 3) | % { $_.Trim().Split("`n") | %{ $irc.SendMessage("Message", $Data.Channel, $_.Trim() ) }}
  72.    }
  73. }
  74.  
  75. ####################################################################################################
  76. ## The PowerBotCommands hashtable is extremely simple ...
  77. ##
  78. ## You register a "command" which must be the FIRST WORD of a message (either private, or channel)
  79. ##   and you provide a scriptblock to process said message.  
  80. ## The handler scriptblock gets two parameters, for convenience:
  81. ##   $Params is the rest of the message text after the command word (which is probably all you need)
  82. ##   $Data is the Data propert of the IrcEventArgs, which has everything in it that you could want
  83. ##
  84. ## You may do whatever you want in the scriptblock (this runs on your PC, after all), but the
  85. ##   simplest thing is to respond by returning "something" which will be sent to wherever the message
  86. ##   came from.  
  87. ##
  88. ## NOTE 1: Evrything you return is output to Out-String and then the channel or user.  Be careful!
  89. ## NOTE 2: 510 is the Max IRC Message Length, including the channel name etc.
  90. ##         http`://www.faqs.org/rfcs/rfc1459.html
  91. ##
  92. $PowerBotCommands=@{}
  93.  
  94. ## A sample command to get you started
  95. $PowerBotCommands."Hello" = {Param($Query,$Data)
  96.    "Hello, $($Data.Nick)!"
  97. }
  98.  
  99. ##$PowerBotCommands."!Echo" = {Param($Query,$Data)
  100. ##   "$Query"
  101. ##}
  102. ##
  103.  
  104. ##$PowerBotCommands."!Get-Help" = {Param($Query)
  105. ##   $help = get-help $Query | Select Name,Synopsis,Syntax
  106. ##   if($?) {
  107. ##      if($help -is [array]) {
  108. ##         "You're going to need to be more specific, I know all about $((($help | % { $_.Name })[0..($help.Length-2)] -join ', ') + ' and even ' + $help[-1].Name)"
  109. ##      } else {
  110. ##         @($help.Synopsis,($help.Syntax | Out-String -width 1000).Trim().Split("`n",4,"RemoveEmptyEntries")[0..3])
  111. ##      }
  112. ##   } else {
  113. ##      "I couldn't find the help file for '$Query', sorry.  I probably don't have that snapin loaded."
  114. ##   }
  115. ##}

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me