-
-
Save kujirahand/e76867c0bd06aec9acf079ec6133f0bf to your computer and use it in GitHub Desktop.
Slackのスラッシュコマンド - 数値を合計する/sumコマンド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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