LINEbot is 1on1, isn't it? Isn't it possible to add it to a group like a common bot?
By the way, although it is a LINE bot, the hurdle is raised where an SSL server is required. Let's make this quickly with https-ready GAE. The problem is how to fix the IP at the time of meg send, but this is muddy. I will describe it later.
I will use flask, so let's dig lib in the project folder and pip it
sh
$ pip install -t lib flask
python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import urllib
import json
from google.appengine.ext import vendor
vendor.add('lib')
from google.appengine.api import urlfetch
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/')
def index():
return 'hello my line bot'
@app.route('/callback', methods=["POST"])
def linebot():
args = json.loads(request.get_data().decode('utf-8'))
logging.debug('kick from line server,\n %s'%(args['result']))
for msg in args['result']:
kickBot( msg["content"]["from"], msg["eventType"], msg["content"]["text"] )
return "{}"
def kickBot(tgt_id, event_type, msg_data):
url = "https://trialbot-api.line.me/v1/events"
form_fields = {
"to": [str(tgt_id)],
"toChannel": 1383378250,
"eventType": 138311608800106203,
"content":{
"contentType":1,
"toType":1,
"text":u"Naked%s !"%(msg_data)
}
}
logging.debug(form_fields)
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(
url=url,
payload=json.dumps(form_fields,ensure_ascii=False),
method=urlfetch.POST,
headers={
'Content-type':'application/json; charset=UTF-8',
'X-Line-ChannelID':'<YOUR-ID>',
'X-Line-ChannelSecret':'<YOUR-SECRET>',
'X-Line-Trusted-User-With-ACL':'<YOUR-ACL>?',
}
)
if result.status_code == 200:
logging.debug(result.content)
else:
logging.debug(result.content)
yaml
application: <YOUR-GAE-NAME>
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
Have / callback
send LINE server messages.
https://developers.line.me/channels/<YOUR-CHANNEL-ID>
Register it in the Callback URL on the LINE Dev console.
https://your-gae.appspot.com:443/callback
Let's release the above GAE app and tweet to the bot. No reply comes at this point. This is because the IP is not registered in the LINEbot server. In GAE, the IP associated with the published application name and the IP of the server on which urlfetch is executed are Since they are not the same, you need to actually hit LINEbot from GAE to investigate the IP.
On the GCP Logging console, Such an IP Shirangana! I think you're getting an error like
. This is the one.
{"statusCode":"427","statusMessage":"Your ip address [This address] is not allowed to access this API."}
The IP that appears in this this time's address
will be registered in the WhiteList of LINE's Dev console. If you do it several times, you may see some differences. Please register each time.
In my case, I registered about 3 but it didn't increase any more. (GAE version is changed and deployed)
Now you will get a reply from the bot.
This implementation is just for checking. It's just simple text, so what happens if you change the type, whether it's a video or an image? Please check it. The reason is that it can't handle even some traffic. Check here for details
In the future, I plan to use TQ to create a version that will be passed asynchronously.
Recommended Posts