科学の箱

科学・IT・登山の話題

Python

Python + Slack Bot – 2

投稿日:

Slackのチャネルに投稿するにはChanel IDが必要。

Chanel IDは下記から取得できる。

https://api.slack.com/methods/channels.list/test

しかしテストをするためにはLegacy Tokenが必要。

これは下記から生成できる。

 

 

サンプルコードはここから

https://python-slackclient.readthedocs.io/en/1.0.2/

とりあえずチャネルにメッセージを送るコード


from slackclient import SlackClient

token = "xoxp-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # found at https://api.slack.com/web#authentication
sc = SlackClient(token)
sc.api_call("api.test")
sc.api_call("channels.info", channel="CHANEL ID HERE")
sc.api_call(
   "chat.postMessage", channel="#starterbot-test", text="Hello from Python! :tada:",
   username='pybot', icon_emoji=':robot_face:'
)

 

次にリアルタイムでメッセージを送るコード


import time
from slackclient import SlackClient

token = "xoxp-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"# found at https://api.slack.com/web#authentication
sc = SlackClient(token)
if sc.rtm_connect():
  while True:
    print(sc.rtm_read())
    time.sleep(1)
else:
  print("Connection Failed, invalid token?")

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

errorbarで誤差棒付きグラフの作成

概要 学習モデルのグラフは誤差を含んでいる。今2軸(xおよびy)をとり、以下のようなモデルに近似したとする。実際のデータは誤差がある。この誤差を表示できるのがerrorbarである。 ドキュメント h …

no image

タイタニックデータでEDA-2

前回は、タイタニックデータについてはビジュアライズしてデータについて理解を深めた。 今回はデータをいじって機械学習に使えるようにする。 機械学習をするために必要な処理は3つある。 null値の置換 余 …

no image

automated the boring – day7

https://automatetheboringstuff.com/chapter14/ 14章ではcsvとJSONを取り扱う。フォーマットとしては単純であるのに、なぜexcel,word, pdf …

no image

condaでjupyter notebookが使えないとき

condaで環境を作るとjupyter notebookはインストールされていない。 この場合には個別にjupyterをインストールすればよい。   conda install jupyte …

no image

pythonで地理情報を取り扱う

pythonでデータ分析をして地理情報に表示したいときに使うパッケージはplotlyである。 plotlyのchropleth map(階級区分図)を利用する。 plotlyについてはこちらでサンプル …

2018年9月
月 火 水 木 金 土 日
« 6月   10月 »
 12
3456789
10111213141516
17181920212223
24252627282930

side bar top



アーカイブ

カテゴリー