Advertisement
Hey_Arnold

Kik keepalive example

Jul 6th, 2022
745
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import kik_unofficial.datatypes.xmpp.chatting as chatting
  3. from kik_unofficial.client import KikClient
  4. from kik_unofficial.callbacks import KikClientCallback
  5. from kik_unofficial.datatypes.xmpp.errors import LoginError
  6. from kik_unofficial.datatypes.xmpp.login import ConnectionFailedResponse, LoginResponse
  7.  
  8. global kik_authenticated, client, online_status, my_jid
  9. online_status = None
  10.  
  11. def login(username, password):
  12.  
  13.     def main():
  14.         YourBot()
  15.  
  16.     global client, my_jid
  17.  
  18.     class YourBot(KikClientCallback):
  19.         global client, my_jid
  20.  
  21.         def __init__(self):
  22.             global client
  23.             client = KikClient(self, username, password)
  24.  
  25.         def on_login_ended(self, response: LoginResponse):
  26.             global my_jid
  27.             my_jid = str(response.kik_node) + "@talk.kik.com"
  28.             print("Saved JID \"" + my_jid + "\" for refreshing!")
  29.  
  30.         def on_authenticated(self):
  31.             print("Kik login successful!")
  32.             global kik_authenticated
  33.             kik_authenticated = True
  34.  
  35.         def on_chat_message_received(self, chat_message: chatting.IncomingChatMessage):
  36.             global online_status
  37.             online_status = True
  38.  
  39.         def on_connection_failed(self, response: ConnectionFailedResponse):
  40.             global kik_authenticated
  41.             print("Connection failed!")
  42.             kik_authenticated = False
  43.  
  44.         def on_login_error(self, login_error: LoginError):
  45.             global kik_authenticated
  46.             print("Kik login failed!")
  47.             kik_authenticated = False
  48.  
  49.     if __name__ == '__main__':
  50.         main()
  51.  
  52. def refresh(username, password):
  53.     global online_status, my_jid
  54.     try:
  55.         online_status = False
  56.         client.send_chat_message(my_jid, "This is a message to myself to check if I am online.")
  57.         time.sleep(2)
  58.         if online_status == True:
  59.           print("Bot is online!")
  60.           return True
  61.         elif online_status == False:
  62.           kik_authenticated = None
  63.           print("Reconnecting...")
  64.           login(username, password)
  65.           while kik_authenticated == None:
  66.             pass
  67.           if kik_authenticated == False:
  68.             return False
  69.           elif kik_authenticated == True:
  70.             return True
  71.     except:
  72.         print("Something went wrong while refreshing!")
  73.         return False
  74.  
  75. username = input("Enter a username: ")
  76. password = input("Enter a password: ")
  77.  
  78. login(username, password)
  79. time.sleep(300)
  80. keepalive = True
  81. while keepalive == True: #refreshes every 5 minutes
  82.     print("Refreshing...")
  83.     result = refresh(username, password)
  84.     while result == False: #Retries every minute if refresh fails
  85.       result = refresh(username, password)
  86.       time.sleep(60)
  87.     time.sleep(300)
Advertisement
RAW Paste Data Copied
Advertisement