Check the stock price on slack. It's very convenient. (Here was very helpful)
## 1. Make a slackbothttps://my.slack.com/services/new/bot
$ pip install slackbot
$ pip install jsm
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
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.
$ python run.py
(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