Fixed some Facebook issues. Improved tests.
This commit is contained in:
parent
ec69425f2c
commit
a397132931
7 changed files with 422 additions and 201 deletions
|
@ -57,6 +57,18 @@ job_integration_twitter:
|
||||||
- rake db:seed
|
- rake db:seed
|
||||||
- ruby -I test/ test/integration/twitter_test.rb
|
- ruby -I test/ test/integration/twitter_test.rb
|
||||||
|
|
||||||
|
job_integration_facebook:
|
||||||
|
stage: test
|
||||||
|
tags:
|
||||||
|
- core
|
||||||
|
script:
|
||||||
|
- export RAILS_ENV=test
|
||||||
|
- rake db:drop;
|
||||||
|
- rake db:create
|
||||||
|
- rake db:migrate
|
||||||
|
- rake db:seed
|
||||||
|
- ruby -I test/ test/integration/facebook_test.rb
|
||||||
|
|
||||||
job_integration_geo_ip:
|
job_integration_geo_ip:
|
||||||
stage: test
|
stage: test
|
||||||
tags:
|
tags:
|
||||||
|
|
|
@ -4,53 +4,78 @@ require 'facebook'
|
||||||
|
|
||||||
class Channel::Driver::Facebook
|
class Channel::Driver::Facebook
|
||||||
|
|
||||||
def fetch (_adapter_options, channel)
|
def fetch (options, channel)
|
||||||
|
|
||||||
@channel = channel
|
@channel = channel
|
||||||
@facebook = Facebook.new( @channel[:options] )
|
@sync = options['sync']
|
||||||
@sync = @channel[:options][:sync]
|
@pages = options['pages']
|
||||||
|
|
||||||
Rails.logger.debug 'facebook fetch started'
|
Rails.logger.debug 'facebook fetch started'
|
||||||
|
|
||||||
fetch_feed
|
fetch_feed
|
||||||
|
|
||||||
disconnect
|
disconnect
|
||||||
|
|
||||||
Rails.logger.debug 'facebook fetch completed'
|
Rails.logger.debug 'facebook fetch completed'
|
||||||
|
notice = ''
|
||||||
|
{
|
||||||
|
result: 'ok',
|
||||||
|
notice: notice,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def send(article, _notification = false)
|
def send(options, fb_object_id, article, _notification = false)
|
||||||
|
access_token = nil
|
||||||
|
options['pages'].each {|page|
|
||||||
|
next if page['id'].to_s != fb_object_id.to_s
|
||||||
|
access_token = page['access_token']
|
||||||
|
}
|
||||||
|
if !access_token
|
||||||
|
fail "No access_token found for fb_object_id: #{fb_object_id}"
|
||||||
|
end
|
||||||
|
client = Facebook.new(access_token)
|
||||||
|
client.from_article(article)
|
||||||
|
end
|
||||||
|
|
||||||
@channel = Channel.find_by( area: 'Facebook::Inbound', active: true )
|
=begin
|
||||||
@facebook = Facebook.new( @channel[:options] )
|
|
||||||
|
|
||||||
post = @facebook.from_article(article)
|
instance = Channel::Driver::Facebook.new
|
||||||
disconnect
|
instance.fetchable?(channel)
|
||||||
|
|
||||||
post
|
=end
|
||||||
|
|
||||||
|
def fetchable?(_channel)
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def disconnect
|
def disconnect
|
||||||
@facebook.disconnect
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def fetch_feed
|
def get_page(page_id)
|
||||||
|
@pages.each {|page|
|
||||||
return if !@sync[:group_id]
|
return page if page['id'].to_s == page_id.to_s
|
||||||
|
|
||||||
counter = 0
|
|
||||||
feed = @facebook.client.get_connections('me', 'feed')
|
|
||||||
feed.each { |feed_item|
|
|
||||||
|
|
||||||
break if @sync[:limit] && @sync[:limit] <= counter
|
|
||||||
|
|
||||||
post = @facebook.client.get_object( feed_item['id'] )
|
|
||||||
|
|
||||||
@facebook.to_group( post, @sync[:group_id] )
|
|
||||||
|
|
||||||
counter += 1
|
|
||||||
}
|
}
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_feed
|
||||||
|
return if !@sync
|
||||||
|
return if !@sync['pages']
|
||||||
|
|
||||||
|
@sync['pages'].each {|page_to_sync_id, page_to_sync_params|
|
||||||
|
page = get_page(page_to_sync_id)
|
||||||
|
next if !page
|
||||||
|
next if !page_to_sync_params['group_id']
|
||||||
|
next if page_to_sync_params['group_id'].empty?
|
||||||
|
page_client = Facebook.new(page['access_token'])
|
||||||
|
|
||||||
|
posts = page_client.client.get_connection('me', 'feed', fields: 'id,from,to,message,created_time,comments')
|
||||||
|
posts.each {|post|
|
||||||
|
page_client.to_group(post, page_to_sync_params['group_id'], @channel, page)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,13 +19,35 @@ class Observer::Ticket::Article::CommunicateFacebook < ActiveRecord::Observer
|
||||||
type = Ticket::Article::Type.lookup(id: record.type_id)
|
type = Ticket::Article::Type.lookup(id: record.type_id)
|
||||||
return if type['name'] !~ /\Afacebook/
|
return if type['name'] !~ /\Afacebook/
|
||||||
|
|
||||||
|
ticket = Ticket.lookup(id: record.ticket_id)
|
||||||
|
fail "Can't find ticket.preferences for Ticket.find(#{record.ticket_id})" if !ticket.preferences
|
||||||
|
fail "Can't find ticket.preferences['channel_id'] for Ticket.find(#{record.ticket_id})" if !ticket.preferences['channel_id']
|
||||||
|
channel = Channel.lookup(id: ticket.preferences['channel_id'])
|
||||||
|
fail "Channel.find(#{channel.id}) isn't a twitter channel!" if channel.options[:adapter] !~ /\Afacebook/i
|
||||||
|
|
||||||
|
# check source object id
|
||||||
|
ticket = record.ticket
|
||||||
|
if !ticket.preferences['channel_fb_object_id']
|
||||||
|
fail "fb object id is missing in ticket.preferences['channel_fb_object_id'] for Ticket.find(#{ticket.id})"
|
||||||
|
end
|
||||||
|
|
||||||
|
# fill in_reply_to
|
||||||
|
if !record.in_reply_to || record.in_reply_to.empty?
|
||||||
|
record.in_reply_to = ticket.articles.first.message_id
|
||||||
|
end
|
||||||
|
|
||||||
facebook = Channel::Driver::Facebook.new
|
facebook = Channel::Driver::Facebook.new
|
||||||
post = facebook.send({
|
post = facebook.send(
|
||||||
|
channel.options,
|
||||||
|
ticket.preferences[:channel_fb_object_id],
|
||||||
|
{
|
||||||
type: type['name'],
|
type: type['name'],
|
||||||
to: record.to,
|
to: record.to,
|
||||||
body: record.body,
|
body: record.body,
|
||||||
in_reply_to: record.in_reply_to
|
in_reply_to: record.in_reply_to,
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
record.from = post['from']['name']
|
||||||
record.message_id = post['id']
|
record.message_id = post['id']
|
||||||
record.save
|
record.save
|
||||||
end
|
end
|
||||||
|
|
243
lib/facebook.rb
243
lib/facebook.rb
|
@ -6,101 +6,195 @@ class Facebook
|
||||||
|
|
||||||
attr_accessor :client, :account
|
attr_accessor :client, :account
|
||||||
|
|
||||||
def initialize(options)
|
=begin
|
||||||
|
|
||||||
connect( options[:auth][:access_token] )
|
client = Facebook.new('user_or_page_access_token')
|
||||||
|
|
||||||
page_access_token = access_token_for_page( options[:sync] )
|
=end
|
||||||
|
|
||||||
if page_access_token
|
def initialize(access_token)
|
||||||
connect( page_access_token )
|
connect(access_token)
|
||||||
end
|
end
|
||||||
|
|
||||||
@account = client.get_object('me')
|
=begin
|
||||||
end
|
|
||||||
|
reconnect with other access_token
|
||||||
|
|
||||||
|
client.connect('user_or_page_access_token')
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
def connect(access_token)
|
def connect(access_token)
|
||||||
@client = Koala::Facebook::API.new(access_token)
|
@client = Koala::Facebook::API.new(access_token)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
=begin
|
||||||
|
|
||||||
|
disconnect client
|
||||||
|
|
||||||
|
client.disconnect
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
def disconnect
|
def disconnect
|
||||||
|
|
||||||
return if !@client
|
return if !@client
|
||||||
|
|
||||||
@client = nil
|
@client = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
=begin
|
||||||
|
|
||||||
|
get pages of user
|
||||||
|
|
||||||
|
pages = client.pages
|
||||||
|
|
||||||
|
result
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
id: '12345',
|
||||||
|
name: 'Some Page Name',
|
||||||
|
access_token, 'some_access_token_for_page',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
def pages
|
def pages
|
||||||
pages = []
|
pages = []
|
||||||
@client.get_connections('me', 'accounts').each { |page|
|
@client.get_connections('me', 'accounts').each { |page|
|
||||||
pages.push({
|
pages.push(
|
||||||
id: page['id'],
|
id: page['id'],
|
||||||
name: page['name'],
|
name: page['name'],
|
||||||
access_token: page['access_token'],
|
access_token: page['access_token'],
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
pages
|
pages
|
||||||
end
|
end
|
||||||
|
|
||||||
def user(item)
|
=begin
|
||||||
|
|
||||||
|
get current user
|
||||||
|
|
||||||
|
pages = current_user
|
||||||
|
|
||||||
|
result
|
||||||
|
|
||||||
|
{
|
||||||
|
'id' => '1234567890123456',
|
||||||
|
'name' => 'Page/User Name',
|
||||||
|
'access_token' => 'some_acces_token'
|
||||||
|
}
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
@client.get_object('me')
|
||||||
|
end
|
||||||
|
|
||||||
|
=begin
|
||||||
|
|
||||||
|
get user of comment/post
|
||||||
|
|
||||||
|
pages = user(comment_or_post)
|
||||||
|
|
||||||
|
result
|
||||||
|
|
||||||
|
???
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
|
def user(item)
|
||||||
return if !item['from']
|
return if !item['from']
|
||||||
return if !item['from']['id']
|
return if !item['from']['id']
|
||||||
return if !item['from']['name']
|
cache_key = "FB:User:Lookup:#{item['from']['id']}"
|
||||||
|
cache = Cache.get(cache_key)
|
||||||
return if item['from']['id'] == @account['id']
|
return cache if cache
|
||||||
|
begin
|
||||||
@client.get_object( item['from']['id'] )
|
result = @client.get_object(item['from']['id'], fields: 'first_name,last_name,email')
|
||||||
|
rescue
|
||||||
|
result = @client.get_object(item['from']['id'], fields: 'name')
|
||||||
|
end
|
||||||
|
if result
|
||||||
|
Cache.write(cache_key, result, { expires_in: 15.minutes })
|
||||||
|
end
|
||||||
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_user(item)
|
def to_user(item)
|
||||||
|
|
||||||
Rails.logger.debug 'Create user from item...'
|
Rails.logger.debug 'Create user from item...'
|
||||||
Rails.logger.debug item.inspect
|
Rails.logger.debug item.inspect
|
||||||
|
|
||||||
# do item_user lookup
|
# do item_user lookup
|
||||||
item_user = user(item)
|
item_user = user(item)
|
||||||
|
|
||||||
return if !item_user
|
return if !item_user
|
||||||
|
|
||||||
auth = Authorization.find_by(uid: item_user['id'], provider: 'facebook')
|
auth = Authorization.find_by(uid: item_user['id'], provider: 'facebook')
|
||||||
|
|
||||||
# create or update user
|
# create or update user
|
||||||
user_data = {
|
user_data = {
|
||||||
login: item_user['id'], # TODO
|
image_source: "https://graph.facebook.com/#{item_user['id']}/picture?type=large",
|
||||||
firstname: item_user['first_name'] || item_user['name'],
|
|
||||||
lastname: item_user['last_name'] || '',
|
|
||||||
email: '',
|
|
||||||
password: '',
|
|
||||||
# TODO: image_source: '',
|
|
||||||
# TODO: note: '',
|
|
||||||
active: true,
|
|
||||||
roles: Role.where( name: 'Customer' ),
|
|
||||||
}
|
}
|
||||||
if auth
|
if auth
|
||||||
user_data[:id] = auth.user_id
|
user = User.find(auth.user_id)
|
||||||
end
|
map = {
|
||||||
user = User.create_or_update( user_data )
|
#note: 'description',
|
||||||
|
}
|
||||||
|
|
||||||
# create or update authorization
|
# ignore if value is already set
|
||||||
|
map.each {|target, source|
|
||||||
|
next if user[target] && !user[target].empty?
|
||||||
|
new_value = tweet_user.send(source).to_s
|
||||||
|
next if !new_value || new_value.empty?
|
||||||
|
user_data[target] = new_value
|
||||||
|
}
|
||||||
|
user.update_attributes(user_data)
|
||||||
|
else
|
||||||
|
user_data[:login] = item_user['id']
|
||||||
|
if item_user['first_name'] && item_user['last_name']
|
||||||
|
user_data[:firstname] = item_user['first_name']
|
||||||
|
user_data[:lastname] = item_user['last_name']
|
||||||
|
else
|
||||||
|
user_data[:firstname] = item_user['name']
|
||||||
|
end
|
||||||
|
user_data[:active] = true
|
||||||
|
user_data[:roles] = Role.where(name: 'Customer')
|
||||||
|
|
||||||
|
user = User.create(user_data)
|
||||||
|
end
|
||||||
|
|
||||||
|
if user_data[:image_source]
|
||||||
|
avatar = Avatar.add(
|
||||||
|
object: 'User',
|
||||||
|
o_id: user.id,
|
||||||
|
url: user_data[:image_source],
|
||||||
|
source: 'facebook',
|
||||||
|
deletable: true,
|
||||||
|
updated_by_id: user.id,
|
||||||
|
created_by_id: user.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# update user link
|
||||||
|
if avatar && user.image != avatar.store_hash
|
||||||
|
user.image = avatar.store_hash
|
||||||
|
user.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# create authorization
|
||||||
|
if !auth
|
||||||
auth_data = {
|
auth_data = {
|
||||||
uid: item_user['id'],
|
uid: item_user['id'],
|
||||||
username: item_user['id'], # TODO
|
username: item_user['id'],
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
provider: 'facebook'
|
provider: 'facebook'
|
||||||
}
|
}
|
||||||
if auth
|
Authorization.create(auth_data)
|
||||||
auth.update_attributes( auth_data )
|
|
||||||
else
|
|
||||||
Authorization.new( auth_data )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
UserInfo.current_user_id = user.id
|
UserInfo.current_user_id = user.id
|
||||||
|
|
||||||
user
|
user
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_ticket(post, group_id)
|
def to_ticket(post, group_id, channel, page)
|
||||||
|
|
||||||
Rails.logger.debug 'Create ticket from post...'
|
Rails.logger.debug 'Create ticket from post...'
|
||||||
Rails.logger.debug post.inspect
|
Rails.logger.debug post.inspect
|
||||||
|
@ -109,16 +203,27 @@ class Facebook
|
||||||
user = to_user(post)
|
user = to_user(post)
|
||||||
return if !user
|
return if !user
|
||||||
|
|
||||||
|
# prepare title
|
||||||
|
|
||||||
|
title = post['message']
|
||||||
|
if title.length > 80
|
||||||
|
title = "#{title[0, 80]}..."
|
||||||
|
end
|
||||||
|
|
||||||
Ticket.create(
|
Ticket.create(
|
||||||
customer_id: user.id,
|
customer_id: user.id,
|
||||||
title: "#{post['message'][0, 37]}...",
|
title: title,
|
||||||
group_id: group_id,
|
group_id: group_id,
|
||||||
state: Ticket::State.find_by(name: 'new'),
|
state: Ticket::State.find_by(name: 'new'),
|
||||||
priority: Ticket::Priority.find_by(name: '2 normal'),
|
priority: Ticket::Priority.find_by(name: '2 normal'),
|
||||||
|
preferences: {
|
||||||
|
channel_id: channel.id,
|
||||||
|
channel_fb_object_id: page['id'],
|
||||||
|
},
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_article(post, ticket)
|
def to_article(post, ticket, _page)
|
||||||
|
|
||||||
Rails.logger.debug 'Create article from post...'
|
Rails.logger.debug 'Create article from post...'
|
||||||
Rails.logger.debug post.inspect
|
Rails.logger.debug post.inspect
|
||||||
|
@ -133,12 +238,26 @@ class Facebook
|
||||||
user = to_user(post)
|
user = to_user(post)
|
||||||
return if !user
|
return if !user
|
||||||
|
|
||||||
|
to = nil
|
||||||
|
if post['to'] && post['to']['data']
|
||||||
|
post['to']['data'].each {|to_entry|
|
||||||
|
if !to
|
||||||
|
to = ''
|
||||||
|
else
|
||||||
|
to += ', '
|
||||||
|
end
|
||||||
|
to += to_entry['name']
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
feed_post = {
|
feed_post = {
|
||||||
from: "#{user.firstname} #{user.lastname}",
|
from: post['from']['name'],
|
||||||
|
to: to,
|
||||||
body: post['message'],
|
body: post['message'],
|
||||||
message_id: post['id'],
|
message_id: post['id'],
|
||||||
type_id: Ticket::Article::Type.find_by(name: 'facebook feed post').id,
|
type_id: Ticket::Article::Type.find_by(name: 'facebook feed post').id,
|
||||||
}
|
}
|
||||||
|
|
||||||
articles = []
|
articles = []
|
||||||
articles.push(feed_post)
|
articles.push(feed_post)
|
||||||
|
|
||||||
|
@ -147,41 +266,35 @@ class Facebook
|
||||||
end
|
end
|
||||||
|
|
||||||
articles.each { |article|
|
articles.each { |article|
|
||||||
|
|
||||||
next if Ticket::Article.find_by(message_id: article[:message_id])
|
next if Ticket::Article.find_by(message_id: article[:message_id])
|
||||||
|
|
||||||
article = {
|
article = {
|
||||||
to: @account['name'],
|
#to: @account['name'],
|
||||||
ticket_id: ticket.id,
|
ticket_id: ticket.id,
|
||||||
internal: false,
|
internal: false,
|
||||||
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
|
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
|
||||||
created_by_id: 1,
|
created_by_id: 1,
|
||||||
updated_by_id: 1,
|
updated_by_id: 1,
|
||||||
}.merge(article)
|
}.merge(article)
|
||||||
|
|
||||||
Ticket::Article.create(article)
|
Ticket::Article.create(article)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_group(post, group_id)
|
def to_group(post, group_id, channel, page)
|
||||||
|
|
||||||
Rails.logger.debug 'import post'
|
Rails.logger.debug 'import post'
|
||||||
|
return if !post['message']
|
||||||
ticket = nil
|
ticket = nil
|
||||||
|
|
||||||
# use transaction
|
# use transaction
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
|
|
||||||
UserInfo.current_user_id = 1
|
UserInfo.current_user_id = 1
|
||||||
|
|
||||||
existing_article = Ticket::Article.find_by(message_id: post['id'])
|
existing_article = Ticket::Article.find_by(message_id: post['id'])
|
||||||
if existing_article
|
ticket = if existing_article
|
||||||
ticket = existing_article.ticket
|
existing_article.ticket
|
||||||
else
|
else
|
||||||
ticket = to_ticket(post, group_id)
|
to_ticket(post, group_id, channel, page)
|
||||||
return if !ticket
|
|
||||||
end
|
end
|
||||||
|
to_article(post, ticket, page)
|
||||||
to_article(post, ticket)
|
|
||||||
|
|
||||||
# execute ticket events
|
# execute ticket events
|
||||||
Observer::Ticket::Notification.transaction
|
Observer::Ticket::Notification.transaction
|
||||||
|
@ -191,17 +304,13 @@ class Facebook
|
||||||
end
|
end
|
||||||
|
|
||||||
def from_article(article)
|
def from_article(article)
|
||||||
|
|
||||||
post = nil
|
post = nil
|
||||||
if article[:type] == 'facebook feed comment'
|
if article[:type] == 'facebook feed comment'
|
||||||
|
|
||||||
Rails.logger.debug 'Create feed comment from article...'
|
Rails.logger.debug 'Create feed comment from article...'
|
||||||
|
|
||||||
post = @client.put_comment(article[:in_reply_to], article[:body])
|
post = @client.put_comment(article[:in_reply_to], article[:body])
|
||||||
else
|
else
|
||||||
fail "Can't handle unknown facebook article type '#{article[:type]}'."
|
fail "Can't handle unknown facebook article type '#{article[:type]}'."
|
||||||
end
|
end
|
||||||
|
|
||||||
Rails.logger.debug post.inspect
|
Rails.logger.debug post.inspect
|
||||||
@client.get_object(post['id'])
|
@client.get_object(post['id'])
|
||||||
end
|
end
|
||||||
|
@ -209,19 +318,14 @@ class Facebook
|
||||||
private
|
private
|
||||||
|
|
||||||
def access_token_for_page(lookup)
|
def access_token_for_page(lookup)
|
||||||
|
|
||||||
access_token = nil
|
access_token = nil
|
||||||
pages.each { |page|
|
pages.each { |page|
|
||||||
|
|
||||||
next if !lookup[:page_id] && !lookup[:page]
|
next if !lookup[:page_id] && !lookup[:page]
|
||||||
next if lookup[:page_id] && lookup[:page_id].to_s != page[:id]
|
next if lookup[:page_id] && lookup[:page_id].to_s != page[:id]
|
||||||
next if lookup[:page] && lookup[:page] != page[:name]
|
next if lookup[:page] && lookup[:page] != page[:name]
|
||||||
|
|
||||||
access_token = page[:access_token]
|
access_token = page[:access_token]
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
access_token
|
access_token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -234,11 +338,8 @@ class Facebook
|
||||||
return result if comments.empty?
|
return result if comments.empty?
|
||||||
|
|
||||||
comments.each { |comment|
|
comments.each { |comment|
|
||||||
|
|
||||||
user = to_user(comment)
|
user = to_user(comment)
|
||||||
|
|
||||||
next if !user
|
next if !user
|
||||||
|
|
||||||
article_data = {
|
article_data = {
|
||||||
from: "#{user.firstname} #{user.lastname}",
|
from: "#{user.firstname} #{user.lastname}",
|
||||||
body: comment['message'],
|
body: comment['message'],
|
||||||
|
@ -247,14 +348,12 @@ class Facebook
|
||||||
in_reply_to: in_reply_to
|
in_reply_to: in_reply_to
|
||||||
}
|
}
|
||||||
result.push(article_data)
|
result.push(article_data)
|
||||||
|
|
||||||
sub_comments = @client.get_object( "#{comment['id']}/comments" )
|
sub_comments = @client.get_object( "#{comment['id']}/comments" )
|
||||||
|
|
||||||
sub_articles = nested_comments(sub_comments, comment['id'])
|
sub_articles = nested_comments(sub_comments, comment['id'])
|
||||||
|
|
||||||
result += sub_articles
|
result += sub_articles
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,27 +7,55 @@ class FacebookTest < ActiveSupport::TestCase
|
||||||
Setting.set('system_init_done', true)
|
Setting.set('system_init_done', true)
|
||||||
|
|
||||||
# needed to check correct behavior
|
# needed to check correct behavior
|
||||||
Group.create_if_not_exists(
|
group = Group.create_if_not_exists(
|
||||||
id: 2,
|
|
||||||
name: 'Facebook',
|
name: 'Facebook',
|
||||||
note: 'All Facebook feed posts.',
|
note: 'All Facebook feed posts.',
|
||||||
updated_by_id: 1,
|
updated_by_id: 1,
|
||||||
created_by_id: 1
|
created_by_id: 1
|
||||||
)
|
)
|
||||||
|
|
||||||
provider_key = 'CAACEdEose0cBAC56WJvrGb5avKbTlH0c7P4xZCZBfT8zG4nkgEWeFKGnnpNZC8xeedXzmqZCxEUrAumX245T4MborvAmRW52PSpuDiXwXXSMjYaZCJOih5v6CsP3xrZAfGxhPWBbI8dSoquBv8eRbUAMSir9SDSoDeKJSdSfhuytqx5wfveE8YibzT2ZAwYz0d7d2QZAN4b10d9j9UpBhXCCCahj4hyk9JQZD'
|
# account config
|
||||||
consumer_key = 'CAACEdEose0cBAHZCXAQ68snZBf2C7jT6G7pVXaWajbZCZAZAFWRZAVUb9FAMXHZBQECZBX0iL5qOeTsZA0mnR0586XTq9vYiWP8Y3qCzftrd9hnsP7J9VB6APnR67NEdY8SozxIFtctQA9Xp4Lb8lbxBmig2v5oXRIH513kImPYXJoCFUlQs0aJeZBCtRG6BekfPs5GPZB8tieQE3yGgtZBTZA3HI2TtQLZBNXyLAZD'
|
if !ENV['FACEBOOK_USER']
|
||||||
|
fail "ERROR: Need FACEBOOK_USER - hint FACEBOOK_USER='name:1234:access_token'"
|
||||||
|
end
|
||||||
|
user_name = ENV['FACEBOOK_USER'].split(':')[0]
|
||||||
|
user_id = ENV['FACEBOOK_USER'].split(':')[1]
|
||||||
|
user_access_token = ENV['FACEBOOK_USER'].split(':')[2]
|
||||||
|
|
||||||
|
if !ENV['FACEBOOK_PAGE']
|
||||||
|
fail "ERROR: Need FACEBOOK_PAGE - hint FACEBOOK_PAGE='name:1234:access_token'"
|
||||||
|
end
|
||||||
|
page_name = ENV['FACEBOOK_PAGE'].split(':')[0]
|
||||||
|
page_id = ENV['FACEBOOK_PAGE'].split(':')[1]
|
||||||
|
page_access_token = ENV['FACEBOOK_PAGE'].split(':')[2]
|
||||||
|
|
||||||
|
if !ENV['FACEBOOK_CUSTOMER']
|
||||||
|
fail "ERROR: Need FACEBOOK_CUSTOMER - hint FACEBOOK_CUSTOMER='name:1234:access_token'"
|
||||||
|
end
|
||||||
|
customer_name = ENV['FACEBOOK_CUSTOMER'].split(':')[0]
|
||||||
|
customer_id = ENV['FACEBOOK_CUSTOMER'].split(':')[1]
|
||||||
|
customer_access_token = ENV['FACEBOOK_CUSTOMER'].split(':')[2]
|
||||||
|
|
||||||
provider_page_name = 'Hansi Merkurs Hutfabrik'
|
|
||||||
provider_options = {
|
provider_options = {
|
||||||
adapter: 'facebook',
|
adapter: 'facebook',
|
||||||
auth: {
|
auth: {
|
||||||
access_token: provider_key
|
access_token: user_access_token
|
||||||
},
|
},
|
||||||
|
user: {
|
||||||
|
name: user_name,
|
||||||
|
id: user_id,
|
||||||
|
},
|
||||||
|
pages: [
|
||||||
|
{
|
||||||
|
'id' => page_id,
|
||||||
|
'name' => page_name,
|
||||||
|
'access_token' => page_access_token,
|
||||||
|
}
|
||||||
|
],
|
||||||
sync: {
|
sync: {
|
||||||
page: provider_page_name,
|
pages: {
|
||||||
group_id: 2,
|
page_id => { 'group_id' => group.id.to_s },
|
||||||
limit: 1,
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,34 +70,55 @@ class FacebookTest < ActiveSupport::TestCase
|
||||||
updated_by_id: 1,
|
updated_by_id: 1,
|
||||||
)
|
)
|
||||||
|
|
||||||
test 'pages' do
|
# check users account
|
||||||
|
test 'a - user account' do
|
||||||
provider_options_clone = provider_options
|
client = Facebook.new(user_access_token)
|
||||||
|
current_user = client.current_user
|
||||||
provider_options_clone[:sync].delete(:page)
|
assert_equal(user_id, current_user['id'])
|
||||||
|
assert_equal(user_name, current_user['name'])
|
||||||
facebook = Facebook.new( provider_options_clone )
|
|
||||||
|
|
||||||
pages = facebook.pages
|
|
||||||
|
|
||||||
page_found = false
|
|
||||||
pages.each { |page|
|
|
||||||
|
|
||||||
next if page[:name] != provider_page_name
|
|
||||||
page_found = true
|
|
||||||
}
|
|
||||||
|
|
||||||
assert( page_found, "Page lookup for '#{provider_page_name}'" )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'feed post to ticket' do
|
# check available pages
|
||||||
|
test 'b - available pages' do
|
||||||
|
client = Facebook.new(user_access_token)
|
||||||
|
page_found = false
|
||||||
|
client.pages.each {|page|
|
||||||
|
next if page[:name] != page_name
|
||||||
|
page_found = true
|
||||||
|
assert_equal(page_id, page[:id])
|
||||||
|
assert_equal(page_name, page[:name])
|
||||||
|
}
|
||||||
|
assert(page_found, "Page lookup for '#{page_name}'")
|
||||||
|
end
|
||||||
|
|
||||||
consumer_client = Koala::Facebook::API.new( consumer_key )
|
# check access to pages
|
||||||
feed_post = "I've got an issue with my hat, serial number ##{rand(9999)}"
|
test 'c - page access' do
|
||||||
|
page_found = false
|
||||||
|
provider_options[:pages].each {|page|
|
||||||
|
client = Facebook.new(page['access_token'])
|
||||||
|
current_user = client.current_user
|
||||||
|
next if page['name'] != page_name
|
||||||
|
page_found = true
|
||||||
|
assert_equal(page_id, current_user['id'])
|
||||||
|
assert_equal(page_name, current_user['name'])
|
||||||
|
}
|
||||||
|
|
||||||
facebook = Facebook.new( provider_options )
|
assert(page_found, "Page lookup for '#{page_name}'")
|
||||||
|
end
|
||||||
|
|
||||||
post = consumer_client.put_wall_post(feed_post, {}, facebook.account['id'])
|
# check page account
|
||||||
|
test 'd - page account' do
|
||||||
|
client = Facebook.new(page_access_token)
|
||||||
|
current_user = client.current_user
|
||||||
|
assert_equal(page_id, current_user['id'])
|
||||||
|
assert_equal(page_name, current_user['name'])
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'e - feed post to ticket' do
|
||||||
|
|
||||||
|
customer_client = Koala::Facebook::API.new(customer_access_token)
|
||||||
|
message = "I've got an issue with my hat, serial number ##{rand(99_999)}"
|
||||||
|
post = customer_client.put_wall_post(message, {}, page_id)
|
||||||
|
|
||||||
# fetch check system account
|
# fetch check system account
|
||||||
Channel.fetch
|
Channel.fetch
|
||||||
|
@ -78,12 +127,19 @@ class FacebookTest < ActiveSupport::TestCase
|
||||||
article = Ticket::Article.find_by(message_id: post['id'])
|
article = Ticket::Article.find_by(message_id: post['id'])
|
||||||
|
|
||||||
assert(article, "article post '#{post['id']}' imported")
|
assert(article, "article post '#{post['id']}' imported")
|
||||||
assert_equal( article.body, feed_post, 'ticket article inbound body' )
|
assert_equal(article.from, customer_name, 'ticket article inbound body')
|
||||||
|
assert_equal(article.to, page_name, 'ticket article inbound body')
|
||||||
|
assert_equal(article.body, message, 'ticket article inbound body')
|
||||||
assert_equal(1, article.ticket.articles.count, 'ticket article inbound count')
|
assert_equal(1, article.ticket.articles.count, 'ticket article inbound count')
|
||||||
assert_equal( feed_post, article.ticket.articles.last.body, 'ticket article inbound body' )
|
assert_equal(message, article.ticket.articles.last.body, 'ticket article inbound body')
|
||||||
|
|
||||||
|
# check customer
|
||||||
|
customer = article.ticket.customer
|
||||||
|
assert_equal('Bernd', customer.firstname)
|
||||||
|
assert_equal('Hofbecker', customer.lastname)
|
||||||
|
|
||||||
post_comment = "Any updates yet? It's urgent. I love my hat."
|
post_comment = "Any updates yet? It's urgent. I love my hat."
|
||||||
comment = consumer_client.put_comment(post['id'], post_comment)
|
comment = customer_client.put_comment(post['id'], post_comment)
|
||||||
|
|
||||||
# fetch check system account
|
# fetch check system account
|
||||||
Channel.fetch
|
Channel.fetch
|
||||||
|
@ -92,31 +148,34 @@ class FacebookTest < ActiveSupport::TestCase
|
||||||
article = Ticket::Article.find_by(message_id: comment['id'])
|
article = Ticket::Article.find_by(message_id: comment['id'])
|
||||||
|
|
||||||
assert(article, "article comment '#{comment['id']}' imported")
|
assert(article, "article comment '#{comment['id']}' imported")
|
||||||
|
assert_equal(article.from, customer_name, 'ticket article inbound body')
|
||||||
assert_equal(article.body, post_comment, 'ticket article inbound body')
|
assert_equal(article.body, post_comment, 'ticket article inbound body')
|
||||||
assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
|
assert_equal(2, article.ticket.articles.count, 'ticket article inbound count')
|
||||||
assert_equal(post_comment, article.ticket.articles.last.body, 'ticket article inbound body')
|
assert_equal(post_comment, article.ticket.articles.last.body, 'ticket article inbound body')
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'feed post and comment reply' do
|
test 'f - feed post and comment reply' do
|
||||||
|
|
||||||
consumer_client = Koala::Facebook::API.new( consumer_key )
|
customer_client = Koala::Facebook::API.new(customer_access_token)
|
||||||
feed_post = "I've got an issue with my hat, serial number ##{rand(9999)}"
|
feed_post = "I've got an issue with my hat, serial number ##{rand(99_999)}"
|
||||||
|
post = customer_client.put_wall_post(feed_post, {}, page_id)
|
||||||
facebook = Facebook.new( provider_options )
|
|
||||||
|
|
||||||
post = consumer_client.put_wall_post(feed_post, {}, facebook.account['id'])
|
|
||||||
|
|
||||||
# fetch check system account
|
# fetch check system account
|
||||||
Channel.fetch
|
Channel.fetch
|
||||||
|
|
||||||
# check if first article has been created
|
|
||||||
article = Ticket::Article.find_by(message_id: post['id'])
|
article = Ticket::Article.find_by(message_id: post['id'])
|
||||||
|
ticket = article.ticket
|
||||||
|
assert(article, "article post '#{post['id']}' imported")
|
||||||
|
|
||||||
reply_text = "What's your issue Bernd?"
|
# check customer
|
||||||
|
customer = ticket.customer
|
||||||
|
assert_equal('Bernd', customer.firstname)
|
||||||
|
assert_equal('Hofbecker', customer.lastname)
|
||||||
|
|
||||||
# reply via ticket
|
# reply via ticket
|
||||||
|
reply_text = "What's your issue Bernd?"
|
||||||
outbound_article = Ticket::Article.create(
|
outbound_article = Ticket::Article.create(
|
||||||
ticket_id: article.ticket.id,
|
ticket_id: ticket.id,
|
||||||
body: reply_text,
|
body: reply_text,
|
||||||
in_reply_to: post['id'],
|
in_reply_to: post['id'],
|
||||||
type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
|
type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
|
||||||
|
@ -126,19 +185,22 @@ class FacebookTest < ActiveSupport::TestCase
|
||||||
created_by_id: 1,
|
created_by_id: 1,
|
||||||
)
|
)
|
||||||
assert(outbound_article, 'outbound article created')
|
assert(outbound_article, 'outbound article created')
|
||||||
|
assert_equal(outbound_article.from, 'Hansi Merkurs Hutfabrik', 'ticket article outbound count')
|
||||||
assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count')
|
assert_equal(outbound_article.ticket.articles.count, 2, 'ticket article outbound count')
|
||||||
|
|
||||||
post_comment = 'The peacock feather is fallen off.'
|
post_comment = 'The peacock feather is fallen off.'
|
||||||
comment = consumer_client.put_comment(post['id'], post_comment)
|
comment = customer_client.put_comment(post['id'], post_comment)
|
||||||
|
|
||||||
# fetch check system account
|
# fetch check system account
|
||||||
Channel.fetch
|
Channel.fetch
|
||||||
|
|
||||||
reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
|
article = Ticket::Article.find_by(message_id: comment['id'])
|
||||||
|
assert(article, "article comment '#{comment['id']}' imported")
|
||||||
|
|
||||||
# reply via ticket
|
# reply via ticket
|
||||||
|
reply_text = "Please send it to our address and add the ticket number #{article.ticket.number}."
|
||||||
outbound_article = Ticket::Article.create(
|
outbound_article = Ticket::Article.create(
|
||||||
ticket_id: article.ticket.id,
|
ticket_id: ticket.id,
|
||||||
body: reply_text,
|
body: reply_text,
|
||||||
in_reply_to: comment['id'],
|
in_reply_to: comment['id'],
|
||||||
type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
|
type: Ticket::Article::Type.find_by(name: 'facebook feed comment'),
|
||||||
|
@ -147,8 +209,9 @@ class FacebookTest < ActiveSupport::TestCase
|
||||||
updated_by_id: 1,
|
updated_by_id: 1,
|
||||||
created_by_id: 1,
|
created_by_id: 1,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert(outbound_article, 'outbound article created')
|
assert(outbound_article, 'outbound article created')
|
||||||
|
assert_equal(outbound_article.from, 'Hansi Merkurs Hutfabrik', 'ticket article outbound count')
|
||||||
assert_equal(outbound_article.ticket.articles.count, 4, 'ticket article outbound count')
|
assert_equal(outbound_article.ticket.articles.count, 4, 'ticket article outbound count')
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -187,7 +187,7 @@ class TwitterBrowserTest < TestCase
|
||||||
)
|
)
|
||||||
|
|
||||||
# wait till new streaming of channel is active
|
# wait till new streaming of channel is active
|
||||||
sleep 35
|
sleep 40
|
||||||
|
|
||||||
# start tweet from customer
|
# start tweet from customer
|
||||||
client = Twitter::REST::Client.new do |config|
|
client = Twitter::REST::Client.new do |config|
|
||||||
|
|
Loading…
Reference in a new issue