Tworzenie i zarządzanie opcjami głosowania

Microsoft Outlook umożliwia użytkownikom tworzenie ankiety podczas komponowania nowego e-maila, pozwalając na dodanie opcji głosowania takich jak Tak, Nie, Może i inne. Podobnie, Aspose.Email zapewnia funkcję tworzenia ankiet przy programowym generowaniu nowej wiadomości Outlook. FollowUpOptions klasa zawiera właściwość voting_buttons, która może być użyta do ustawienia lub pobrania żądanych opcji głosowania.

W tym artykule przyjrzymy się szczegółowemu przykładowi, jak stworzyć MapiMessage z opcjami głosowania, aby skutecznie ustawić ankietę.

Utwórz ankietę używając MapiMessage

Aby utworzyć ankietę, użyj właściwości voting_buttons obiektu FollowUpOptions klasy, jak pokazano w poniższym przykładzie kodu:

import aspose.email as ae

eml = ae.MailMessage("from@domain.com", "to@domain.com", "Poll", "Please vote")
msg = ae.mapi.MapiMessage.from_mail_message(eml)

# Set the voting buttons
options = ae.mapi.FollowUpOptions()
options.voting_buttons = "Yes;No;Maybe;Exactly!"
ae.mapi.FollowUpManager.set_options(msg, options)

msg.save("voting_btns.msg")

Odczytaj opcje głosowania z MapiMessages

Poniższy fragment kodu pokazuje, jak odczytać opcje głosowania z MapiMessage.

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "voting_btns.msg")

# This method is useful when, besides the voting buttons, other parameters
# (a category, for example) have to be read as well
options = FollowUpManager.get_options(message)

# The voting buttons are returned as a single string with a semicolon as a separator
print(options.voting_buttons)

Odczytaj tylko przyciski głosowania

Aby odczytać tylko przyciski głosowania, rozważ poniższy fragment kodu:

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "voting_btns.msg")

buttons = FollowUpManager.get_voting_buttons(message)
for button in buttons:
    print(button)

Dodaj przyciski głosowania do istniejących wiadomości

Poniższy fragment kodu pokazuje, jak dodać przycisk głosowania do istniejącej wiadomości:

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "message.msg")

FollowUpManager.add_voting_button(message, "Indeed!")

message.save(data_dir + "AddVotingButtonToExistingMessage_out.msg")

Usuń przyciski głosowania z wiadomości

Poniższy fragment kodu pokazuje, jak usunąć przycisk głosowania z wiadomości:

from aspose.email.mapi import MapiMessage, FollowUpManager

message = MapiMessage.load(data_dir + "voting_btns.msg")

# Delete a single button
FollowUpManager.remove_voting_button(message, "Exactly!")

# Or delete all the buttons of the message
FollowUpManager.clear_voting_buttons(message)

message.save(data_dir + "MapiMsgWithoutPoll_out.msg")

Odczytaj informacje o wynikach głosowania

Poniższy fragment kodu pokazuje, jak odczytać informacje o wynikach głosowania:

from aspose.email.mapi import MapiMessage, MapiPropertyTag

msg = MapiMessage.load(data_dir + "voting_btns.msg")

for recipient in msg.recipients:
    print(f"Recipient: {recipient.display_name}")

    # Get the PR_RECIPIENT_AUTORESPONSE_PROP_RESPONSE property
    if recipient.properties.contains_key(MapiPropertyTag.RECIPIENT_AUTORESPONSE_PROP_RESPONSE):
        response = recipient.properties[MapiPropertyTag.RECIPIENT_AUTORESPONSE_PROP_RESPONSE]
        print(f"Response: {response.get_string()}")

    # Get the PR_RECIPIENT_TRACKSTATUS_TIME property
    if recipient.properties.contains_key(MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME):
        response_time = recipient.properties[MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME]
        print(f"Response time: {response_time.get_date_time()}")

Pobierz informacje o reakcjach z MSG

Ten get_reactions metoda FollowUpManager klasa pozwala pobrać listę reakcji pozostawionych na wiadomości MAPI, co ułatwia analizę zaangażowania użytkowników. Każdy element zwróconej listy jest UserReaction obiekt udostępniający name, email, type i reaction_date_time właściwości. Rodzaj reakcji jest wartością ReactionType enumeracji: LIKE, HEART, CELEBRATE, LAUGH, SURPRISED, SAD lub UNKNOWN.

Poniższy przykład kodu demonstruje, jak pobrać dostępne reakcje dla określonej wiadomości:

from aspose.email.mapi import MapiMessage, FollowUpManager

# Load the message file
msg = MapiMessage.load("message.msg")

# Retrieve the list of reactions on the message
reactions = FollowUpManager.get_reactions(msg)

# Iterate through each reaction and output the details
for reaction in reactions:
    print(f"User: {reaction.name}, Email: {reaction.email}, "
          f"Reaction: {reaction.type}, Date: {reaction.reaction_date_time}")