PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/messengerbot/services/local_services/HelloWorldService.py

https://gitlab.com/namboy94/messengerbot
Python | 204 lines | 162 code | 7 blank | 35 comment | 0 complexity | ac0667a1d5fac3556cda8531b66d7b8f MD5 | raw file
  1. # coding=utf-8
  2. """
  3. Copyright 2015,2016 Hermann Krumrey
  4. This file is part of messengerbot.
  5. messengerbot makes use of various third-party python modules to serve
  6. information via the online chat services.
  7. messengerbot is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. messengerbot is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with messengerbot. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. # imports
  19. import re
  20. from messengerbot.servicehandlers.Service import Service
  21. from messengerbot.connection.generic.Message import Message
  22. class HelloWorldService(Service):
  23. """
  24. The HelloWorldService Class that extends the generic Service class.
  25. The service parses www.kicktipp.de to get a kicktipp group's current standings
  26. """
  27. identifier = "hello_world"
  28. """
  29. The identifier for this service
  30. """
  31. help_description = {"en": "/helloworld\tSends a message containing how to write a 'Hello World'"
  32. "Program in a specific language\n"
  33. "syntax:\n"
  34. "/helloworld <language|list>",
  35. "de": "/helloworld\tSchickt eine Nachricht mit einem 'Hello World' Codeschnipsel für eine"
  36. "spezifische Programmiersprache\n"
  37. "syntax:\n"
  38. "/helloworld <sprache|liste>"}
  39. """
  40. Help description for this service.
  41. """
  42. implementations = {"python": "print(\"Hello World!\")",
  43. "python2": "print \"Hello World!\"",
  44. "python3": "print(\"Hello World!\")",
  45. "java": "pubic class Main {\n"
  46. " public static void main(String[] args) {\n"
  47. " System.out.println(\"Hello World!\");\n"
  48. " }\n"
  49. "}",
  50. "c": "#include <stdio.h>\n\n"
  51. "int main() {\n"
  52. " printf(\"Hello World!\\n\");\n"
  53. " return 0;\n"
  54. "}",
  55. "c++": "#include <iostream>\n\n"
  56. "int main() {\n"
  57. " std::cout << \"Hello World!\";\n"
  58. " return 0;\n"
  59. "}",
  60. "bash": "echo \"Hello World\"",
  61. "rust": "fn main() {\n"
  62. " println!(\"Hello World!\");\n"
  63. "}",
  64. "ruby": "puts 'Hello, world!'",
  65. "perl": "print \"Hello World!\\n\";",
  66. "c#": "using System;\n"
  67. "namespace HelloWorldApplication{\n"
  68. " class HelloWorld {\n"
  69. " Console.WriteLine(\"Hello World!\");\n"
  70. " Console.ReadKey();\n"
  71. " }\n"
  72. "}",
  73. "basic": "10 PRINT \"Hello World!\"",
  74. "visual basic": "Module HelloWorld\n"
  75. " Sub Main()\n"
  76. " System.Console.WriteLine(\"Hello World!\")\n"
  77. " System.Console.ReadLine()\n"
  78. " End\n"
  79. " End Sub\n"
  80. "End Module\n",
  81. "brainfuck": "++++++++++\n"
  82. "[\n"
  83. " >+++++++>++++++++++>+++>+<<<<-\n"
  84. "]\n"
  85. ">++.\n"
  86. ">+.\n"
  87. "+++++++.\n"
  88. ".\n"
  89. "+++.\n"
  90. ">++.\n"
  91. "<<+++++++++++++++.\n"
  92. ">.\n"
  93. "+++.\n"
  94. "------.\n"
  95. "--------.\n"
  96. ">+.\n"
  97. ">.\n"
  98. "+++.",
  99. "haskell": "main = putStrLn \"Hello World!\"",
  100. "erlang": "-module(hello).\n"
  101. "-export([hello_world/0]).\n\n"
  102. "hello_world() -> io:fwrite(\"Hello World!\n\").",
  103. "prolog": "?- write('Hello World!'), nl.",
  104. "swift": "print(\"Hello World!\")",
  105. "b": "main() {\n"
  106. " printf(\"Hello World!\");\n"
  107. "}",
  108. "d": "import std.stdio;\n"
  109. "void main() {\n"
  110. " writeln(\"Hello World!\");\n"
  111. "}",
  112. "cobol": "000100 IDENTIFICATION DIVISION.\n"
  113. "000200 PROGRAM-ID. HELLOWORLD.\n"
  114. "000900 PROCEDURE DIVISION.\n"
  115. "001000 MAIN.\n"
  116. "001100 DISPLAY \"Hello World!\".\n"
  117. "001200 STOP RUN.",
  118. "fortran": "program hello\n"
  119. "write(*,*) \"Hello World!\"\n"
  120. "end program hello",
  121. "go": "package main\n\n"
  122. "import \"fmt\"\n\n"
  123. "func main() {\n"
  124. " fmt.Println(\"Hello World!\")\n"
  125. "}",
  126. "lua": "print (\"Hello World!\")",
  127. "x86 assembly": "section .data\n"
  128. "str: db 'Hello World!', 0Ah\n"
  129. "str_len: equ $ - str\n\n\n"
  130. "section .text\n"
  131. "global _start\n\n"
  132. "_start:\n"
  133. " mov eax, 4\n"
  134. " mov ebx, 1\n\n"
  135. " mov ecx, str\n"
  136. " mov edx, str_len\n"
  137. " int 80h\n\n"
  138. " mov eax, 1\n"
  139. " mov ebx, 0\n"
  140. " int 80h"}
  141. """
  142. The actual implementations of hello world in the different languages
  143. """
  144. language_not_found_error = {"en": "Programming Language not found",
  145. "de": "Programmiersprache nicht gefunden"}
  146. """
  147. Error message sent when the programming language could not be found.
  148. """
  149. def process_message(self, message: Message) -> None:
  150. """
  151. Process a message according to the service's functionality
  152. :param message: the message to process
  153. :return: None
  154. """
  155. prog_language = message.message_body.lower().split(" ", 1)[1]
  156. if prog_language.startswith("list"):
  157. reply = self.list_languages()
  158. else:
  159. reply = self.implementations[prog_language]
  160. reply_message = self.generate_reply_message(message, "Hello World", reply)
  161. if self.connection.identifier in ["whatsapp", "telegram"] and not prog_language.startswith("list"):
  162. self.send_text_as_image_message(reply_message)
  163. else:
  164. self.send_text_message(reply_message)
  165. @staticmethod
  166. def regex_check(message: Message) -> bool:
  167. """
  168. Checks if the user input is valid for this service to continue
  169. :return: True if input is valid, False otherwise
  170. """
  171. regex = "^/helloworld (list(e)?|" \
  172. + Service.regex_string_from_dictionary_keys([HelloWorldService.implementations]) + ")$"
  173. regex = regex.replace("+", "\+")
  174. return re.search(re.compile(regex), message.message_body.lower())
  175. def list_languages(self) -> str:
  176. """
  177. Creates a list of implemented languages
  178. :return: the list of implemented languages
  179. """
  180. list_string = ""
  181. for language in self.implementations:
  182. list_string += language + "\n"
  183. return list_string.rstrip("\n")