def resto(mess, args): """ Retourne les menus de resto """ actions = {'piment': piment, 'eaty': eaty} list_of_resto = '\n'.join(['%s resto %s' % (BaguetteJabberBot().nick, rest) for rest in actions.keys()]) actions[''] = lambda: '%s\n%s' % ('Tu veux dire quoi ?', list_of_resto) def default_action(): return '%s\n%s' % ('Je ne cromprends pas', list_of_resto) BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, actions.get(args.strip(), default_action)())
def ask_baguette(): """ Demande aux gens s'ils veulent une baguette """ global round orders = Order.objects() notifs = Notif.get_all(times_gt=round) results = [ user.name for user in notifs if user.name not in [order.name for order in orders] and user.name not in non_list ] BaguetteJabberBot.send( BaguetteJabberBot(), text="Coucou tout le monde! Voulez vous une baguette {} ?".format( ' '.join(map(str, results))), user=BaguetteJabberBot().room, message_type="groupchat") round += 1
def baguette(mess, args): """Tout pour commande une baguette""" actions = { 'commande': order, 'annule': cancel, 'liste': list_orders, 'notif': notif, 'list-notif': list_notif, 'no-notif': no_notif, } commands = '\n'.join([ '%s baguette %s (%s)' % (BaguetteJabberBot().nick, action_name, action_def.__doc__) for action_name, action_def in actions.iteritems() ]) actions[''] = lambda *args: '%s\n%s' % ('Tu veux dire quoi ?', commands) def default_action(*args): return '%s\n%s' % ('Je ne cromprends pas', commands) BaguetteJabberBot.send_simple_reply( BaguetteJabberBot(), mess, actions.get(args.strip().split(" ")[0], default_action)(mess, args))
def non(mess, args): """ Ne plus me notifier pour aujourd'hui""" global non_list username = mess.getFrom().getResource() if username not in non_list: non_list.append(username) BaguetteJabberBot.send(BaguetteJabberBot(), text="ok, tu prends pas de pain aujourd'hui", user=BaguetteJabberBot().room, message_type="groupchat") else: BaguetteJabberBot.send( BaguetteJabberBot(), text="Oui, j'ai bien compris que tu voulais pas de pain.", user=BaguetteJabberBot().room, message_type="groupchat")
def fact(mess, args): """ Chuck Norris Facts """ # Retrieve a fact req = requests.get('http://www.chucknorrisfacts.fr/api/get?data=tri:alea;nb:1') pars = HTMLParser.HTMLParser() if req.status_code == 200: fact = req.json() BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, pars.unescape(fact[0]['fact'])) else: BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, 'Chuck Norris est malade...')
def star(mess, args): """ Retourne le passage des bus Boulanger star [line_code] """ api_params = { 'dataset': 'tco-bus-circulation-passages-tr', 'geofilter.distance': '48.128336,-1.625569,500', 'sort': '-depart', 'rows': 15, 'timezone': 'Europe/Paris' } base_url = 'https://data.explore.star.fr/api/records/1.0/search/' splitted_args = args.split() if splitted_args and splitted_args[0]: api_params['q'] = 'nomcourtligne=%s' % splitted_args[0] req = requests.get(base_url, params=api_params, verify=False) if req.status_code == 200: bus = [] for record in req.json().get('records', []): stop = record['fields']['nomarret'] line = record['fields']['nomcourtligne'] destination = record['fields']['destination'] passing_time = record['fields']['depart'] # Ugly but working parsed_date = datetime.datetime.strptime(passing_time[:-6], "%Y-%m-%dT%H:%M:%S") passing_time = parsed_date.strftime("%Y-%m-%d %H:%M:%S") bus.append('[%s] %s -> %s - [%s]' % (line, stop, destination, passing_time)) if bus: BaguetteJabberBot.send_simple_reply( BaguetteJabberBot(), mess, u'Voici les prochains bus:\n{}'.format('\n'.join(bus))) else: BaguetteJabberBot.send_simple_reply( BaguetteJabberBot(), mess, "Il n'y a pas de bus prochainement") else: BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, 'star est malade...')
def gif(mess, args): """ Random GIF """ # Retrieve a gif base_url = "http://api.giphy.com/v1/gifs/random" api_params = {'api_key': 'dc6zaTOxFJmzC'} if args: api_params['tag'] = args req = requests.get(base_url, params=api_params) pars = HTMLParser.HTMLParser() if req.status_code == 200: fact = req.json() BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, pars.unescape(fact['data']['image_original_url'])) else: BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, 'Giphy est malade...')
def insulte(mess, args): """Insulte quelqu'un""" # Lire une insulte insulte = list(Insulte.objects().aggregate(*[{ '$sample': { 'size': 1 } }]))[0]['text'] # Qui instulter? if args and BaguetteJabberBot().nick not in args: BaguetteJabberBot.send_simple_reply( BaguetteJabberBot(), mess, u'{}'.format(insulte.replace("%guy%", args))) else: BaguetteJabberBot.send_simple_reply( BaguetteJabberBot(), mess, u'{}'.format(insulte.replace("%guy%", mess.getFrom().getResource())))
def ping(mess, args): """ Tu veux jouer ? """ BaguetteJabberBot.send_simple_reply(BaguetteJabberBot(), mess, 'pong')
def sendmail(): """ Send email """ global round global non_list round = 0 non_list = [] orders = Order.objects() if orders: msg = MIMEText( "Bonjour Marie,\nEst-il possible de rapporter {} baguettes aujourd'hui ?" "\n\nDemandeurs :\n{}".format(len(orders), '\n'.join([o.name for o in orders]))) msg['Subject'] = BaguetteJabberBot().subject msg['From'] = BaguetteJabberBot().fromm msg['To'] = BaguetteJabberBot().mail_to smtp = smtplib.SMTP('localhost') smtp.sendmail(BaguetteJabberBot().fromm, [BaguetteJabberBot().mail_to], msg.as_string()) smtp.quit() BaguetteJabberBot.send( BaguetteJabberBot(), text="J'ai envoye la commande a Marie ! cc {}".format(" ".join( [o.name for o in orders])), user=BaguetteJabberBot().room, message_type="groupchat") Order.drop_collection() else: BaguetteJabberBot.send(BaguetteJabberBot(), text="Pas de commande aujourd'hui !", user=BaguetteJabberBot().room, message_type="groupchat")