Advertisement
redhorsemanwar

Untitled

Oct 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. from api import LineClient
  2. from api.services import ttypes
  3. import threading
  4. import traceback
  5. import livejson
  6.  
  7.  
  8. class Bot(threading.Thread):
  9.     client = None
  10.     unpaused = True
  11.     alive = True
  12.     invites = False
  13.  
  14.     def __init__(self, id=None, password=None, token=None, qr=False, debug=False, names=[]):
  15.         super(Bot, self).__init__()
  16.         if id is not None and password is not None:
  17.             self.client = LineClient(id=id, password=password, qr=qr)
  18.         elif token is not None:
  19.             self.client = LineClient(token=token, qr=qr)
  20.         else:
  21.             self.client = LineClient()
  22.         self.debug = debug
  23.         self.bot_data = livejson.File("%s_data.json" % self.client.profile.mid)
  24.         if "users" not in self.bot_data.keys():
  25.             self.bot_data["users"] = []
  26.             self.bot_data["users"].append(self.client.profile.mid)
  27.             print("First time loading users, created list and added my own UID")
  28.         self.names = names
  29.         self.ops_dict = {26: self.operation_message_receive, 13: self.operation_invitation}
  30.         self.message_type_dict = {0: self.process_message}
  31.         self.command_dict = {"test": self.command_test}
  32.  
  33.     def run(self):
  34.         while self.alive is True:
  35.             while self.unpaused is True:
  36.                 for op in self.client.long_poll():
  37.                     if self.debug is True:
  38.                         print("[%s]:\t%s" % (ttypes.OpType._VALUES_TO_NAMES[op.type], op))
  39.                     if op.type in self.ops_dict.keys():
  40.                         self.ops_dict[op.type](op)
  41.                     else:
  42.                         # op unprocessed
  43.                         pass
  44.  
  45.     def operation_message_receive(self, op):
  46.         if op.message.content_type in self.message_type_dict.keys():
  47.             self.message_type_dict[op.message.content_type](op.message)
  48.         else:
  49.             # message unprocessed
  50.             pass
  51.  
  52.     def operation_invitation(self, op):
  53.         # handle invitations
  54.         self.operation_invitation.append
  55.         pass
  56.    
  57.     def command_test(self, message):
  58.         message.reply_message("test")
  59.  
  60.     def process_message(self, message):
  61.         # command detection (AUTIMM STYLE)
  62.         if message.from_id in self.bot_data["users"]:
  63.             # user detected
  64.             message.array_original = message.text.split(" ")
  65.             message.array_lower = message.text.lower().split(" ")
  66.             message.array_length = len(message.array_lower)
  67.             if message.array_lower[0] in self.names:
  68.                 # command detected
  69.                 if message.array_lower[1] in self.command_dict.keys():
  70.                     # valid command
  71.                     self.command_dict[message.array_lower[1]](message)
  72.                 else:
  73.                     message.reply_message(message.text)
  74.  
  75.     def pause(self):
  76.         if self.unpaused is True:
  77.             self.unpaused = False
  78.         else:
  79.             self.unpaused = True
  80.         print("Bot running: %s" % self.unpaused)
  81.  
  82.     def kill(self):
  83.         self.alive = False
  84.         print("Killed bot thread")
  85.  
  86.  
  87. def main():
  88.     try:
  89.         b = Bot(debug=True, names=['meh'])
  90.         b.start()
  91.     except Exception as e:
  92.         print("Failed to login: %s" % e)
  93.         traceback.print_exc()
  94.     while True:
  95.         c = input("pause/kill")
  96.         if c.lower() == "pause":
  97.             b.pause()
  98.         elif c.lower() == "kill":
  99.             b.kill()
  100.             b.join()
  101.             exit()
  102.  
  103.  
  104. if __name__ == '__main__':
  105.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement