2016-06-28 56 views
2

使用twilio-ruby包连接到Twilio的IP消息系统服务的REST API并尝试计算未读消息数。Twilio IP消息 - 获取REST API上的最后一条消息索引

的REST API是分页的消息,以便像

channel.messages.list.last.index 

将返回49就曾有在渠道超过50条消息。

有没有办法让通道上的最后一条消息(在android/ios SDK中似乎是可能的),以避免在所有消息历史中分页?

回答

1

在问候计算未读消息计数,看看在Message Consumption Horizon和从列表中的消息的总数减去lastConsumedMessageIndex - 1.

对于消息列表(在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); 
} 
+0

谢谢,我能够设置和检索成员的lastConsumedMessageIndex并执行减法以获得未读计数。但是,我担心的是在REST API中获取没有通过所有消息进行分页的消息总数。如果你的例子中的下一行是使用'len(messages)',它是否也返回50(从REST API返回的结果的数量?) – mattwarren

+1

啊,是的,它会返回当前页面的结果。该API目前不提供一种方式来获取未分页的频道中的邮件总数。 –

相关问题