Skip to content

Instantly share code, notes, and snippets.

@kujirahand
Created June 4, 2022 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kujirahand/e76867c0bd06aec9acf079ec6133f0bf to your computer and use it in GitHub Desktop.
Save kujirahand/e76867c0bd06aec9acf079ec6133f0bf to your computer and use it in GitHub Desktop.
Slackのスラッシュコマンド - 数値を合計する/sumコマンド
#!/usr/local/bin/python3.7
# ↑ Pythoのパスは書き換えが必要
# ライブラリの取り込み --- (*1)
import os, sys, io, cgi
sys.stdin = open(sys.stdin.fileno(), 'r', encoding='UTF-8')
sys.stdout = open(sys.stdout.fileno(), 'w', encoding='UTF-8')
sys.stderr = open(sys.stderr.fileno(), 'w', encoding='UTF-8')
out = lambda msg: print(msg, end="\r\n")
# /calc に続く文字列を得る --- (*2)
form = cgi.FieldStorage()
text = 'print("[使い方] /sum 1,2,3...")'
if 'text' in form: text = form['text'].value
try:
total = 0
for s in text.split(','):
total += float(s.strip())
except Exception as e:
result = 'error' + str(e)
# ヘッダとコンテンツを出力 --- (*3)
out("Content-Type: text/html; charset=utf-8");
out("") # ヘッダと本文は空行で区切るのが決まり
out("合計: " + str(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment