Author Topic: IRC Chat Bot demo/beginning  (Read 3810 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
IRC Chat Bot demo/beginning
« on: August 14, 2019, 02:18:13 am »
Code: QB64: [Select]
  1. TITLE "Steve QB64-IRC Bot"
  2.  
  3. DIM SHARED Client AS LONG, Server AS STRING, Channel AS STRING
  4. crlf$ = CHR$(13) + CHR$(10)
  5. nick$ = "SqbBot"
  6. pass$ = ""
  7. Server = "irc.freenode.net"
  8. Channel = "#qb64"
  9.  
  10. PRINT "Connecting to " + Server + "..."
  11. Client = _OPENCLIENT("TCP/IP:6667:" + Server)
  12. IF Client& = 0 THEN PRINT "Error: could not connect...": SLEEP: SYSTEM
  13. IF pass$ > "" THEN SendInfo "PASS" + pass$
  14. SendInfo "NICK " + nick$
  15. SendInfo "USER " + nick$ + " 0 * :" + nick$
  16. PRINT "Connected!"
  17.  
  18. SendInfo "JOIN " + Channel
  19. SendInfo "TOPIC " + Channel
  20. PRINT "Joined "; Channel
  21.  
  22. respond = 0
  23.     _LIMIT 1000
  24.     GET #Client&, , In$
  25.     IF LEFT$(In$, 4) = "PING" THEN
  26.         'Respond with PONG
  27.         res$ = "PONG" + MID$(In$, 5) + CHR$(13) + CHR$(10)
  28.         PUT #Client, , res$
  29.     END IF
  30.  
  31.     'IF In$ <> "" THEN PRINT LEFT$(In$, LEN(In$) - 2) 'Unremark this is we want to see what's being typed by everyone.
  32.     IF In$ <> "" AND respond THEN ProcessInput In$
  33.     IF INSTR(In$, "End of /NAMES list.") THEN respond = -1 'Don't start responding to the automatic server messages, like an idiot bot!
  34. LOOP UNTIL INKEY$ = CHR$(32) 'Spacebar to quit
  35.  
  36.  
  37. SUB SendInfo (text$)
  38. text$ = text$ + CHR$(13) + CHR$(10)
  39. PUT #Client&, , text$
  40.  
  41. SUB SendReply (text$)
  42. text$ = "PRIVMSG " + Channel$ + " :" + text$ + CHR$(13) + CHR$(10)
  43. PUT #Client&, , text$
  44. COLOR 14: PRINT text$
  45.  
  46. SUB ProcessInput (text$)
  47.  
  48. Speaker$ = MID$(text$, 2, INSTR(text$, "!") - 2)
  49. c$ = UCASE$(Channel) + " :"
  50. In$ = UCASE$(LEFT$(text$, LEN(text$) - 2)) + " " ' Strip off the CRLF
  51. eval$ = " " + MID$(In$, INSTR(In$, c$) + LEN(c$)) + " "
  52.  
  53. IF INSTR(eval$, " SQBBOT ") THEN
  54.     'someone is talking directly to the bot or giving it a command
  55.     IF INSTR(eval$, " QUIT ") THEN SYSTEM 'A means to automatically shut down the bot
  56.     IF INSTR(eval$, " FINISH ") THEN SYSTEM 'A means to automatically shut down the bot
  57.     IF INSTR(eval$, " DIE ") THEN SYSTEM 'A means to automatically shut down the bot
  58.     IF INSTR(eval$, " SHUT DOWN ") THEN SYSTEM 'A means to automatically shut down the bot
  59.     IF INSTR(eval$, " EXIT ") THEN SYSTEM 'A means to automatically shut down the bot
  60.     IF INSTR(eval$, " END ") THEN SYSTEM 'A means to automatically shut down the bot
  61.     IF INSTR(eval$, " TELL") THEN
  62.         IF INSTR(eval$, "TIME") THEN Out$ = Out$ + "The TIME is " + TIME$ + ".  "
  63.         IF INSTR(eval$, "DATE") THEN Out$ = Out$ + "The DATE is " + DATE$ + ".  "
  64.     END IF
  65.  
  66.     IF INSTR(eval$, " HI ") THEN Out$ = "Hiyas, " + Speaker$ + ".  "
  67.     IF INSTR(eval$, " HELLO ") THEN Out$ = "Hello to you too, " + Speaker$ + ".  "
  68.     IF INSTR(eval$, " YO ") THEN Out$ = "Hola!  " + Speaker$ + "  How's it hanging?  "
  69.     IF INSTR(eval$, " HOLA ") THEN Out$ = "What's happening, " + Speaker$ + "?  "
  70.  
  71. IF INSTR(In$, " JOIN ") AND (INSTR(eval$, "JOIN") = 0) THEN Out$ = "Welcome to QB64 Chat, " + Speaker$ + ".  "
  72.  
  73. IF Out$ <> "" THEN
  74.     COLOR 15
  75.     l = INSTR(In$, "PRIVMSG")
  76.     PRINT Speaker$; " on "; MID$(In$, l + 8) 'I put a print here, so we can see what our bot is responding to, no matter what.
  77.     SendReply Out$

Another program from the depths of my hard drive — the start of an IRC chat bot.  I’m certain many of the old regulars will remember this guy popping up over and over in the IRC channel as folks tested it and then used it as a base to build their own custom bot, but we have a lot of new members around now, and I thought they too deserved the chance to drive us old QB64 chat channel lurkers crazy...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: IRC Chat Bot demo/beginning
« Reply #1 on: August 14, 2019, 12:07:55 pm »
Awesome! One time I saw someone create an ALICE chat bot for IRC, decades ago. If anyone is interested in making one, there might be info online somewhere.
« Last Edit: August 14, 2019, 12:10:10 pm by SierraKen »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: IRC Chat Bot demo/beginning
« Reply #2 on: August 15, 2019, 10:00:40 am »
Lemme help reinforce Steve's point:

Real Qb64 coders hang out at in the #qb64 channel on irc.freenode.net. (I guess the yuppies use discord but I dont see the attraction.)

Steve's code has the ability to connect to the #qb64 room on IRC. A new name will pop up in the user list, and then you have 100% freedom to control what the bot does (minus heavy spam).

I used that ^ code to learn irc protocols and such. Twas pretty useful, so I think you all should try it. Dont be intimidated - Qb64 does all kinds of online stuff.
You're not done when it works, you're done when it's right.