yoshiislandblog.net
元営業の駆け出しアラサーSEが、休日にMACと戯れた際の殴り書きメモ。日々勉強。日々進歩。

この記事は3年以上前に書かれた記事で内容が古い可能性があります

Office365のOutlook APIをpythonで叩いてSlackに本日と翌営業日の予定をPostしようとしてみる(5)本日と翌営業日の予定をPostしようとしてみる

2019-06-09

フルコードはこちら
https://github.com/yoshi-island/o365_slack_work

前回の記事はこちら

本日と翌営業日の予定をPostしようとしてみる

ごめんなさい、まだSlack Botレビュー中なので、Slackにポストはまだできていない
でもこの通りにやればできるはず

わかりやすいように、一旦ポストする内容をprintするようにしています

python-slackclientを使うので、
python-slackclientをpipでインストール

% pip install slackclient

同じ階層の先ほど作成したpassword_list.pyの内容に
先ほど控えたslackのtokenと、ポストしたいチャンネルのIDを記載
(チャンネルIDはブラウザでSlackチャンネルにアクセスした際のURLに記載されている)

https://[workspace].slack.com/messages/[channel id]/
% cat password_list.py
# azure
client_id = '[client id]'
client_secret = '[client secret]'

# slack
token = '[oauth token]'
channel = '[channel id]'

同じ階層に以下のポストするこのようなファイルを作成する
汎用的でなくてすみませんが、以下の変数は自身の環境に合わせて書き換えとく

calName = 'Calendar'  #予定表
calName2 = 'Tasks'  #タスク用予定表
% cat get_calender_post_slack_test.py
# temp: to avoid ssl error
import urllib3
urllib3.disable_warnings()

import datetime
from slackclient import SlackClient
import password_list
import json


# import python-o365
import sys
sys.path.insert(0, './python-o365')
from O365 import Account

# outlook
client_id = password_list.client_id
client_secret = password_list.client_secret
account = Account((client_id, client_secret))
# slack
token = password_list.token
channel = password_list.channel


def getOlcalEventsAllday(account,calName,calDay):
  if not account.is_authenticated:
      account.authenticate(scopes=['basic', 'calendar_all'])

  schedule = account.schedule()
  calendar = schedule.list_calendars()

  calendar = schedule.get_calendar(calendar_name=calName)
  q = calendar.new_query('start').greater_equal(calDay + ' 00:00:00')
  q.chain('and').on_attribute('end').less_equal(calDay + ' 23:59:59')

  cal = calendar.get_events(query=q, order_by='end/dateTime', include_recurring=True)  # include_recurring=True will include repeated events on the result set.

  return cal


def postSlack(token, channel, msg):
  slack_token = token
  sc = SlackClient(slack_token)
  sc.api_call(
    "chat.postMessage",
    channel=channel,
    #text="<@channel>" + msg1,
    text=msg,
    as_user=True
  )


def postSlackOlEventsAllday(calDay, account, calName, token, channel):
  msg = '`' + calDay + '` \n```\n'
  for l in getOlcalEventsAllday(account,calName,calDay):
    msg = msg + str(l).replace('Subject: ', '* ') + '\n'

  msg = msg + '```'

  # temp
  print(msg)

  postSlack(token, channel, msg)



if __name__ == "__main__":
  # get Events
  calName = 'Calendar' #予定表
  calNameFmt = '*' + calName + '*'
  postSlack(token, channel, calNameFmt)

  ## get today events
  calDay = str(datetime.date.today()) #'%Y-%m-%d'
  postSlackOlEventsAllday(calDay, account, calName, token, channel)

  ## get next business day events
  if datetime.date.today().weekday() <  4:
    calDay2 = str(datetime.date.today() + datetime.timedelta(days=1))
  elif datetime.date.today().weekday() == 4:
    calDay2 = str(datetime.date.today() + datetime.timedelta(days=3))
  elif datetime.date.today().weekday() == 5:
    calDay2 = str(datetime.date.today() + datetime.timedelta(days=2))
  else:
    calDay2 = str(datetime.date.today() + datetime.timedelta(days=1))
  postSlackOlEventsAllday(calDay2, account, calName, token, channel)

  # get tasks
  calName2 = 'Tasks' #タスク用予定表
  calNameFmt2 = '*' + calName2 + '*'
  postSlack(token, channel, calNameFmt2)

  ## get today tasks
  postSlackOlEventsAllday(calDay, account, calName2, token, channel)

  ## get next business day tasks
  postSlackOlEventsAllday(calDay2, account, calName2, token, channel)

これで実行すると、本日の予定と、次の出勤日(月〜金)の予定がPostされる
いつの予定を出力したいかは「## get next business day events」を操作してくださいませ。

まだBotが動かないので、一旦print結果でご容赦、、

土曜日の場合は当日と翌月曜の予定が記載される

(office_slack_work) [yoshi@19-06-08T14:16:55] ~/office_slack_work
% date
2019年 6月 8日 土曜日 14時16分56秒 JST
(office_slack_work) [yoshi@19-06-08T14:16:56] ~/office_slack_work
% python get_calender_post_slack_test.py
`2019-06-08` 
```
* Gym (on: 2019-06-08 from: 10:00:00 to: 12:00:00)
* Dinner (on: 2019-06-08 from: 18:00:00 to: 21:00:00)
```
`2019-06-10`
```
* Team Meeting (on: 2019-06-10 from: 10:00:00 to: 11:00:00)
* Project Meeting (on: 2019-06-10 from: 16:00:00 to: 17:00:00)
```
`2019-06-08`
```
* Reading book (on: 2019-06-08 from: 15:00:00 to: 16:30:00)
```
`2019-06-10`
```
* Read Newspaper (on: 2019-06-10 from: 09:00:00 to: 09:30:00)
* Paper work (on: 2019-06-10 from: 13:00:00 to: 15:00:00)
* Daily Report (on: 2019-06-10 from: 19:00:00 to: 19:30:00)
```
(office_slack_work) [yoshi@19-06-08T14:17:11] ~/office_slack_work
%

日〜木曜日の場合は当日と翌日の予定が記載される

(office_slack_work) [yoshi@19-06-08T14:17:13] ~/office_slack_work
% date
2019年 6月 9日 日曜日 14時17分18秒 JST
(office_slack_work) [yoshi@19-06-09T14:17:18] ~/office_slack_work
% python get_calender_post_slack_test.py
`2019-06-09`
```
* Hair salon (on: 2019-06-09 from: 10:00:00 to: 12:00:00)
* Party (on: 2019-06-09 from: 20:00:00 to: 23:00:00)
```
`2019-06-10`
```
* Team Meeting (on: 2019-06-10 from: 10:00:00 to: 11:00:00)
* Project Meeting (on: 2019-06-10 from: 16:00:00 to: 17:00:00)
```
`2019-06-09`
```
* Gardening (on: 2019-06-09 from: 13:00:00 to: 15:00:00)
* Prep for party (on: 2019-06-09 from: 17:00:00 to: 19:00:00)
```
`2019-06-10`
```
* Read Newspaper (on: 2019-06-10 from: 09:00:00 to: 09:30:00)
* Paper work (on: 2019-06-10 from: 13:00:00 to: 15:00:00)
* Daily Report (on: 2019-06-10 from: 19:00:00 to: 19:30:00)
```
(office_slack_work) [yoshi@19-06-09T14:17:31] ~/office_slack_work
%
(office_slack_work) [yoshi@19-06-10T14:18:13] ~/office_slack_work
% date
2019年 6月10日 月曜日 14時18分17秒 JST
(office_slack_work) [yoshi@19-06-10T14:18:17] ~/office_slack_work
% python get_calender_post_slack_test.py
`2019-06-10`
```
* Team Meeting (on: 2019-06-10 from: 10:00:00 to: 11:00:00)
* Project Meeting (on: 2019-06-10 from: 16:00:00 to: 17:00:00)
```
`2019-06-11`
```
* Code Review (on: 2019-06-11 from: 10:00:00 to: 12:00:00)
* Appointment with B Corp. (on: 2019-06-11 from: 14:00:00 to: 15:00:00)
* Yoga (on: 2019-06-12 from: 07:00:00 to: 08:00:00)
```
`2019-06-10`
```
* Read Newspaper (on: 2019-06-10 from: 09:00:00 to: 09:30:00)
* Paper work (on: 2019-06-10 from: 13:00:00 to: 15:00:00)
* Daily Report (on: 2019-06-10 from: 19:00:00 to: 19:30:00)
```
`2019-06-11`
```
* Read Newspaper (on: 2019-06-11 from: 09:00:00 to: 09:30:00)
* Quarter Target Planning (on: 2019-06-11 from: 16:00:00 to: 18:00:00)
* Daily Report (on: 2019-06-11 from: 19:00:00 to: 19:30:00)
```
(office_slack_work) [yoshi@19-06-10T14:18:27] ~/office_slack_work
%
(office_slack_work) [yoshi@19-06-10T14:18:34] ~/office_slack_work
% date
2019年 6月12日 水曜日 14時18分38秒 JST
(office_slack_work) [yoshi@19-06-12T14:18:38] ~/office_slack_work
% python get_calender_post_slack_test.py
`2019-06-12`
```
* Code Review (on: 2019-06-12 from: 10:00:00 to: 12:00:00)
* Appointment with A corp. (on: 2019-06-12 from: 14:00:00 to: 15:00:00)
* Dinner with Friends (on: 2019-06-12 from: 20:00:00 to: 22:00:00)
```
`2019-06-13`
```
* Conference (on: 2019-06-13 from: 10:00:00 to: 18:00:00)
```
`2019-06-12`
```
* Read Newspaper (on: 2019-06-12 from: 09:00:00 to: 09:30:00)
* Prepare for Conference (on: 2019-06-12 from: 16:00:00 to: 18:00:00)
* Daily Report (on: 2019-06-12 from: 19:00:00 to: 19:30:00)
```
`2019-06-13`
```
* Read Newspaper (on: 2019-06-13 from: 09:00:00 to: 09:30:00)
* Daily Report (on: 2019-06-13 from: 19:00:00 to: 19:30:00)
```
(office_slack_work) [yoshi@19-06-12T14:18:50] ~/office_slack_work
%

金曜日の場合は当日と翌月曜の予定が記載される

(office_slack_work) [yoshi@19-06-14T14:19:18] ~/office_slack_work
% date
2019年 6月14日 金曜日 14時19分19秒 JST
(office_slack_work) [yoshi@19-06-14T14:19:19] ~/office_slack_work
% python get_calender_post_slack_test.py
`2019-06-14`
```
* Lunch on Meeting (on: 2019-06-14 from: 12:00:00 to: 13:00:00)
* Team Meeting (on: 2019-06-14 from: 16:00:00 to: 18:00:00)
* Dating (on: 2019-06-14 from: 20:00:00 to: 22:00:00)
```
`2019-06-17`
```
* Team Meeting (on: 2019-06-17 from: 10:00:00 to: 11:00:00)
```
`2019-06-14`
```
* Read Newspaper (on: 2019-06-14 from: 09:00:00 to: 09:30:00)
* Paper work (on: 2019-06-14 from: 10:00:00 to: 11:00:00)
* Daily Report (on: 2019-06-14 from: 19:00:00 to: 19:30:00)
```
`2019-06-17`
```
* Read Newspaper (on: 2019-06-17 from: 09:00:00 to: 09:30:00)
* Paper work (on: 2019-06-17 from: 13:00:00 to: 15:00:00)
* Daily Report (on: 2019-06-17 from: 19:00:00 to: 19:30:00)
```
(office_slack_work) [yoshi@19-06-14T14:19:31] ~/office_slack_work
%

うん、とりあえず必要な情報取れてる。

Trouble Shooting

Slack Botの投稿がうまくいかない場合は、Slack Distributionのチェックが全て付いていないかも
ちょっと面倒くさいけど、全てがチェックつくように規約確認したり、Botの説明を記載したり頑張ってください。