Browse Source

first commit

Evgeny Polivanov 1 year ago
commit
4cc175a689
8 changed files with 77 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 35 0
      BotClass.py
  3. 8 0
      README.md
  4. BIN
      __pycache__/BotClass.cpython-39.pyc
  5. BIN
      __pycache__/config.cpython-39.pyc
  6. 3 0
      config_template.py
  7. 2 0
      requirements.txt
  8. 26 0
      run.py

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+__pycahce__/
+env/
+config.py

+ 35 - 0
BotClass.py

@@ -0,0 +1,35 @@
+import sys, xmpp
+#Основной класс бота
+class JabberBot: 
+    def __init__(self, jid, password):
+        jid = xmpp.JID(jid)
+        self.user, self.server, self.password = jid.getNode(), jid.getDomain(), password
+        self.connect()
+        self.auth()
+    #Метод проверки подключения к серверу xmpp
+    def connect(self): 
+        self.conn = xmpp.Client(self.server, debug = [])
+        conn_result = self.conn.connect()
+        if not conn_result:
+            print("Can't connect to server!\n")
+            sys.exit(1)
+    #Метод аутентификации
+    def auth(self): 
+        auth_result = self.conn.auth(self.user, self.password)
+        if not auth_result:
+            print("Can't to authorize!\n")
+            sys.exit(1)
+    #Метод для привязки функций к событиям
+    def register_handler(self, name, handler):
+        self.conn.RegisterHandler(name, handler)
+
+    def step_on(self):
+        try:
+            self.conn.Process(1)
+        except KeyboardInterrupt: return 0
+        return 1
+
+    def start(self):
+        self.conn.sendInitPresence()
+        print("Bot started!")
+        while self.step_on(): pass

+ 8 - 0
README.md

@@ -0,0 +1,8 @@
+# Simple JabberBot
+### Simple XMPP echo bot.
+
+## Install and run
+1) ```python -m venv env```(for Windows) or ```python3 -m venv env```(for Linux)
+2) ```.\env\Scripts\activate``` or ```source env/bin/activate```
+3) ```pip install -r requirements.txt```
+4) ```python run.py```

BIN
__pycache__/BotClass.cpython-39.pyc


BIN
__pycache__/config.cpython-39.pyc


+ 3 - 0
config_template.py

@@ -0,0 +1,3 @@
+#rename this file to confgi.py
+JID="example@msg.sharix-app.org"
+PASSWORD="12345"

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+six==1.16.0
+xmpppy==0.7.1

+ 26 - 0
run.py

@@ -0,0 +1,26 @@
+import xmpp
+from xmpp import cli
+import config
+from BotClass import JabberBot
+
+def message_handler(conn, mess):
+    text = mess.getBody()
+    user = mess.getFrom()
+    print(text)
+    print(mess)
+    if text is not None:
+        message = xmpp.Message()
+        message.setBody(text)
+        message.setFrom(config.JID)
+        message.setTo(user)
+        message.setType('chat')
+        
+        conn.send(message)
+
+def simple_send_message():
+    cli.send_message(config.JID, config.PASSWORD, "test@msg.sharix-app.org", "request.data")   
+
+#Авторизация и запуск бота
+bot = JabberBot(config.JID, config.PASSWORD)
+bot.register_handler('message', message_handler)
+bot.start()