LINE Messaging API SDK for Pythonのv3でpush_messageする

2023年11月24日プログラムline-messagin-api,pushmessage,python,v3

スポンサーリンク

LineBotApiを使うパターン

「LINE Messaging API pushmessage」などで検索すると、以下のようなコードがヒットしてくる。

from linebot import LineBotApi
from linebot.models import TextSendMessage

line_bot_api = LineBotApi('access_token')
user_id = "user_id"
messages = TextSendMessage(text='test')
line_bot_api.push_message(user_id, messages=messages)

ただ、上記を実行すると

LineBotSdkDeprecatedIn30: Call to deprecated class LineBotApi. (Use v3 class; linebot.v3.<feature>. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.
LineBotSdkDeprecatedIn30: Call to deprecated method push_message. (Use 'from linebot.v3.messaging import MessagingApi' and 'MessagingApi(...).push_message(...)' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.

といったエラーが出る。

要は「v3を使ってください」ということだと思われる。

でもv3について検索しても日本語の情報はおろか、Stack Overflowなどでも検索した限り情報が出てこなかった。

よくよく考えてみたら、海外ではLINE使ってないもんね。

linebot.v3.messagingを使う

しょうがないので、公式のコードを読み解いて実装できたので、そのコードを載せておく。

公式のREADMEのコードで動かないのは、ワナだと思った。
access_tokenとchannel_id(push_message先のチャンネルID)は、適宜置き換えてください。

from linebot.v3.messaging import Configuration, MessagingApi, ApiClient, PushMessageRequest, ApiException


configuration = Configuration(
    access_token = 'access_token'
)

message_dict = {
    'to': 'channel_id',
    'messages': [
        {
            'type': 'text',
            'text': 'テストメッセージ'
        },
    ]
}

with ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = MessagingApi(api_client)
    push_message_request = PushMessageRequest.from_dict(message_dict)

    try:
        push_message_result = api_instance.push_message_with_http_info(push_message_request, _return_http_data_only=False)
        print(f'The response of MessagingApi->push_message status code => {push_message_result.status_code}')
    except ApiException as e:
        print('Exception when calling MessagingApi->push_message: %s\n' % e)

VideoMessage

ちなみに、動画も送りたい場合はmessage_dictを

message_dict = {
    'to': 'channel_id',
    'messages': [
        {
            'type': 'text',
            'text': '動画メッセージ'
        },
        {
            'type': 'video',
            'originalContentUrl': '動画のURL',
            'previewImageUrl': 'プレビュー画像のURL',
        }
    ]
}

とすればよい。

FlexMessage

動画を含んだFlexMessageにしたい場合は

message_dict = {
    'messages': [
        {
            'type': 'flex',
            'altText': 'テストメッセージ',
            'contents': {
                'type': 'bubble',
                'hero': {
                    'type': 'video',
                    'url': '動画のURL',
                    'previewUrl': 'プレビュー画像のURL',
                    'altContent': {
                        'type': 'image',
                        'size': 'full',
                        'aspectRatio': '5:3',
                        'aspectMode': 'cover',
                        'url': 'プレビュー画像のURL'
                    },
                    'aspectRatio': '5:3'
                },
                'body': {
                    'type': 'box',
                    'layout': 'vertical',
                    'contents': [
                        {
                            'type': 'text',
                            'text': 'テストメッセージ'
                        }
                    ]
                }
            }
        }
    ]
}

こんな感じにすれば動画も送れる。

ninki_glog_ranking
ブログランキング・にほんブログ村へ

スポンサーリンク