如何读取从 AWS Pinpoint 收到的 SMS

问题描述

我目前正在尝试通过读取从 AWS Pinpoint 收到的 SMS 来触发 AWS Lambda 中函数的不同部分。例如,如果 I send,则 lambda 函数被触发以仅执行代码的特定部分。如果我发送 stop,lambda 函数会运行不同的代码部分。

我设置了一个由 SNS 触发的 lambda 函数。我是从那个 link 设置的。并且这件作品没有问题。

然后我创建了一个 Kinesis 流(遵循 here 中的步骤),它将读取任何传入的消息到 Pinpoint 电话号码。

我有以下一段代码要从数据流中读取:

def get_kinesis_data_iterator(stream_name,minutes_running):
    # Get data about Kinesis stream for Tag Monitor
    kinesis_stream = conn.describe_stream(stream_name)
    # Get the shards in that stream
    shards = kinesis_stream['StreamDescription']['Shards']
    # Collect together the shard IDs
    shard_ids = [shard['ShardId'] for shard in shards]
    # Get shard iterator
    iter_response = conn.get_shard_iterator(stream_name,shard_ids[0],"TRIM_HORIZON")
    shard_iterator = iter_response['ShardIterator']
    
    # Calculate end time
    end_time = datetime.Now() + timedelta(minutes=minutes_running)
    while True:
        try:
            # Get data
            record_response = conn.get_records(shard_iterator)
            # Only run for a certain amount of time.
            # Stop looping if no data returned. This means it's done
            Now = datetime.Now()
            print('Time: {0}'.format(Now.strftime('%Y/%m/%d %H:%M:%s')))
            if end_time < Now or not record_response:
                break
            # yield data to outside calling iterator
            for record in record_response['Records']:
                if not record_response['Records']:
                    pass
                else:
                    last_sequence = record['SequenceNumber']
                    # print(record['Data'])
                yield json.loads(record['Data'])
            # Get next iterator for shard from prevIoUs request
            shard_iterator = record_response['NextShardIterator']
        # Catch exception meaning hitting API too much
        except boto.kinesis.exceptions.ProvisionedThroughputExceededException:
            print('ProvisionedThroughputExceededException found. Sleeping for 0.5 second...')
            time.sleep(0.5)
        # Catch exception meaning iterator has expired
        except boto.kinesis.exceptions.ExpiredIteratorException:
            iter_response = conn.get_shard_iterator(stream_name,"AFTER_SEQUENCE_NUMBER",last_sequence)
            shard_iterator = iter_response['ShardIterator']

    conn.close()


if __name__ == "__main__":
    kinesis_data = get_kinesis_data_iterator(STREAM_NAME,MINUTES_RUNNING)

    for data in kinesis_data:
        print(data)

但是当我运行它并向 Pinpoint 号码发送 SMS 时,我看不到包含我发送的消息的流。任何想法为什么会发生这种情况?还有另一种方式可以读取来自 Pinpoint 的传入短信吗?也许更简单的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)