Develop a LineBot to do something for me.
- Having a Line account.
- Having a Heroku account.
- After login Heroku, click
New
->Create New App
. - Type in app name, and click
Create app
button.
- Enter Line Console.
- Creat provider.
- Type in provider name.
- Click
create
button. - Click
Create Channel
. - Fill in Bot information.
- Agree with the term, and click
create
. - Enter the Bot just created.
- Open webhook.
- Paste Channel access token and Channel secret.
- In terminal:
git clone git@github.com:penguinwang96825/LineBot.git
. - Copy paste Channel access token and Channel secret into
app.py
.
- Download and install Heroku CLI. If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key.
- Open terminal:
heroku login
- Initialise git:
git config --global user.name "Your name"
git config --global user.email [Your email]
git init
- Connect to Heroku. Use Git to clone line-bot-wang's source code to your local machine.
heroku git:remote -a {HEROKU_APP_NAME}
- Push your code onto Heroku. (Every time you update your code, please run these three lines of code.) Make some changes to the code you just cloned and deploy them to Heroku using Git.
git add .
git commit -m "Add code"
git push -f heroku master
- Enter Line Console and choose your bot.
- Type in url of Heroku in
webhook URL
.
https://{HEROKU_APP_NAME}.herokuapp.com/callback
- Click
verify
button.
- Debug (Optional)
heroku login
heroku logs --tail --app {HEROKU_APP_NAME}
- File in folder
Procfile
:In Heroku, web: {language} {app_name}, so we name it web: python app.py。requirements.txt
: You can install the packages you need.
You can send the following types of messages. By defining actions, you can make these messages interactive. Here is the official API documentation.
- Text message
- Sticker message
- Image message
- Video message
- Audio message
- Location message
- Imagemap message
- Template message
- Flex Message
message = TextSendMessage(text='Hello, world')
line_bot_api.reply_message(event.reply_token, message)
message = ImageSendMessage(
original_content_url='https://example.com/original.jpg',
preview_image_url='https://example.com/preview.jpg'
)
line_bot_api.reply_message(event.reply_token, message)
message = VideoSendMessage(
original_content_url='https://example.com/original.mp4',
preview_image_url='https://example.com/preview.jpg'
)
line_bot_api.reply_message(event.reply_token, message)
message = AudioSendMessage(
original_content_url='https://example.com/original.m4a',
duration=240000
)
line_bot_api.reply_message(event.reply_token, message)
message = LocationSendMessage(
title='my location',
address='Tokyo',
latitude=35.65910807942215,
longitude=139.70372892916203
)
line_bot_api.reply_message(event.reply_token, message)
message = StickerSendMessage(
package_id='1',
sticker_id='1'
)
line_bot_api.reply_message(event.reply_token, message)
message = ImagemapSendMessage(
base_url='https://example.com/base',
alt_text='this is an imagemap',
base_size=BaseSize(height=1040, width=1040),
actions=[
URIImagemapAction(
link_uri='https://example.com/',
area=ImagemapArea(
x=0, y=0, width=520, height=1040
)
),
MessageImagemapAction(
text='hello',
area=ImagemapArea(
x=520, y=0, width=520, height=1040
)
)
]
)
line_bot_api.reply_message(event.reply_token, message)
message = TemplateSendMessage(
alt_text='Buttons template',
template=ButtonsTemplate(
thumbnail_image_url='https://example.com/image.jpg',
title='Menu',
text='Please select',
actions=[
PostbackTemplateAction(
label='postback',
text='postback text',
data='action=buy&itemid=1'
),
MessageTemplateAction(
label='message',
text='message text'
),
URITemplateAction(
label='uri',
uri='http://example.com/'
)
]
)
)
line_bot_api.reply_message(event.reply_token, message)
message = TemplateSendMessage(
alt_text='Confirm template',
template=ConfirmTemplate(
text='Are you sure?',
actions=[
PostbackTemplateAction(
label='postback',
text='postback text',
data='action=buy&itemid=1'
),
MessageTemplateAction(
label='message',
text='message text'
)
]
)
)
line_bot_api.reply_message(event.reply_token, message)
![](https://i.imgur.com/982Glgo.png =250x)
message = TemplateSendMessage(
alt_text='Carousel template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url='https://example.com/item1.jpg',
title='this is menu1',
text='description1',
actions=[
PostbackTemplateAction(
label='postback1',
text='postback text1',
data='action=buy&itemid=1'
),
MessageTemplateAction(
label='message1',
text='message text1'
),
URITemplateAction(
label='uri1',
uri='http://example.com/1'
)
]
),
CarouselColumn(
thumbnail_image_url='https://example.com/item2.jpg',
title='this is menu2',
text='description2',
actions=[
PostbackTemplateAction(
label='postback2',
text='postback text2',
data='action=buy&itemid=2'
),
MessageTemplateAction(
label='message2',
text='message text2'
),
URITemplateAction(
label='uri2',
uri='http://example.com/2'
)
]
)
]
)
)
line_bot_api.reply_message(event.reply_token, message)
![](https://i.imgur.com/2ys1qqc.png =250x)
message = TemplateSendMessage(
alt_text='ImageCarousel template',
template=ImageCarouselTemplate(
columns=[
ImageCarouselColumn(
image_url='https://example.com/item1.jpg',
action=PostbackTemplateAction(
label='postback1',
text='postback text1',
data='action=buy&itemid=1'
)
),
ImageCarouselColumn(
image_url='https://example.com/item2.jpg',
action=PostbackTemplateAction(
label='postback2',
text='postback text2',
data='action=buy&itemid=2'
)
)
]
)
)
line_bot_api.reply_message(event.reply_token, message)