WhatsApp Web - Chat Bot

A chat bot for WhatsApp Web, with some basic commands. Check console for log.

  1. // ==UserScript==
  2. // @name WhatsApp Web - Chat Bot
  3. // @namespace WACB
  4. // @version 0.1
  5. // @description A chat bot for WhatsApp Web, with some basic commands. Check console for log.
  6. // @author Royalgamer06
  7. // @match https://web.whatsapp.com/
  8. // @grant GM_xmlhttpRequest
  9. // @grant unsafeWindow
  10. // @run-at document-idle
  11. // ==/UserScript==
  12.  
  13. var jq = document.createElement('script');
  14. jq.onload = function() {
  15. jQuery.noConflict();
  16. console.log('jQuery loaded');
  17. setTimeout(Main, 3500);
  18. };
  19. jq.src = "//ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js";
  20. document.getElementsByTagName('head')[0].appendChild(jq);
  21.  
  22. function Main() {
  23. console.log("[WACB] Waiting for chat to load");
  24. jQuery("#pane-side").on("click", function() {
  25. setTimeout(listenToChat, 350);
  26. });
  27. }
  28.  
  29. function listenToChat() {
  30. console.log("[WACB] Listening to chat");
  31. /*jQuery(".message-list").bind("DOMSubtreeModified", function() {
  32. var new_msg = jQuery(".selectable-text").last().text();
  33. console.log("[WACB] New chat message: \n" + new_msg);
  34. if (new_msg.indexOf("!") === 0) {
  35. var cmd_line = new_msg.substring(1);
  36. var cmd = cmd_line.split(" ")[0];
  37. var args = cmd_line.split(" ").shift();
  38. if (cmd == "about") {
  39. sendMsg("I am a chat bot, made by Roy van Dijk.");
  40. }
  41. }
  42. });*/
  43. unsafeWindow.sendMsg = function(msg) {
  44. console.log("[WACB] Sending message: \n" + msg);
  45. var target = document.getElementsByClassName("input")[1];
  46. var eventType = "textInput";
  47. var evt = document.createEvent("TextEvent");
  48. evt.initTextEvent(eventType, true, true, unsafeWindow, msg, 0, "en-US");
  49. target.focus();
  50. target.dispatchEvent(evt);
  51. jQuery(".send-container").click();
  52. };
  53. var last_msg = jQuery(".selectable-text").last().text();
  54. setInterval(function() {
  55. var new_msg = jQuery(".selectable-text").last().text();
  56. if (new_msg !== last_msg) {
  57. console.log("[WACB] New chat message: \n" + new_msg);
  58. last_msg = new_msg;
  59. if (new_msg.indexOf("!") === 0) {
  60. var cmd_line = new_msg.substring(1);
  61. var cmd = cmd_line.split(" ")[0];
  62. var args = cmd_line.split(" ").slice(1);
  63. if (cmd == "help") {
  64. sendMsg("I can do the following commands:");
  65. sendMsg("*!about*: Returns who I am.");
  66. sendMsg("*!joke*: Returns a random joke (about Chuck Norris).");
  67. sendMsg("*!weather*: Returns the current weather in Eindhoven.");
  68. sendMsg("*!weather <LOCATION>*: Returns the current weather in <LOCATION>.");
  69. sendMsg("*!gewis*: Returns the agenda of GEWIS.");
  70. sendMsg("(Work in progress)");
  71. }
  72. if (cmd == "about") {
  73. sendMsg("I am a chat bot.");
  74. }
  75. if (cmd == "joke") {
  76. GM_xmlhttpRequest({
  77. method: "GET",
  78. url: "http://api.icndb.com/jokes/random?escape=javascript",
  79. onload: function(response) {
  80. var json = JSON.parse(response.responseText);
  81. sendMsg(json.value.joke);
  82. }
  83. });
  84. }
  85. if (cmd == "weather") {
  86. var url = "http://api.apixu.com/v1/current.json?key=d0c5d252848043d6af4210418162706&q=Eindhoven";
  87. if (args.length > 0) {
  88. url = "http://api.apixu.com/v1/current.json?key=d0c5d252848043d6af4210418162706&q=" + args[0];
  89. }
  90. GM_xmlhttpRequest({
  91. method: "GET",
  92. url: url,
  93. onload: function(response) {
  94. var json = JSON.parse(response.responseText);
  95. if (args.length > 0) {
  96. if (json.error) {
  97. sendMsg("ERROR: Couldn't find location.");
  98. } else {
  99. sendMsg("It is currently " + json.current.temp_c + "°C in " + args[0]);
  100. }
  101. } else {
  102. sendMsg("It is currently " + json.current.temp_c + "°C in Eindhoven.");
  103. }
  104. }
  105. });
  106. }
  107. if (cmd == "gewis") {
  108. GM_xmlhttpRequest({
  109. method: "GET",
  110. url: "https://www.gewis.nl/activity",
  111. onload: function(response) {
  112. var acts = jQuery(".agenda-item-body", response.responseText);
  113. for (var i = 0; i < acts.length; i++) {
  114. sendMsg(jQuery("h4 > a", acts[i]).text().trim() + " - " + jQuery("div.col-md-4 > dl > dd:nth-child(2)", acts[i]).text().trim() + " @ " + jQuery("div.col-md-4 > dl > dd:nth-child(6)", acts[i]).text().trim());
  115. }
  116. }
  117. });
  118. }
  119. }
  120. }
  121. }, 100);
  122. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址