def send_message(data): channelname = data.get('channelname', None) textmessage = data.get('text', None) displayname = data.get('displayname', None) timestamp = datetime.utcnow().strftime("%m/%d/%Y, %H:%M:%S") channel = channels.get(channelname, None) if channel is None: # channel does not exist. # TODO handle! return "Could not find the channel", 404 # maximum messages storage = 100. messages = channel.get('messages', []) if len(messages) == 100: delete_message({'msgIndex': 0, 'channelname': channelname}) newId: int = 0 try: lastAddedId = messages[-1]['id'] newId = lastAddedId + 1 except: # no messages in this channel, id will be 0 (newid=0) for the first message. pass message = Message(id=newId, displayname=displayname, text=textmessage, timestamp=timestamp) messages.append(message.asdict()) emit('message sent', message.asdict(), room=channelname)
def _populate_database(): get_user_model().objects.create_superuser(username='******', password='******') # Approved farmers for farmer_name in ('Philippe', 'Pierre', 'Agnès', 'Marie', 'Jean', 'Sophie'): email = farmer_name + "@example.com" get_user_model().objects.create_user(farmer_name, email=email, password="******") farmer = Farmer( name=farmer_name, lat=45.1808, lon=1.893, email=email, approved=True, can_send_messages=True, ) farmer.save() # Unapproved farmers for farmer_name in ('Edouard', 'Nicolas', 'Anne', 'Claire'): email = farmer_name + "@example.com" get_user_model().objects.create_user(farmer_name, email=email, password="******") farmer = Farmer( name=farmer_name, lat=0.0, lon=0.0, email=email, ) farmer.save() # Message sent from Edouard to Philippe edouard = Farmer.objects.get(name="Edouard") philippe = Farmer.objects.get(name="Philippe") Message( sender=edouard, recipient=philippe, subject="Hello Philippe", body="How you doin?" ).save() # Message sent from Edouard to Philippe edouard = Farmer.objects.get(name="Edouard") agnes = Farmer.objects.get(name="Agnès") Message( sender=edouard, recipient=agnes, subject="Hello Agnès", body="Everything OK?", sent_at=datetime.datetime(2019, 1, 1, tzinfo=pytz.timezone('Europe/Paris')) ).save()
def handle_send_message_event(data): sender = User(username=data['username'], user_id=data['user_id']) user_type = data['user_type'] current_room = get_room(data['room_id']) last_sender = None prev_msg = None if len(current_room.messages) > 0: index = len(current_room.messages) - 1 prev_msg = current_room.messages[index] last_sender = prev_msg.sender if user_type == "BOT": sender.personality = data['personality'] sender.user_type = user_type bot = assign_bot(personality=sender.personality) prev_content = prev_msg.content.message if prev_msg else None message = bot(prev_content) else: message = Message(sender=sender, content=Content(message=data['msg'])) data['ok'] = "DIN TUR" if last_sender is None or last_sender.user_id != sender.user_id: data['message'] = asdict(message) current_room.messages.append(message) socket.emit('receive_message', data, to=current_room.room_id)
def sweet(message=None) -> Message: if message: return responses.choose_sweet_response(message) else: return Message( sender=user3, content=Content(message="Do you want to cook together?"))
def dict2Msg(dct): sender = dict2User(dct['sender']) content = dict2Content(dct['content']) msg_type = dct['msg_type'] msg_id = dct['msg_id'] return Message(sender=sender, content=content, msg_type=msg_type, msg_id=msg_id)
def _populate_database(): User.objects.create_superuser(username='******', password='******') # Approved farmers for farmer_name in ('Philippe', 'Pierre', 'Agnès'): email = farmer_name + "@farmer.com" User.objects.create_user(farmer_name, email=email, password="******") farmer = Farmer( name=farmer_name, lat=45.1808, lon=1.893, email=email, approved=True, ) farmer.save() # Unapproved farmers User.objects.create_user("Edouard", email="*****@*****.**", password="******") Farmer( name="Edouard", email="*****@*****.**", lat=0.0, lon=0.0, approved=False, phone_number='012345678' ).save() # Message sent from Edouard to Philippe edouard = Farmer.objects.get(name="Edouard") philippe = Farmer.objects.get(name="Philippe") Message( sender=edouard, recipient=philippe, subject="Hello Philippe", body="How you doin?" ).save() # Message sent from Edouard to Philippe edouard = Farmer.objects.get(name="Edouard") agnes = Farmer.objects.get(name="Agnès") Message( sender=edouard, recipient=agnes, subject="Hello Agnès", body="Everything OK?", sent_at=datetime.datetime(2019, 1, 1, tzinfo=pytz.timezone('Europe/Paris')) ).save()
def grumpy(message=None) -> Message: if message: return responses.choose_grumpy_response(message) else: return Message( sender=user1, content=Content( message="Let's sleep at my house. You coming or what?"))
def annoying(message=None) -> Message: if message: return responses.choose_annoying_response(message) else: return Message( sender=user2, content=Content( message="It's time to steal something. You guys in?"))
def get(self): messages = Message.gql("ORDER BY date DESC LIMIT 30") template_values = { 'title': 'PintMe General Feed', 'link': 'http://pintme.appspot.com/', 'description': 'PintMe General Feed', 'items': messages } # We get the template path then show it path = os.path.join(os.path.dirname(__file__), '../views/rss.html') self.response.out.write(template.render(path, template_values))
def get(self, nickname): account = Account.gql("WHERE nickname = :1", nickname).get() messages = Message.gql("WHERE author IN :1 ORDER BY date DESC LIMIT 10", account.following) template_values = { 'title': nickname + '\'s feed', 'link': 'http://pintme.appspot.com/user/' + nickname, 'description': 'Personnal user feed for ' + nickname, 'items': messages } # We get the template path then show it path = os.path.join(os.path.dirname(__file__), '../views/rss.xml') self.response.out.write(template.render(path, template_values))
def log_message(message: discord.Message): user, _ = User.get_or_create(id=message.author.id, defaults={ "username": message.author.name, "discriminator": message.author.discriminator, "nickname": message.author.display_name }) text_channel, _ = TextChannel.get_or_create( id=message.channel.id, defaults={"name": message.channel.name}) msg, _ = Message.get_or_create(id=message.id, defaults={ "author": user, "channel": text_channel, "timestamp": message.created_at, "jump_url": message.jump_url, "was_deleted": False }) embed_json = "" try: for embed in message.embeds: embed_json += f" | Embed: {json.dumps(embed.to_dict())}" except: # todo better handling pass attachment = "" try: attachment = message.attachments[0].proxy_url except IndexError: pass # Ignore, just means we don't have an attachment todo better handling :) # todo Embeds show as an empty message - this needs better handling MessageContent.create( message_id=msg, timestamp=message.edited_at or message.created_at, text=message.clean_content + embed_json, attachment_url=attachment, )
def energetic(message=None) -> Message: if message: return responses.choose_energetic_response(message) else: return Message(sender=user4, content=Content(message="Let's party guys! My treat!"))
def getMessages(self, account, limit = 10): if account: return Message.gql( "WHERE author IN :1 ORDER BY date DESC", account.following ).fetch( limit ) else: return list()
def getAllMessages(self, limit = 10): return Message.gql("ORDER BY date DESC").fetch(limit)
def getSentMessages(self, account, limit = 10): if account: return Message.gql( "WHERE author = :1 ORDER BY date DESC", account.key() ).fetch( limit ) else: return list()
return random.choice(annoying_responses['negative']) def choose_grumpy_response(message: str) -> Message: if 'sleep' in message.lower() or any(reply in message for reply in positive_replies): return random.choice(grumpy_responses['positive']) elif any(reply in message for reply in farewell_replies): return random.choice(grumpy_responses['farewell']) else: return random.choice(grumpy_responses['negative']) sweet_responses = { 'positive': [ Message(sender=user3, content=Content(message="Cooking with you is so much fun!")), Message(sender=user3, content=Content(message="Let's start cooking right away.")), Message(sender=user3, content=Content( message="I can't wait to cook you favorite food!.")), ], 'negative': [ Message(sender=user3, content=Content(message="Whatever. Then I will cook alone.")), Message(sender=user3, content=Content(message="I will cook with the others then.")), Message(sender=user3, content=Content(message="You will starve without me.")), ], 'farewell': [
def _populate_database(): User.objects.create_superuser(username='******', password='******') # Approved farmers for farmer_name in ('Philippe', 'Pierre', 'Agnès'): email = farmer_name + "@farmer.com" User.objects.create_user(farmer_name, email=email, password="******") farmer = Farmer( name=farmer_name, lat=45.1808, lon=1.893, email=email, approved=True, ) farmer.save() # Unapproved farmers User.objects.create_user("Edouard", email="*****@*****.**", password="******") Farmer(name="Edouard", email="*****@*****.**", lat=0.0, lon=0.0, approved=False, phone_number='012345678').save() philippe = Farmer.objects.get(name="Philippe") pierre = Farmer.objects.get(name="Pierre") agnes = Farmer.objects.get(name="Agnès") edouard = Farmer.objects.get(name="Edouard") # Approved experiments for experiment_name in ('Faux semis', 'Allongement', 'Lâchés de trichos'): experiment = Experiment( name=experiment_name, farmer=Farmer.objects.filter(name='Pierre').first(), approved=True, ) experiment.save() Experiment( name="Culture de millet", farmer=Farmer.objects.filter(name='Edouard').first(), approved=True, ).save() # Unapproved experiments Experiment( name="Vente directe", farmer=Farmer.objects.filter(name='Pierre').first(), approved=False, ).save() Experiment( name="Couvert de sarrasin", farmer=Farmer.objects.filter(name='Edouard').first(), approved=False, ).save() # Message exchange between Philippe and Pierre Message( sender=philippe, recipient=pierre, ).save() Message( sender=pierre, recipient=philippe, ).save() Message( sender=philippe, recipient=pierre, ).save() # Message exchange between Agnès and Edouard Message( sender=edouard, recipient=agnes, ).save() Message( sender=agnes, recipient=edouard, ).save() # Single message from Agnès Pierre Message( sender=agnes, recipient=pierre, ).save()
def _populate_database(): # Farmers for farmer_name in ('Philippe', 'Pierre', 'Agnès', 'Marie', 'Jean', 'Sophie'): email = farmer_name + "@example.com" get_user_model().objects.create_user(farmer_name, email=email, password="******") farmer = Farmer( name=farmer_name, lat=45.1808, lon=1.893, email=email, approved=True, can_send_messages=True, ) farmer.save() philippe = Farmer.objects.get(name="Philippe") pierre = Farmer.objects.get(name="Pierre") agnes = Farmer.objects.get(name="Agnès") marie = Farmer.objects.get(name="Marie") jean = Farmer.objects.get(name="Jean") sophie = Farmer.objects.get(name="Sophie") # ----------------------------------- # Message sent from Philippe to Pierre on Jan 1, 2020 Message( sender=philippe, recipient=pierre, subject="Hello Pierre", body=":)", sent_at=datetime.datetime(2020, 1, 1, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save() # ----------------------------------- # Message sent from Philippe to Agnès on Jan 1, 2020 Message( sender=philippe, recipient=agnes, subject="Hello Agnès", body=":)", sent_at=datetime.datetime(2020, 1, 1, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save() # ----------------------------------- # Conversation between Philippe and Jean (1, 2 and 3 Jan, 2020) Message( sender=philippe, recipient=jean, subject="Hello Jean", body=":)", sent_at=datetime.datetime(2020, 1, 1, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save() Message( sender=jean, recipient=philippe, subject="Hello Philippe", body=":)", sent_at=datetime.datetime(2020, 1, 2, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save() Message( sender=philippe, recipient=jean, subject="Hello Jean", body=":)", sent_at=datetime.datetime(2020, 1, 3, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save() # ----------------------------------- # Message sent from Philippe to Marie on Jan 10, 2020 Message( sender=philippe, recipient=marie, subject="Hello Marie", body=":)", sent_at=datetime.datetime(2020, 1, 10, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save() # ----------------------------------- # Message sent from Philippe to Sophie on Jan 1, 2020. Sophie sees it the next day. Message( sender=philippe, recipient=sophie, subject="Hello Sophie", body=":)", sent_at=datetime.datetime(2020, 1, 1, 1, tzinfo=pytz.timezone('Europe/Paris')), read_at=datetime.datetime(2020, 1, 2, 1, tzinfo=pytz.timezone('Europe/Paris')), ).save()
def log_message_deleted(message: discord.Message): msg = Message.get_by_id(message.id) msg.was_deleted = True msg.save()