A little Slack app to ask our team social questions
Tags: showdev, webdev, php, programming
Published: 2021-06-10 09:10:46

## Changes coming from the pandemic

A lot has changed since the pandemic started and we all can learn a lot from the past 18 months. Because of COVID-19, we are now a remote workplace. Which is very nice, but also brings some challenges.

For example, some people from our team have never met in person. While working remotely, you do not occasionally run into someone and have to talk to him*her. The social talk decreased a lot. While it is okay for some, others want it back. How can we counteract this?

## A little idea

We do not want to bring the office back and we do not want to add a lot of distraction. We just want to get to know each other a bit better. So we developed an app, which asks our team a social question every Monday at 1 pm. Those questions are posted to an off-topic channel, which is optional for everyone to read.

## Questions to ask

We gathered questions and put those into a JSON file. Sorry for using German here, but those are some real questions our team was asked in the last months.

```json
[
    ":palm_tree: An welchem Ort machst du am liebsten Urlaub? Vielleicht kannst du ja auch ein Foto teilen.",
    ":tv: Was ist deine Lieblingsserie? Und wo können die anderen diese Serie gucken?",
    ":green_salad: Welches Essen ist so gar nichts für dich?",
    ":musical_note: Welche Musik hörst du, wenn du einfach nur abschalten willst?",
    ":frame_with_picture: Wie hast du als Kind ausgesehen?",
    ":moneybag: Was meinst du? Wird Bitcoin die Währung der Zukunft?",
    ":children_crossing: Was ist deine schlimmste Jugendsünde?",
    ":movie_camera: Was ist dein Lieblingskinderfilm?",
    ":tropical_drink: Zitronen- oder Pfirsich-Eistee?",
    ":desert_island: Welche drei Dinge würdest du mit auf eine einsame Insel nehmen?",
    ":bread: Der, die oder das Nutella?",
    ":icecream: Was ist deine Lieblingseissorte und warum?",
    "[…]"
]
```

## Source code

The app is just a PHP script, which is run by a cronjob once a week. It works like this:

1. Get a question from `questions.json`.
2. Post the question using the [chat.postMessage](https://api.slack.com/methods/chat.postMessage) API endpoint.
3. Add the question to `questions-archive.json`.
4. Remove the question from `questions.json`.

```php
#!/usr/local/bin/php
<?php

// Abort, if not in CLI mode
if (php_sapi_name() !== 'cli') {
  echo 'Not in CLI mode';
  echo "\n";
  die();
}

// Prepare some variables
$archivedQuestionsFilename = 'questions-archive.json';
$questionsFilename = 'questions.json';

// Read questions from file
$questions = json_decode(file_get_contents($questionsFilename));

// Abort, if no questions are left
if (count($questions) === 0) {
  echo 'No questions left in ' . $questionsFilename;
  echo "\n";
  die();
}

// Send question via cURL to Slack
$curlHandle = curl_init();
$sendQuestionUrl = 'https://slack.com/api/chat.postMessage';
$sendQuestionParameters = [
  'channel' => 'C03C9RPDF',
  'text' => 'Hallo Freund*innen der gepflegten Unterhaltung.

' . $questions[0],
];
$sendQuestionBearerToken = '0123456789ABCDEF';

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ['Content-Type: application/json' , 'Authorization: Bearer ' . $sendQuestionBearerToken]);
curl_setopt($curlHandle, CURLOPT_URL, $sendQuestionUrl);
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, json_encode($sendQuestionParameters));
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$sendQuestionResponse = curl_exec($curlHandle);
curl_close ($curlHandle);

// Read archived questions from file
$archivedQuestions = json_decode(file_get_contents($archivedQuestionsFilename));

// Add question to archived questions
$archivedQuestions[] = $questions[0];

// Remove question from questions
unset($questions[0]);

// Persist archived questions
file_put_contents($archivedQuestionsFilename, json_encode(array_values($archivedQuestions)));

// Persist questions
file_put_contents($questionsFilename, json_encode(array_values($questions)));
```