科学の箱

科学・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

seleniumでWebElement object is not iterableが出るときの対処方法

iterableなオブジェクトを想定して要素を取得、forループに取り込むと下記のようなWebElement object is not iterableが出る。 結論としては勘違い。 Seleniu …

no image

グラフの軸を操作する

概要 ここでは以下の項目について取り扱う 軸にラベルを設定する ー xlabel, ylabel 軸の最小値、最大値を設定する ー xlim, ylim, xaxis 軸にメモリを設定する ー xti …

no image

automated the boring – day 2

今日は簡単なUser認証のプロトタイプといえば恰好はよいが、 パスワードの入力 パスワードの突き合せ パスワードつきあわせの結果によりメッセージを変える name = ‘Mary’ password …

no image

kaggle Titanic Tutorial – 10

いろいろ試しているがうまくいかないので、とりあえずAgeを正しく補完できるか調べる。 調査は線形回帰でどれくらい相関が出るかで判断する。   import numpy as nm impor …

no image

numpyで配列を抜き出す方法

numpyで配列を抜き出す まず基本となるやり方 arr = np.arange(50).reshape(5,10) arr[1:1,] arr[1:2,]   np.arange(50)で …

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

side bar top



アーカイブ

カテゴリー