-
Notifications
You must be signed in to change notification settings - Fork 77
/
send_acks.py
77 lines (58 loc) · 2.88 KB
/
send_acks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
"""
A bot that sends acknowledgements for every message in the account's past messaging history
"""
import argparse
import logging
import sys
import yaml
import kik_unofficial.datatypes.xmpp.chatting as chatting
from kik_unofficial.client import KikClient
from kik_unofficial.callbacks import KikClientCallback
from kik_unofficial.datatypes.xmpp.errors import LoginError
from kik_unofficial.datatypes.xmpp.login import LoginResponse, ConnectionFailedResponse
from kik_unofficial.datatypes.xmpp.history import HistoryResponse
username = sys.argv[1] if len(sys.argv) > 1 else input("Username: ")
password = sys.argv[2] if len(sys.argv) > 2 else input("Password: ")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--credentials", default="creds.yaml", help="Credentials file containing at least username, device_id and android_id.")
args = parser.parse_args()
with open(args.credentials) as f:
creds = yaml.safe_load(f)
if not creds.get("password"):
creds["password"] = input("Password: ")
# set up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(logging.Formatter(KikClient.log_format()))
logger.addHandler(stream_handler)
# create the bot
bot = AckBot(creds)
bot.wait_for_messages()
class AckBot(KikClientCallback):
def __init__(self, creds):
# fmt: off
self.client = KikClient(self, creds["username"], creds["password"], kik_node=creds.get("node"), device_id=creds["device_id"], android_id=creds["android_id"]) # noqa E501 fmt: on
def on_authenticated(self):
print("Authenticated, requesting messaging history")
self.client.request_messaging_history()
def on_login_ended(self, response: LoginResponse):
self.client.log.info(f"Full name: {response.first_name} {response.last_name}")
def on_message_history_response(self, response: HistoryResponse):
self.client.send_ack(messages=response.messages, request_history=response.more)
def on_chat_message_received(self, chat_message: chatting.IncomingChatMessage):
self.client.log.info(f"'{chat_message.from_jid}' says: {chat_message.body}")
self.client.send_ack(chat_message)
def on_group_message_received(self, chat_message: chatting.IncomingGroupChatMessage):
self.client.log.info(f"'{chat_message.from_jid}' from group ID {chat_message.group_jid} says: {chat_message.body}")
self.client.send_ack(chat_message)
# Error handling
def on_connection_failed(self, response: ConnectionFailedResponse):
self.client.log.error(f"Connection failed: {response.message}")
def on_login_error(self, login_error: LoginError):
if login_error.is_captcha():
login_error.solve_captcha_wizard(self.client)
if __name__ == "__main__":
main()