Check stock prices with slackbot using python

Check the stock price on slack. It's very convenient. (Here was very helpful)

スクリーンショット 2017-08-16 15.19.35.png ## 1. Make a slackbot

https://my.slack.com/services/new/bot

2. Install the python library

$ pip install slackbot
$ pip install jsm

3. Create a directory for slackbot

slackbot         #A directory that organizes programs. Any name is fine
├─ run.py        #Start the bot by running this program
├─ slackbot_settings.py   #File to write settings related to bot
└─ plugins                #Add bot functionality to this directory
   ├─ __init__.py         #A file to indicate the module. Sky is fine
   └─ my_mention.py       #Features each file. Any name is fine
$ touch run.py slackbot_settings.py
$ mkdir plugins
$ cd plugins
$ touch __init__.py my_mention.py

4. Create a script

run.py


# coding: utf-8

from slackbot.bot import Bot

def main():
    bot = Bot()
    bot.run()

if __name__ == "__main__":
    print('start slackbot')
    main()

slackbot_settings.py


# coding: utf-8

#Specify token for bot account
API_TOKEN = "xxxx-xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx"

#Response string when a message addressed to this bot does not apply to any response
DEFAULT_REPLY = "Ah, ah ..."

#List of subdirectory names where plugin scripts are located
PLUGINS = ['plugins']

my_mention.py


# coding: utf-8

from slackbot.bot import respond_to     # @botname:Decoder that reacts with
from slackbot.bot import listen_to      #Decoder that responds to in-channel remarks
from slackbot.bot import default_reply  #Decoder that reacts when there is no corresponding response

# @respond_to('string')Message to bot
#string can be a regular expression "r'string'」
# @listen_to('string')Posts other than to bots in the channel
#                           @botname:Note that it does not react
#React when mentioning to others
#Regular expression possible
# @default_reply()          DEFAULT_Same function as REPLY
#If you specify a regular expression, it will not hit other decoders,
#Reacts when matching a regular expression
#... But is it an error if I specify a regular expression?

# message.reply('string')   @Speaker name:Send message with string
# message.send('string')Send string
# message.react('icon_emoji')Reaction to the speaker's message(stamp)To do
#In the string':'I don't need
@respond_to('good job')
def mention_func(message):
    message.reply('It's Otsu') #Mention
    message.react('+1')

@listen_to('python')
def listen_func(message):
    message.send('python shuki')      #Just a post

@listen_to('mac')
def listen_func(message):
    message.send('mac is good ~')      #Just a post

@listen_to('win')
def listen_func(message):
    message.send('I hate windows')      #Just a post

@respond_to('Furukawa')
def listen_func(message):
    import jsm
    q = jsm.Quotes()
    price = q.get_price(5801).close # furukawa
    message.send("The stock price is"+str(price)+"It's a circle")

@respond_to('Stock price')
def listen_func(message):
    import jsm
    q = jsm.Quotes()
    price = q.get_price(5801).close
    message.send("Furukawa Electric{0} ({1})".format(price, price-2990))
    price = q.get_price(3382).close
    message.send("7&iHD {0} ({1})".format(price, price-4450))
    price = q.get_price(5401).close
    message.send("Nippon Steel & Sumitomo Metal{0} ({1})".format(price, price-2633))
    price = q.get_price(6643).close
    message.send("Togami Electric{0} ({1})".format(price, price-459))
    price = q.get_price(7203).close
    message.send("Toyota own{0} ({1})".format(price, price-6239))
    price = q.get_price(8306).close
    message.send("Mitsubishi UFJ{0} ({1:.1f})".format(price, price-702.4))
    price = q.get_price(9022).close
    message.send("JR Central{0} ({1})".format(price, price-17781))
    price = q.get_price(9437).close
    message.send("NTT DoCoMo{0} ({1:.1f})".format(price, price-2600))
    price = q.get_price(9613).close
    message.send("NTT DATA{0} ({1})".format(price, price-1215))
    price = q.get_price(9501).close
    message.send("TEPCO{0} ({1})".format(price, price-472))

The current stock price and the value compared with the acquisition unit price are displayed.

5. Run

$ python run.py
スクリーンショット 2017-08-16 15.26.21.png スクリーンショット 2017-08-16 15.26.33.png

(Addition) Run slackbot as a daemon on Raspberry Pi

(mac)$ ssh [email protected]

$ sudo apt-get install python3-pip
$ pip3 install slackbot
$ pip3 install jsm
$ git clone https://github.com/msrks/slackbot.git
$ cd slackbot/
$ ls
README.md  plugins  run.py  slackbot_settings.py
$ nano slackbot_settings.py #Rewrite API key
$ nohup python3 run.py &
$ exit

See previous article for nohup

Recommended Posts

Check stock prices with slackbot using python
Automatic collection of stock prices using python
Domain check with Python
Check version with python
Check python coverage with pytest-cov
Using Quaternion with Python ~ numpy-quaternion ~
[Python] Using OpenCV with Python (Basic)
Get stock price with Python
Using OpenCV with Python @Mac
Send using Python with Gmail
IP spoof using tor on macOS and check with python
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
[Python] Generate a password with Slackbot
Check python code styles using pep8
Notes on using rstrip with python.
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[AWS] Using ini files with Lambda [Python]
[Python] Easy argument type check with dataclass
Try mathematical formulas using Σ with python
Behind the flyer: Using Docker with Python
Using Python and MeCab with Azure Databricks
Check the existence of the file with python
Download Japanese stock price data with python
Try using Python with Google Cloud Functions
Working with OpenStack using the Python SDK
Tips for using python + caffe with TSUBAME
Web scraping with Python ② (Actually scraping stock sites)
I'm using tox and Python 3.3 with Travis-CI
[Personal memo] julia --Using Python library with julia using PyCall
FizzBuzz with Python3
Stock number ranking by Qiita tag with python
Scraping with Python
Python nan check
Read data with python / netCDF> nc.variables [] / Check data size
Automatically check Python scripts with GitHub + Travis-CI + pycodestyle
python grammar check
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Start using Python
Get stock price data with Quandl API [Python]
Check the date of the flag duty with Python
Play with 2016-Python
I tried using mecab with python2.7, ruby2.3, php7
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Recent ranking creation using Qiita API with Python
Bingo with python
Zundokokiyoshi with python
Let's do web scraping with Python (stock price)
Scraping using Python
Excel with Python
Microcomputer with Python