Recently, I went to an AI / IOT seminar. In terms of content, how about programming to improve the skills of clerical staff rather than for engineers? It was a kind of story, but it was enough for me to think that I should try Python! Thank you! (*'∀')
That's why this time, I'm a Python beginner, and I'm always trying to implement the LINEWORKS BOT in Python!
Language: python WEB framework: Flask Editor: Visual Studio Code Server: Local environment (ngrok)
I don't have a verification server, so it's still local (laughs) I love ngrok! (゚ Д ゚)
I referred to the articles of my seniors for the detailed construction procedure! Thank you! (*'▽') I will not explain it here, so I will post a link ('ω') No Petapeta
For the Callback URL when registering the BOT, use the URL obtained by ngrok. Of course, if you have your own server, please use it. This completes the settings!
Finally, we will make the BOT body with Python ~ (* ´Д `)
Receive messages sent to the BOT using Flask.
Flask is a Python web framework. Django seems to be more famous and highly functional in Python, but since it was personally developed, I decided to use lightweight Flask.
I have a lot of knowledge and it really helps! (* ^ ▽ ^ *)
Now, let's first display the received message on the console. Like this.
b'{"type":"message","source":{"accountId":"xxx@yyy-zzz"},"createdTime":1585813140779,"content":{"type":"text","text":"hoge"}}'
I will write the code.
bot.py
from flask import Flask, request
app = Flask(__name__)
@app.route('/callback', methods=['POST'])
def callback():
data = request.get_data()
print(data)
return "200 ok"
##port setting
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8000)
--To use request.get_data () to receive a message
It's the first line, but it seems that I can't receive the message if I just import flask.
Follow it with , request
and then import request as well.
--return I get angry if I don't enter
At first, I got angry when I emptied the return.
TypeError: The view function did not return a valid response.
The function either returned None or ended without a return statement.
I received it for the time being! That's why I returned 200 ok
.
… Maybe there is a way to return it properly, like the javascript res.send
. (.-`ω-)
--Port defaults to 5000
If you do nothing in the port setting, the default will be 5000.
if __name__ == "__main__":
app.run(debug=True) #port is set to 5000
No, 5000 is fine, but ngrok set it to 8000 and it turned out to be "I can't connect! (゚ Д ゚)" w
I stumbled on it, but thanks to the knowledge of my seniors, I solved it myself! Thank you! (*'▽')
Let's move on to the next STEP!
I use the LINEWORKS API to reply, but some people have published the library to PyPI! Thanks! (* ^ ▽ ^ *) PyPI - lineworks 0.1.0
Let's install and use it immediately.
pip install lineworks
After installing, add the code referring to the sample.
lineworks_bot.py
from flask import Flask, request
app = Flask(__name__)
from lineworks import TalkBotApi
api_id = "your api id."
server_api_consumer_key = "your server api consumer key"
server_id = "your server id."
private_key = "your private key."
domain_id = "your domain id."
bot_no = "your bot number."
import json
@app.route('/callback', methods=['POST'])
def callback():
data = json.loads(request.get_data())
talk_bot = TalkBotApi(api_id, server_api_consumer_key, server_id, private_key, domain_id, bot_no)
#Send message (return parrot)
talk_bot.send_text_message(send_text=data['content']['text'], account_id=data['source']['accountId'])
return "200 ok"
##port setting
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=8000)
Run it in Visual Studio Code and you're ready to go! Let's talk! (* ´ ▽ ` *)
YES!NINJA! You returned the parrot properly! It's a success ♪
--How to write privateKey
This is a stumbling block, but as you can see from the actual authentication key, it is extremely long and has strange line breaks. So it's a little troublesome,
" ----- BEGIN PRIVATE KEY ----- \ nMIIEvAIBADANBgkqhkiG9 ... \ n ----- END PRIVATE KEY ----- "
\ nand
\ n` so that Put the authentication key between and write it in one line.
--Handling of JSON data
Use it after parse it with loads in the JSON library.
However, if you try to handle it with data.source.accountId
and parameters like Javascript, an error will occur. (It's very possible that my method is wrong ...)
Therefore, when extracting the contents, write data ['source'] ['accountId']
as described above.
In the example, text
and ʻaccountId` in the JSON data are extracted and used.
Anyway, it's done! (^ O ^)
Thank you for staying with us so far.
No, what I'm doing is the same as usual, but different languages are different. But I feel like I'm getting along a little!
With this momentum, I would like to develop BOTs that work with machine learning APIs. see you! (^^) /
LINEWORKS Developers Install python and Visual Studio Code on windows 10 (April 2020 version) Easy to use Flask LINEbot development, I want to check the operation in the local environment First bot development in LINE WORKS! (Part 1) Use request.get_data () to receive the data posted in Flask as it is Created a Python library to call the LINE WORKS API
Recommended Posts