Twilio IP消息传递 - 获取REST API上的最后一条消息索引

时间:2016-06-28 18:21:19

标签: twilio twilio-api

使用twilio-ruby软件包连接到Twilio的IP消息系统服务的REST API并尝试计算未读消息计数。

REST API正在对消息进行分页,以便像

这样
channel.messages.list.last.index

一旦频道中有超过50条消息,将返回49。

有没有办法只获取频道上的最后一条消息(似乎可以在android / ios SDK中),以避免在所有消息历史记录中分页?

1 个答案:

答案 0 :(得分:1)

关于计算未读邮件计数,请查看Message Consumption Horizon并从列表中的邮件总数中减去lastConsumedMessageIndex -

对于消息列表(在Python中):

https://www.twilio.com/docs/api/ip-messaging/rest/messages#list-all-messages

# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest.ip_messaging import TwilioIpMessagingClient

# Your Account Sid and Auth Token from twilio.com/user/account
account = "ACCOUNT_SID"
token = "AUTH_TOKEN"
client = TwilioIpMessagingClient(account, token) 

service = client.services.get(sid="SERVICE_SID")
channel = service.channels.get(sid="CHANNEL_ID")
messages = channel.messages.list()  

另请参阅Sending a Consumption Report(JavaScript中的示例):

//determine the newest message index
var newestMessageIndex = activeChannel.messages.length ? 
    activeChannel.messages[activeChannel.messages.length-1].index : 0;
//check if we we need to set the consumption horizon
if (activeChannel.lastConsumedMessageIndex !== newestMessageIndex) {
    activeChannel.updateLastConsumedMessageIndex(newestMessageIndex);
}