def main(): log.info("Starting bot.") bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) while True: try: response = bot.fetch_events() for event in response.json()["response"]["data"]["events"]: if event["type"] == "im": event_data = event["eventData"] source_uin = event_data["source"]["aimId"] message = event_data["message"] if "stickerId" not in event_data: bot.send_im(target=source_uin, message=process(message)) except KeyboardInterrupt: log.info("Shutting down bot.") sys.exit() except: log.exception("Exception in main loop!")
def main(): # Creating new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering required handlers for messages. bot.dispatcher.add_handler(CommandHandler(callback=command_callback)) bot.dispatcher.add_handler( UserAddedToBuddyListHandler(callback=user_added_callback)) bot.dispatcher.add_handler( MessageHandler(callback=text_callback, filters=MessageFilter.text)) bot.dispatcher.add_handler( MessageHandler(callback=sticker_callback, filters=MessageFilter.sticker)) bot.dispatcher.add_handler( MessageHandler(callback=link_callback, filters=MessageFilter.link)) bot.dispatcher.add_handler( MessageHandler(callback=image_callback, filters=MessageFilter.image)) bot.dispatcher.add_handler( MessageHandler(callback=video_callback, filters=MessageFilter.video)) bot.dispatcher.add_handler( MessageHandler(callback=audio_callback, filters=MessageFilter.audio)) bot.dispatcher.add_handler( MessageHandler(callback=chat_callback, filters=MessageFilter.chat)) bot.dispatcher.add_handler( FeedbackCommandHandler(command="fb", target="176756440")) # Starting polling thread watching for new events from server. This is non-blocking call. bot.start_polling() # Block current thread while bot working until SIGINT, SIGTERM or SIGABRT received. bot.idle()
def main(): # Creating a new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering message handlers. bot.dispatcher.add_handler(HelpCommandHandler(callback=help_cb)) bot.dispatcher.add_handler( UnknownCommandHandler(callback=unknown_command_cb)) bot.dispatcher.add_handler(UserAddedToBuddyListHandler(user_added_cb)) bot.dispatcher.add_handler(TypingHandler(typing_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.text, callback=text_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.sticker, callback=sticker_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.url, callback=url_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.image, callback=image_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.video, callback=video_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.audio, callback=audio_cb)) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.chat, callback=chat_cb)) bot.dispatcher.add_handler(DefaultHandler(default_cb)) # Registering command handlers. bot.dispatcher.add_handler(FeedbackCommandHandler(target=OWNER)) # Starting a polling thread watching for new events from server. This is a non-blocking call. bot.start_polling() # Blocking the current thread while the bot is working until SIGINT, SIGTERM or SIGABRT is received. bot.idle()
def message(self, uin, text): text = str(text).replace('\\n', '\n') dbg_param = (f'{__class__.__name__}.message, ' f'UIN:{str(uin)}, TEXT:{text}') log.info(dbg_param) print(dbg_param) try: bot = ICQBot(token=self.TOKEN, name=self.NAME, version=self.VERSION) response = bot.send_im(target=uin, message=text) dbg_resp = f'RESPONSE: {response}' log.info(dbg_resp) print(dbg_resp) sys.exit(0) except Exception as e: dbg_resp = f'RESPONSE: ERROR {e}' log.error(dbg_resp) print(dbg_resp) sys.exit(1)
def main(): # Creating new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering required handlers for messages. bot.dispatcher.add_handler(MessageHandler(callback=message_callback, filters=MessageFilter.text)) # Registering command handlers. bot.dispatcher.add_handler(UnknownCommandHandler(message_callback)) # Starting polling thread watching for new events from server. This is non-blocking call. bot.start_polling() # Block current thread while bot working until SIGINT, SIGTERM or SIGABRT received. bot.idle()
def main(): # Creating a new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering message handlers. bot.dispatcher.add_handler( MessageHandler(callback=file_callback, filters=MessageFilter.file & ~(MessageFilter.image | MessageFilter.video | MessageFilter.audio))) # Starting a polling thread watching for new events from server. This is a non-blocking call. bot.start_polling() # Blocking the current thread while the bot is working until SIGINT, SIGTERM or SIGABRT is received. bot.idle()
def main(): # Creating a new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering message handlers. bot.dispatcher.add_handler(TypingHandler(typing_cb)) bot.dispatcher.add_handler(MessageHandler(filters=MessageFilter.message, callback=message_cb)) bot.dispatcher.add_handler(MessageHandler(filters=MessageFilter.sticker, callback=sticker_cb)) # Registering command handlers. bot.dispatcher.add_handler(CommandHandler(command="status", callback=status_cb)) # Starting a polling thread watching for new events from server. This is a non-blocking call. bot.start_polling() # Blocking the current thread while the bot is working until SIGINT, SIGTERM or SIGABRT is received. bot.idle()
def main(): # Creating a new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering handlers. bot.dispatcher.add_handler(UserAddedToBuddyListHandler(help_cb)) bot.dispatcher.add_handler(HelpCommandHandler(callback=help_cb)) bot.dispatcher.add_handler( MessageHandler(filters=~MessageFilter.command, callback=message_cb)) bot.dispatcher.add_handler(UnknownCommandHandler(callback=message_cb)) bot.dispatcher.add_handler(FeedbackCommandHandler(target=OWNER)) # Starting a polling thread watching for new events from server. This is a non-blocking call. bot.start_polling() # Blocking the current thread while the bot is working until SIGINT, SIGTERM or SIGABRT is received. bot.idle()
def main(): # Creating a new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering handlers. bot.dispatcher.add_handler(UserAddedToBuddyListHandler(help_cb)) bot.dispatcher.add_handler(MessageHandler(filters=MessageFilter.text, callback=search_cb)) bot.dispatcher.add_handler(CommandHandler(command="random", callback=random_cb)) bot.dispatcher.add_handler(CommandHandler(command="trending", callback=trending_cb)) bot.dispatcher.add_handler(FeedbackCommandHandler(target=OWNER, reply="Got it!\n---\nПринято!")) bot.dispatcher.add_handler(UnknownCommandHandler(callback=help_cb)) bot.dispatcher.add_handler(MessageHandler( filters=MessageFilter.message & ~(MessageFilter.text | MessageFilter.command), callback=help_cb )) # Starting a polling thread watching for new events from server. This is a non-blocking call. bot.start_polling() # Blocking the current thread while the bot is working until SIGINT, SIGTERM or SIGABRT is received. bot.idle()
def main(): # Creating new bot instance. bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) # Registering required handlers for typing and messages. bot.dispatcher.add_handler(TypingHandler(typing_callback)) bot.dispatcher.add_handler( MessageHandler(callback=message_callback, filters=MessageFilter.text | MessageFilter.sticker)) # Registering command handlers. bot.dispatcher.add_handler( CommandHandler(callback=help_callback, command="help")) bot.dispatcher.add_handler( CommandHandler(callback=status_callback, command="status")) bot.dispatcher.add_handler( FeedbackCommandHandler(command="feedback", target=OWNER)) bot.dispatcher.add_handler(UnknownCommandHandler(message_callback)) # Starting polling thread watching for new events from server. This is non-blocking call. bot.start_polling() # Block current thread while bot working until SIGINT, SIGTERM or SIGABRT received. bot.idle()
import logging import os import pprint from datetime import datetime from flask import Flask from flask import jsonify from flask import request from flask_sqlalchemy import SQLAlchemy from icq.bot import ICQBot NAME = "toor" VERSION = "0.0.1" ICQ_BOT_TOKEN = os.environ['ICQ_BOT_TOKEN'] ICQ_CHAT_UIN = os.environ['ICQ_CHAT_UIN'] bot = ICQBot(token=ICQ_BOT_TOKEN, name=NAME, version=VERSION) DB_PATH = os.environ.get('DB_PATH', '/var/lib/alerts/alert.db') app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + DB_PATH open(DB_PATH, 'a').close() db = SQLAlchemy(app) TIME_SEC = 24 * 60 * 60 RESOLVED = "resolved" class Alerts(db.Model): __tablename__ = 'alerts'
def main(): bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) bot.dispatcher.add_handler( MessageHandler(filters=MessageFilter.message, callback=message_event)) bot.start_polling() bot.idle()
def main(): log.info("Starting bot.") bot = ICQBot(token=TOKEN, name=NAME, version=VERSION) while True: # noinspection PyBroadException try: response = bot.fetch_events() for event in response.json()["response"]["data"]["events"]: if event["type"] == "im": event_data = event["eventData"] source_uin = event_data["source"]["aimId"] message = event_data["message"] try: bot.set_typing(target=source_uin, typing_status=TypingStatus.TYPING.value) # Getting info for file in message. (_, _, path, _, _) = urlparse.urlsplit(message) file_id = path.rsplit("/", 1).pop() file_info_response = bot.get_file_info(file_id=file_id) if file_info_response.status_code == requests.codes.not_found: raise FileNotFoundException link = file_info_response.json()["file_list"].pop( )["dlink"] # Starting file download. file_response = bot._http_session.get(link, stream=True) if file_response.encoding is None: file_response.encoding = 'utf-8' # Downloading file and calculating stats. stats = defaultdict(int) status_codes = defaultdict(int) for log_record in iterate_log( (line for line in file_response.iter_lines( decode_unicode=True) if line is not None)): if isinstance(log_record, HTTPLogRecord): (request, response) = (log_record.request, log_record.response) stats["requests_count"] += 1 if request.url.path == "/aim/startSession": stats["start_session_count"] += 1 if request.url.path == "/genToken": stats["gen_token_count"] += 1 if response: status_codes[response.status_code + " " + response.reason_phrase] += 1 else: stats["no_response_count"] += 1 bot.send_im( target=source_uin, message= ("Total requests: {requests_count}\n /aim/startSession: {start_session_count}\n /g" "enToken: {gen_token_count}\n\nResponse count by status code:\n{status_codes}\n\nFound " "problems:\n{problems}\n\n{phrase}"). format( requests_count=stats["requests_count"], start_session_count=stats[ "start_session_count"], gen_token_count=stats["gen_token_count"], status_codes="\n".join([ " {code}: {count}".format(code=code, count=count) for (code, count) in sorted(status_codes.items()) ]), problems= " Requests without response: {no_response_count}" .format(no_response_count=stats[ "no_response_count"]), phrase=random.choice(PHRASES))) except FileNotFoundException: bot.send_im(target=source_uin, message=random.choice(PHRASES) + " Give me your log right now!") except NotImplementedError: bot.send_im(target=source_uin, message=random.choice(PHRASES) + " Log format is not supported!") except Exception: bot.send_im(target=source_uin, message=random.choice(PHRASES) + " Something has gone wrong!") raise finally: bot.set_typing(target=source_uin, typing_status=TypingStatus.NONE.value) except KeyboardInterrupt: log.info("Shutting down bot.") sys.exit() except Exception: log.exception("Exception in main loop!")