def on_join_msg(self, msg): self.debug_print("on_join_msg() called.", 2) self.debug_print("Transformed channel: '%s'" % msg.channel.lower(), 3) if msg.channel.lower() != "#botymcbotface": return None self.debug_print( "Transformed sender: '%s'" % msg.sender.replace("@", "").lower(), 3) if msg.sender.replace("@", "").lower() in [ "enfors", "botymcbotface", "botymctest", "enforsbot", "enforstestbot" ]: if msg.sender.lower() != self.nickname.lower(): self.make_operator(msg.channel, msg.sender) return None message = eb_message.Message("IRC", eb_message.MSG_TYPE_NOTIFY_USER, { "user": "******", "text": "We have a visitor in " \ "#BotyMcBotface: %s." % msg.sender}) self.config.send_message("Main", message)
def run(self): super(TwitterThread, self).run() with self.config.lock: self.config.twitter_auth = tweepy.OAuthHandler( CONSUMER_KEY, CONSUMER_SECRET) self.config.twitter_auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) self.rest_thread = TwitterRestThread("TwitterRest", self.config) self.rest_thread.start() #self.streams_thread = TwitterStreamsThread("TwitterStreams", # self.config) #self.streams_thread.start() message = eb_message.Message("Twitter", eb_message.MSG_TYPE_THREAD_STARTED) self.config.send_message("Main", message) while True: message = self.config.recv_message("Twitter") if message.msg_type == eb_message.MSG_TYPE_STOP_THREAD: self.stop() return
def stop_thread(self, thread): "Stop one specific thread." message = eb_message.Message("Main", eb_message.MSG_TYPE_STOP_THREAD, {}) self.config.send_message(thread, message) self.config.threads[thread].join()
def handle_message(self, msg): content_type, chat_type, chat_id = telepot.glance(msg) if content_type == "text": text = msg["text"].strip() user = msg["chat"]["id"] if text[0] == "/": text = text[1:] if text.lower().startswith("@enfors_bot "): text = text[12:] print("Telegram: Incoming message from %s(%d): '%s'" % (msg["from"]["first_name"], user, text)) # If this is a kind of message only Telegram can handle # (inline keyboards, etc), then don't send it to the # main thread. if self.handle_telegram_message(text.lower(), user): return # Send the message to the main thread. message = eb_message.Message("Telegram", eb_message.MSG_TYPE_USER_MESSAGE, { "user": msg["chat"]["id"], "text": text }) self.config.send_message("Main", message) else: print("Incoming %s message, ignoring.", content_type)
def run(self): super().run() with self.config.lock: self.bot = telepot.Bot(TOKEN) self.bot.message_loop(self.handle_message) message = eb_message.Message("Telegram", eb_message.MSG_TYPE_THREAD_STARTED) self.config.send_message("Main", message) while True: try: message = self.config.recv_message("Telegram") if (message.msg_type == eb_message.MSG_TYPE_STOP_THREAD): self.stop() return if message.msg_type == eb_message.MSG_TYPE_USER_MESSAGE: self.bot.sendMessage(message.data["user"], message.data["text"]) except error: print("ERROR: %s" % error) self.config.set_thread_state("Telegram", "exception") time.sleep(60)
def send_location_update_to_main(self, location, arrived): "Send a location update to the main thread." message = eb_message.Message("TwitterStreams", eb_message.MSG_TYPE_LOCATION_UPDATE, { "location": location, "arrived": arrived }) self.config.send_message("Main", message)
def send_user_message_to_main(self, user, text): "Send a user message to the main thread." message = eb_message.Message("TwitterStreams", eb_message.MSG_TYPE_USER_MESSAGE, { "user": user, "text": text }) self.config.send_message("Main", message)
def handle_incoming_notify_user(self, message): "Send notification message through Twitter." out_message = eb_message.Message("Main", eb_message.MSG_TYPE_USER_MESSAGE, { "user": message.data["user"], "text": message.data["text"] }) self.config.send_message("TwitterRest", out_message)
def on_private_msg(self, msg): self.debug_print("on_private_msg() called.", 2) # We don't need to respond to nickserv. if msg.sender.lower() == "nickserv": return None message = eb_message.Message( "IRC", eb_message.MSG_TYPE_USER_MESSAGE, { "user": msg.sender, "msg_type": msg.msg_type, "channel": msg.channel, "text": msg.msg_text }) self.config.send_message("Main", message)
def run(self): super().run() self.db = sqlite3.connect("enforsbot.db", detect_types=sqlite3.PARSE_DECLTYPES) self.connect("irc.freenode.net", "#Enfors") self.join_channel("#Enfors") message = eb_message.Message("IRC", eb_message.MSG_TYPE_THREAD_STARTED) self.config.send_message("Main", message) self.main_loop()
def run(self): super().run() message = eb_message.Message("TwitterRest", eb_message.MSG_TYPE_THREAD_STARTED) self.config.send_message("Main", message) while True: message = self.config.recv_message("TwitterRest") if message.msg_type == eb_message.MSG_TYPE_USER_MESSAGE: self.send_direct_message(message.data["text"], message.data["user"]) elif message.msg_type == eb_message.MSG_TYPE_STOP_THREAD: self.stop() return
def run(self): super().run() #last_err = None message = eb_message.Message("TwitterStreams", eb_message.MSG_TYPE_THREAD_STARTED) self.config.send_message("Main", message) #self.stream.filter(track=['#svpol']) streaming = False while True: try: message = self.config.recv_message("TwitterStreams", wait=False) if message and \ message.msg_type == eb_message.MSG_TYPE_STOP_THREAD: self.stop() return if not streaming: self.stream.userstream(async=False) streaming = True time.sleep(1) except AttributeError as err: print("Twitter: Attribute exception handled: %s" % err) except ConnectionError as err: print("Twitter: Connection exception handled: %s" % err) except ValueError as err: print("Twitter: Value exception handled: %s" % err) except OSError as err: print("Twitter: OS exception handled: %s" % err) except Exception as err: # This happens every time it times out (a lot, intentionally) print("Twitter: Exception handled: %s" % err)
def handle_incoming_user_message(self, message, response_thread): "Handle an incoming message of type USER." user_name = message.data["user"] text = message.data["text"] print("Main: Message from %s: '%s'" % (user_name, text)) protocol = response_thread if protocol.startswith("Twitter"): protocol = "Twitter" user = self.user_handler.find_user_by_identifier(protocol, user_name) print(user) response = "" default_response = "I'm afraid I don't understand." # If this is an IRC message: if response_thread == "IRC": #msg_type = message.data["msg_type"] channel = message.data["channel"] # But don't respond unless it's a private message. if channel.lower() != "enforsbot" and \ channel.lower() != "enforstestbot": return None text = text.lower() # If this is a command to start an activity: if text in self.activity_cmds.keys() and not user.current_activity(): self.start_activity(user, text) # If we don't have a name for the user, then insert # a question about the user's name. if user.name is None and not user.current_activity(): self.start_ask_user_name(user, text) # Handle any activities that are currently going on if user.current_activity(): repeat = True while repeat: status = self.handle_activity(user, text) response += status.output + " " repeat = status.done and user.current_activity() else: for pattern, pattern_response in self.responses.items(): pat = re.compile(pattern) if pat.match(text): response = pattern_response if callable(response): response = response(text) if response is "": response = default_response response = response.strip() + "\n" if response is not None: print(" - Response: %s" % response.replace("\n", " ")) message = eb_message.Message("Main", eb_message.MSG_TYPE_USER_MESSAGE, { "user": user_name, "text": response }) self.config.send_message(response_thread, message)
def stop(self): print("Thread stopped: %s" % self.name) message = eb_message.Message("Main", eb_message.MSG_TYPE_THREAD_STOPPED, {}) self.config.send_message(self.name, message)