科学の箱

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

Numpyまとめ

環境及びインポート numpyのインポートおよび環境確認 配列生成 配列をリストから生成 配列の属性を確認 すべての要素が同じ値を持つ配列を生成 空の配列を生成 numpy.linspace()を使っ …

no image

連続データのビジュアル

Kaggle TitanicのFareを使っていくつかビジュアル 金額別ヒストグラム-1 titanic_df[‘Fare’].plot(kind=’hist’, figsize=(15,3),bin …

no image

automated the boring – day9

https://automatetheboringstuff.com/chapter17/ >>> from PIL import Image &gt …

no image

condaでjupyter notebookが使えないとき

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

no image

python virtualenvの使い方

pip3 install virtualenv virtualenv myenv myenv/Scripts/activate Related posts:タイタニックデータでEDAkaggle Ti …

2018年9月
« 6月   10月 »
 12
3456789
10111213141516
17181920212223
24252627282930

side bar top



アーカイブ

カテゴリー