from zentropi.shell import ZentropiShell class Relay(Agent): @on_event('*** started') def setup(self, event): # Define custom states. self.states.power = False @on_message('switch {power}', parse=True) def switch(self, message): power = message.data.power.lower().strip() # Clean input if power in ['on', 'up']: self.states.power = True # Will trigger @on_state('power') elif power in ['off', 'down']: self.states.power = False # Will trigger @on_state('power') else: return 'I did not understand {!r}; try "switch [on|off]".'.format( power) return 'Switching power {}'.format(power) # Send reply. @on_state('power') def on_test_state(self, state): print('Change GPIO value to', state.data.value) return True # Accept state change if __name__ == '__main__': relay = Relay('relay') run_agents(relay, shell=True)
index = int(index) - 1 except ValueError: return 'Error: Expected "next" or a number.' tweets = self.get_tweets() if not tweets: return 'No tweets are scheduled.' try: tweet = tweets[index] print('*** index', index) print('*** before', tweets) del tweets[index] print('*** after', tweets) self.save_tweets(tweets) return 'Removed {}: {!r}'.format(index, tweet) except IndexError: return 'Error: Expected 1 <= index <= {}'.format(len(tweets) + 1) except AssertionError as e: return '; '.join(e.args) @on_timer(3 * HOURS) def check_schedule(self): text = self.send_next_tweet() self.telegram_message(text) if __name__ == '__main__': telegram = TelegramAgent('telegram') twitter = TwitterAgent('twitter') tweetin_bird = TweetinBird('tweetin_bird') run_agents(telegram, twitter, tweetin_bird, shell=True)
""" Matches text pattern "good [time_of_day]" Replies only if time_of_day is a known value. """ # Extract field time_of_day from message.data, remove any extra spaces and lower-case it. time_of_day = message.data.time_of_day.strip().lower() # Make a decision if time_of_day == 'morning': # Returning a string will send a reply to the appropriate channel. return 'Top of the morning to you!' elif time_of_day in ['afternoon', 'evening', 'night']: return 'Good {}!'.format(time_of_day) # For "Good [whatever]", we won't respond. return @on_message('will it rain?', fuzzy=True) def rain_forecast(self, message): return random.choice(['Maybe?', 'Umm...', 'I am not sure']) if __name__ == '__main__': # Execute the following only if run as a script. from zentropi.extra.agents import SlackAgent from zentropi import run_agents slack_agent = SlackAgent() my_bot = MyBot() run_agents(slack_agent, my_bot, space='slack', endpoint='inmemory://slack')
# coding=utf-8 from zentropi import ( Agent, on_message, run_agents, ) class HelloBot(Agent): @on_message('hello') def say_hello(self, message): return 'hello, world' if __name__ == '__main__': hello_bot = HelloBot(name='hello_bot') run_agents(hello_bot, shell=True)
#!/usr/bin/env python # coding=utf-8 from zentropi import Agent, on_message, run_agents class Spartan(Agent): @on_message('Spartans?', fuzzy=True) def war_cry(self, message): return 'Ahoo!' if __name__ == '__main__': warrior = Spartan(name='Spartan') run_agents(warrior, space='Thermopylae', shell=True)
results = self.search(query) self.emit('snippet-query-results', data={'results': results}) @on_event('snippet-delete') def on_snippet_delete(self, event): id_ = event.data.id if not id_: self.emit('snippet-delete-error', data={'error': '`data.id` not found in event-id: {}'.format(event.id)}) return self.delete(id_) self.emit('snippet-delete-complete') @on_message('snip {snippet}', parse=True) def on_save(self, message): snippet = message.data.snippet self.save(snippet) return 'Saved' @on_message('snips {query}', parse=True) def on_search(self, message): query = message.data.query.strip().lower() results = self.search(query) if not results: return 'Found nothing.' return '\n'.join(results) if __name__ == '__main__': snippet_bot = SnippetBot(name='snip') run_agents(snippet_bot, shell=True)
# coding=utf-8 from zentropi import ( Agent, on_message, run_agents ) from zentropi.extra.agents import TelegramAgent class EchoBot(Agent): @on_message('help', fuzzy=True) async def help(self, message): return 'Hi, I am {}. I echo messages.'.format(self.name) @on_message('*') async def echo(self, message): # This handler will be called for all messages: @on_message('*'), # we want to skip any messages sent by our own agent ("Echo: ...") if message.source == self.name: # quit here if we are the source of the message. return return 'Echo: {}'.format(message.data.text) if __name__ == '__main__': echo_bot = EchoBot('echo_bot') telegram_agent = TelegramAgent('tg') run_agents(echo_bot, telegram_agent, shell=True)
self.document.body.appendChild(self.ul) @on_event('*') @on_message('*') def every_frame(self, frame): li = Li() prefix = FRAME_PREFIX[frame.kind] text = frame_template.format(prefix=prefix, source=frame.source, name=frame.name) data = frame.data if data and not (len(data.keys()) == 1 and 'text' in data): text += data_template.format(data=pformat(data)) li.innerHTML = text self.ul.appendChild(li) if __name__ == '__main__': start_server() try: dom_agent = BrowserDOMAgent('dom') run_agents(dom_agent, shell=True, endpoint='redis://127.0.0.1:6379', auth=ZENTROPI_REDIS_PASSWORD) except KeyboardInterrupt: stop_server() finally: stop_server()
# coding=utf-8 from zentropi import (Agent, ZentropiShell, on_event, on_timer, run_agents) class PingBot(Agent): @on_timer(2) def send_ping(self): self.emit('ping') print('PING') @on_event('pong') def on_pong(self, event): print('PONG') class PongBot(Agent): @on_event('ping') async def on_ping(self, event): await self.sleep(0.2) self.emit('pong') if __name__ == '__main__': ping_bot = PingBot(name='ping_bot') pong_bot = PongBot(name='pong_bot') run_agents(ping_bot, pong_bot, shell=True)