rveachkc / pymsteams

Format messages and post to Microsoft Teams.
https://pypi.org/project/pymsteams/
Apache License 2.0
424 stars 78 forks source link

How to add data from a list of objects? #79

Closed mahima2112 closed 4 years ago

mahima2112 commented 4 years ago

I have a list of objects getting data using an SQL query. Say contentList[] has the data and column names are "content text", "updated on", "trigger". How can I display this data using pymsteams if I pass the contentList[] in parameter of the function?

rveachkc commented 4 years ago

This is more of a general programming question than a pymsteams question, but you will essentially need to decide what kind of card element you want to use, then format your data to strings.

If you only had two items, the card section facts might be a good fit.

myMessageSection = pymsteams.cardsection()
[ myMessageSection.addFact(x.key, x.value) for x in contentList]

If you have three or more fields, you may want create a new card section for each.


def create_card_section(text, updated, trigger):
    section = pymsteams.cardsection()
    section.activityTitle("Updated on {}".format(updated.strftime("%Y-%m-%d")))
    section.activitySubtitle(trigger)
    section.activityText(text)
    return section

card = pymsteams.connectorcard("<Microsoft Webhook URL>")
[card.addSection(create_card_section(x.text, x.updated, x.trigger)) for x in contentList]

Note: I assumed a list of named tuples for your data types. This pseudo code is untested and may or may not work.

Since this isn't really related to an issue with pymsteams, I'm going to close it.