def _parse_stream(self, bot): stream = JSONStream(self.flow_user_api_key) gen = stream.fetch([self.channel], active=True) for data in gen: process_message = type(data) == dict and (data['event'] == "message" or data['event'] == "comment") if process_message and ("user" in data and self.user != data["user"]): self.spoken = False bot_input = BotInput() if type(data['content']) is dict: bot_input.message = data["content"]['text'] elif "content" in data: bot_input.message = data["content"] else: break if "user" in data and int(data["user"]) > 0: try: bot_input.nick = self.get_user_by_id(data["user"])["nick"] self.user_id = data["user"] if random.random() < (self.chattiness / 100): logger.log("Randomly sending message to %s" % bot_input.nick) self.private_message(data["user"], random.choice(self.responses["private_messages"])) except Exception as e: logger.error(e) self.say(bot.responses["stranger"]) elif "external_name" in data: bot_input.nick = data["external_name"] else: bot_input.nick = "anonymous" bot_input.bot = bot self.user_nick = bot_input.nick marvin.process(bot_input, self)
def run(self, bot): marvin.say_hi(self) while True: self.spoken = False message = input("> ") if "exit" in message: print("Well that's rude. Goodbye") exit() bot_input = BotInput() bot_input.message = message bot_input.bot = bot bot_input.nick = self.nick marvin.process(bot_input, self)
def _parse_stream(self, bot): stream = JSONStream(self.token, self.room_id) gen = stream.fetch() for data in gen: process_message = type(data) == dict and ("text" in data) if process_message and ("fromUser" in data): from_user = data["fromUser"]["username"] self.spoken = False bot_input = BotInput() bot_input.message = data["text"] try: bot_input.nick = from_user #bot_input.bot_speaking = from_user.lower().startswith(self.nick.lower()) if from_user.lower().startswith(self.nick.lower()): continue self.user_id = data["fromUser"]["id"] except Exception as e: logger.error(e) self.say(bot.responses["stranger"]) bot_input.bot = bot self.user_nick = bot_input.nick marvin.process(bot_input, self)