Advertisement
redhorsemanwar

Untitled

Oct 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 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 = None
  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, "rgi": self.command_rgi}
  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.     def operation_invitation(self, op):
  52.         # handle invitations
  53.         param3_list = op.param3.split("\x1e")
  54.         print(param3_list)
  55.         if self.client.profile.mid in param3_list:
  56.             print("The bot got invited")
  57.         if op.param2 in self.bot_data["users"]:
  58.             self.client.accept_group_invitation(op.param1)
  59.         else:
  60.             self.client.reject_group_invitation(op.param1)
  61.  
  62.         pass
  63.  
  64.     def command_test(self, message):
  65.         message.reply_message("test")
  66.  
  67.     def command_rgi(self, message):
  68.         refresh = self.client.refresh_invites
  69.         print(refresh)
  70.         message.reply_message("refreshed")
  71.         ggi = self.client.get_invited_group_mids
  72.         print(ggi)
  73.  
  74.     def process_message(self, message):
  75.         # command detection (AUTIMM STYLE)
  76.         if message.from_id in self.bot_data["users"]:
  77.             # user detected
  78.             message.array_original = message.text.split(" ")
  79.             message.array_lower = message.text.lower().split(" ")
  80.             message.array_length = len(message.array_lower)
  81.             if message.array_lower[0] in self.names:
  82.                 # command detected
  83.                 if message.array_lower[1] in self.command_dict.keys():
  84.                     # valid command
  85.                     self.command_dict[message.array_lower[1]](message)
  86.                 else:
  87.                     message.reply_message(message.text)
  88.  
  89.     def pause(self):
  90.         if self.unpaused is True:
  91.             self.unpaused = False
  92.         else:
  93.             self.unpaused = True
  94.         print("Bot running: %s" % self.unpaused)
  95.  
  96.     def kill(self):
  97.         self.alive = False
  98.         print("Killed bot thread")
  99.  
  100.  
  101. def main():
  102.     try:
  103.         b = Bot(debug=True, names=['u1'])
  104.         b.start()
  105.     except Exception as e:
  106.         print("Failed to login: %s" % e)
  107.         traceback.print_exc()
  108.     while True:
  109.         c = input("pause/kill")
  110.         if c.lower() == "pause":
  111.             b.pause()
  112.         elif c.lower() == "kill":
  113.             b.kill()
  114.             b.join()
  115.             exit()
  116.  
  117.  
  118. if __name__ == '__main__':
  119.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement