A daily random XKCD in Teams
- benhaguenz
- May 11, 2021
- 2 min read
This builds on the previous post to get a random post rather than just the latest. That post can be found here: https://benhaguenz.wixsite.com/my-site/post/automatically-posting-xkcd-in-teams
The first step is to modify the custom connector so that rather than just a straight GET, it does a GET with a parameter called ComicId.
swagger: '2.0'
info: {title: XKCD, description: Random XKCD, version: '1.0'}
host: xkcd.com
basePath: /
schemes: [https]
consumes: []
produces: []
paths:
/info.0.json:
get:
produces: [application/json]
responses:
'200':
description: OK
schema:
type: object
properties:
month: {type: string, description: month}
num: {type: integer, format: int32, description: num}
link: {type: string, description: link}
year: {type: string, description: year}
news: {type: string, description: news}
safe_title: {type: string, description: safe_title}
transcript: {type: string, description: transcript}
alt: {type: string, description: alt}
img: {type: string, description: img}
title: {type: string, description: title}
day: {type: string, description: day}
summary: Get latest image
operationId: GetLatest
parameters: []
description: Gets the latest XKCD comic
/{comicId}/info.0.json:
get:
produces: [application/json]
responses:
'200':
description: OK
schema:
type: object
properties:
month: {type: string, description: month}
num: {type: integer, format: int32, description: num}
link: {type: string, description: link}
year: {type: string, description: year}
news: {type: string, description: news}
safe_title: {type: string, description: safe_title}
transcript: {type: string, description: transcript}
alt: {type: string, description: alt}
img: {type: string, description: img}
title: {type: string, description: title}
day: {type: string, description: day}
summary: Get a specified image
operationId: GetSpecific
parameters:
- {in: path, name: comicId, type: integer, required: true, description: Numeric
ID of the comic to get}
description: Gets a specific XKCD comic
definitions: {}
parameters: {}
responses: {}
securityDefinitions: {}
security: []
tags: []
This will be a recurring flow, running at 9am everyday.

First we call the custom connector to get the latest cartoon

And then once we have that we can call the function to get a specific one with a random number between 1 and the latest.

You can't see the full function there, but it's:
rand(1,outputs('Get_latest_image')?['body/num'])
Once we've got a random cartoon we can display it:

I have made some minor changes from the first version to get what I think is a better looking output:
{
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"body": [
{
"type": "TextBlock",
"text": "XKCD @{outputs('Get_a_specified_image')?['body/num']} - @{outputs('Get_a_specified_image')?['body/safe_title']}",
"wrap": true
},
{
"type": "Image",
"url": "@{outputs('Get_a_specified_image')?['body/img']}"
},
{
"type": "TextBlock",
"text": "@{outputs('Get_a_specified_image')?['body/alt']}",
"wrap": true
},
{
"type": "TextBlock",
"text": "[Go to cartoon](https://xkcd.com/@{outputs('Get_a_specified_image')?['body/num']}/)",
"wrap": true
}
]
}
The only new feature is the use of the hyperlink formatting [] and ().
While this does work in general there is an issue if the returned strings such as alt contain double quotes as the text field seem to remove the escaping. I think this a bug with the action which is still in preview. Hopefully this will be fixed.
Comments