Ruby: เขียน IRC bot ด้วย Ruby 10 บรรทัด


Posted in:

IRC bot สั้นๆครับประมาณ 10 บรรทัด code ภาษา Ruby ง่ายๆ

#!/usr/bin/ruby
require 'socket'
TCPSocket.open("irc.oftc.net", 6667) do |sock|
    sock.print("USER Shimokita Shimokita Shimokita :Shimokita Gloryday\r\n")
    sock.print("Nick Shimokita\r\n")
    sock.print("JOIN #aaabbbccc\r\n")
    while !sock.closed?
        line = sock.readline.chomp
        sock.print("PRIVMSG #aaabbbccc :ฉันรักเธอ ฉันรักเธอ ฉันรักเธอ\n") if line =~ /Shimokita, คิดถึงจัง/
    end
end

บันทึกลงไฟล์ด้วย Encoding แบบ TIS-620 นะครับ จะได้คุยกันเข้าใจ เปิด terminal ขึ้นมาแล้วรัน

$ruby Shimokita.rb

ผลการทดลอง

ขำๆครับ ลองเอาไปเล่นกัน :D
นับดูอีกที 11 บรรทัด -_-'

เพิ่มเติม 23:48 05/26/2007
ท่าน chaow ขอแบบ Python มา
จัดให้ครับ (ไปจัดย่อหน้าเองนะ ไม่งั้น Error)

#!/usr/bin/python
import socket
sock=socket.socket()
sock.connect(("irc.oftc.net", 6667))
sock.send("NICK Shimokita\r\n")
sock.send("USER Shimokita Shimokita Shimokita :Shimokita Gloryday\r\n")
sock.send("JOIN #aaabbbccc\r\n")
while True:
    line = sock.recv (4096)
    if line.find ( 'Shimokita, I miss you.' ) != -1:
        sock.send("PRIVMSG #aaabbbccc :I love you. I love you. I love you.\n")
sock.close()

the DtTvB

แจ๋วครับ

ป.ล. ใช้ Ruby ไม่เป็น

มีแบบ python

มีแบบ python ไหมครับ

เห็น code

เห็น code แล้วน่าสนุกแฮะ :P

4096

ใน sock.recv()
คืออะไรเหรอคะ

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <i> <center> <span> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <br> <dt> <dd> <object> <embed> <hr> <img> <blockquote> <b> <u> <del> <div> <p> <table> <tr> <td> <font>

More information about formatting options