การสร้างและจัดการตัวเลือกการโหวต

Microsoft Outlook อนุญาตให้ผู้ใช้สร้างโพลเมื่อร่างอีเมลใหม่ สามารถรวมตัวเลือกการโหวตเช่น ใช่, ไม่ใช่, อาจจะ และอื่น ๆ ในทำนองเดียวกัน Aspose.Email มีฟังก์ชันการสร้างโพลเมื่อสร้างข้อความ Outlook ใหม่โดยอัตโนมัติ. FollowUpOptions คลาสนี้รวมคุณสมบัติ voting_buttons ซึ่งสามารถใช้ตั้งหรือดึงตัวเลือกการโหวตที่ต้องการ.

ในบทความนี้ เราจะสำรวจตัวอย่างละเอียดของวิธีการสร้าง MapiMessage พร้อมตัวเลือกการโหวตเพื่อจัดตั้งโพลอย่างมีประสิทธิภาพ.

สร้างโพลโดยใช้ MapiMessage

เพื่อสร้างโพล ใช้คุณสมบัติ voting_buttons ของ FollowUpOptions คลาสตามที่แสดงในตัวอย่างโค้ดด้านล่าง:

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")

อ่านตัวเลือกการโหวตจาก MapiMessages

โค้ดส่วนต่อไปนี้แสดงวิธีอ่านตัวเลือกการโหวตจาก 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)

อ่านเฉพาะปุ่มโหวต

หากต้องการอ่านเฉพาะปุ่มโหวต ให้พิจารณาโค้ดตัวอย่างต่อไปนี้:

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)

เพิ่มปุ่มโหวตในข้อความที่มีอยู่

โค้ดสแนปช็อตต่อไปนี้แสดงวิธีเพิ่มปุ่มโหวตให้กับข้อความที่มีอยู่:

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")

ลบปุ่มโหวตจากข้อความ

โค้ดส่วนต่อไปนี้แสดงวิธีลบปุ่มโหวตออกจากข้อความ:

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")

อ่านข้อมูลผลลัพธ์การโหวต

โค้ดส่วนต่อไปนี้แสดงวิธีอ่านข้อมูลผลลัพธ์การโหวต:

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()}")

ดึงข้อมูลการตอบสนองจาก MSG

นี้ get_reactions เมธอดของ FollowUpManager คลาสนี้ให้คุณดึงรายการการตอบสนองที่ผู้ใช้ทิ้งไว้บนข้อความ MAPI ซึ่งทำให้วิเคราะห์การมีส่วนร่วมของผู้ใช้ได้ง่าย รายการที่คืนค่าจะเป็น UserReaction อ็อบเจกต์ที่เปิดเผย name, email, type และ reaction_date_time คุณสมบัติ ประเภทการตอบสนองเป็นค่า ของ ReactionType enumeration: LIKE, HEART, CELEBRATE, LAUGH, SURPRISED, SAD หรือ UNKNOWN.

ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีดึงการตอบสนองที่มีอยู่สำหรับข้อความที่ระบุ:

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}")