telegram_spec.rb 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. require 'rails_helper'
  2. RSpec.describe 'Telegram', type: :request do
  3. describe 'request handling' do
  4. it 'does basic call' do
  5. Ticket.destroy_all
  6. # configure telegram channel
  7. token = 'valid_token'
  8. bot_id = 123_456_789
  9. group_id = Group.find_by(name: 'Users').id
  10. UserInfo.current_user_id = 1
  11. Channel.where(area: 'Telegram::Bot').destroy_all
  12. # try with invalid token
  13. stub_request(:post, 'https://api.telegram.org/botnot_existing/getMe')
  14. .to_return(status: 404, body: '{"ok":false,"error_code":404,"description":"Not Found"}', headers: {})
  15. expect do
  16. Telegram.check_token('not_existing')
  17. end.to raise_error(RuntimeError)
  18. # try valid token
  19. stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
  20. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
  21. bot = Telegram.check_token(token)
  22. expect(bot['id']).to eq(bot_id)
  23. stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
  24. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
  25. Setting.set('http_type', 'http')
  26. expect do
  27. Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  28. end.to raise_error(RuntimeError)
  29. # try invalid port
  30. stub_request(:post, "https://api.telegram.org:443/bot#{token}/getMe")
  31. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
  32. stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
  33. .with(body: { 'url' => "https://somehost.example.com:12345/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
  34. .to_return(status: 400, body: '{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: Webhook can be set up only on ports 80, 88, 443 or 8443"}', headers: {})
  35. Setting.set('http_type', 'https')
  36. Setting.set('fqdn', 'somehost.example.com:12345')
  37. expect do
  38. Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  39. end.to raise_error(RuntimeError)
  40. # try invalid host
  41. stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
  42. .with(body: { 'url' => "https://somehost.example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
  43. .to_return(status: 400, body: '{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: getaddrinfo: Name or service not known"}', headers: {})
  44. Setting.set('fqdn', 'somehost.example.com')
  45. expect do
  46. Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  47. end.to raise_error(RuntimeError)
  48. # valid token, host and port
  49. stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
  50. .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
  51. .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {})
  52. Setting.set('fqdn', 'example.com')
  53. channel = Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  54. UserInfo.current_user_id = nil
  55. # start communication #1
  56. post '/api/v1/channels/telegram_webhook', params: read_message('personal1_message_start'), as: :json
  57. expect(response).to have_http_status(:not_found)
  58. post '/api/v1/channels_telegram_webhook/not_existing', params: read_message('personal1_message_start'), as: :json
  59. expect(response).to have_http_status(:unprocessable_entity)
  60. expect(json_response['error']).to eq('bot params missing')
  61. callback_url = "/api/v1/channels_telegram_webhook/not_existing?bid=#{channel.options[:bot][:id]}"
  62. post callback_url, params: read_message('personal1_message_start'), as: :json
  63. expect(response).to have_http_status(:unprocessable_entity)
  64. expect(json_response['error']).to eq('invalid callback token')
  65. callback_url = "/api/v1/channels_telegram_webhook/#{channel.options[:callback_token]}?bid=#{channel.options[:bot][:id]}"
  66. post callback_url, params: read_message('personal1_message_start'), as: :json
  67. expect(response).to have_http_status(:ok)
  68. # send message1
  69. post callback_url, params: read_message('personal1_message_content1'), as: :json
  70. expect(response).to have_http_status(:ok)
  71. expect(Ticket.count).to eq(1)
  72. ticket = Ticket.last
  73. expect(ticket.title).to eq('Hello, I need your Help')
  74. expect(ticket.state.name).to eq('new')
  75. expect(ticket.articles.count).to eq(1)
  76. expect(ticket.articles.first.body).to eq('Hello, I need your Help')
  77. expect(ticket.articles.first.content_type).to eq('text/plain')
  78. # send channel message1
  79. post callback_url, params: read_message('channel1_message_content1'), as: :json
  80. expect(response).to have_http_status(:ok)
  81. expect(Ticket.count).to eq(1)
  82. ticket = Ticket.last
  83. expect(ticket.title).to eq('Hello, I need your Help')
  84. expect(ticket.state.name).to eq('new')
  85. expect(ticket.articles.count).to eq(1)
  86. expect(ticket.articles.first.body).to eq('Hello, I need your Help')
  87. expect(ticket.articles.first.content_type).to eq('text/plain')
  88. # edit channel message1
  89. post callback_url, params: read_message('channel2_message_content1'), as: :json
  90. expect(response).to have_http_status(:ok)
  91. expect(Ticket.count).to eq(1)
  92. ticket = Ticket.last
  93. expect(ticket.title).to eq('Hello, I need your Help')
  94. expect(ticket.state.name).to eq('new')
  95. expect(ticket.articles.count).to eq(1)
  96. expect(ticket.articles.first.body).to eq('Hello, I need your Help')
  97. expect(ticket.articles.first.content_type).to eq('text/plain')
  98. # send same message again, ignore it
  99. post callback_url, params: read_message('personal1_message_content1'), as: :json
  100. expect(response).to have_http_status(:ok)
  101. ticket = Ticket.last
  102. expect(ticket.title).to eq('Hello, I need your Help')
  103. expect(ticket.state.name).to eq('new')
  104. expect(ticket.articles.count).to eq(1)
  105. expect(ticket.articles.first.body).to eq('Hello, I need your Help')
  106. expect(ticket.articles.first.content_type).to eq('text/plain')
  107. # send message2
  108. post callback_url, params: read_message('personal1_message_content2'), as: :json
  109. expect(response).to have_http_status(:ok)
  110. ticket = Ticket.last
  111. expect(ticket.title).to eq('Hello, I need your Help')
  112. expect(ticket.state.name).to eq('new')
  113. expect(ticket.articles.count).to eq(2)
  114. expect(ticket.articles.last.body).to eq('Hello, I need your Help 2')
  115. expect(ticket.articles.last.content_type).to eq('text/plain')
  116. # send end message
  117. post callback_url, params: read_message('personal1_message_end'), as: :json
  118. expect(response).to have_http_status(:ok)
  119. ticket = Ticket.last
  120. expect(ticket.title).to eq('Hello, I need your Help')
  121. expect(ticket.state.name).to eq('closed')
  122. expect(ticket.articles.count).to eq(2)
  123. expect(ticket.articles.last.body).to eq('Hello, I need your Help 2')
  124. expect(ticket.articles.last.content_type).to eq('text/plain')
  125. # start communication #2
  126. post callback_url, params: read_message('personal2_message_start'), as: :json
  127. expect(response).to have_http_status(:ok)
  128. # send message1
  129. post callback_url, params: read_message('personal2_message_content1'), as: :json
  130. expect(response).to have_http_status(:ok)
  131. expect(Ticket.count).to eq(2)
  132. ticket = Ticket.last
  133. expect(ticket.title).to eq('Can you help me with my feature?')
  134. expect(ticket.state.name).to eq('new')
  135. expect(ticket.articles.count).to eq(1)
  136. expect(ticket.articles.first.body).to eq('Can you help me with my feature?')
  137. expect(ticket.articles.first.content_type).to eq('text/plain')
  138. # send message2
  139. post callback_url, params: read_message('personal2_message_content2'), as: :json
  140. expect(response).to have_http_status(:ok)
  141. expect(Ticket.count).to eq(2)
  142. ticket = Ticket.last
  143. expect(ticket.title).to eq('Can you help me with my feature?')
  144. expect(ticket.state.name).to eq('new')
  145. expect(ticket.articles.count).to eq(2)
  146. expect(ticket.articles.last.body).to eq('Yes of course! <b>lalal</b>')
  147. expect(ticket.articles.last.content_type).to eq('text/plain')
  148. # start communication #3
  149. post callback_url, params: read_message('personal3_message_start'), as: :json
  150. expect(response).to have_http_status(:ok)
  151. # send message1
  152. post callback_url, params: read_message('personal3_message_content1'), as: :json
  153. expect(response).to have_http_status(:ok)
  154. expect(Ticket.count).to eq(3)
  155. ticket = Ticket.last
  156. expect(ticket.title).to eq('Can you help me with my feature?')
  157. expect(ticket.state.name).to eq('new')
  158. expect(ticket.articles.count).to eq(1)
  159. expect(ticket.articles.last.body).to eq('Can you help me with my feature?')
  160. expect(ticket.articles.last.content_type).to eq('text/plain')
  161. # send message2
  162. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  163. .with(body: { 'file_id' => 'ABC-123VabcOcv123w0ABHywrcPqfrbAYIABC' })
  164. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123VabcOcv123w0ABHywrcPqfrbAYIABC","file_path":"abc123"}}', headers: {})
  165. stub_request(:get, "https://api.telegram.org/file/bot#{token}/abc123")
  166. .to_return(status: 200, body: 'ABC1', headers: {})
  167. post callback_url, params: read_message('personal3_message_content2'), as: :json
  168. expect(response).to have_http_status(:ok)
  169. expect(Ticket.count).to eq(3)
  170. ticket = Ticket.last
  171. expect(ticket.title).to eq('Can you help me with my feature?')
  172. expect(ticket.state.name).to eq('new')
  173. expect(ticket.articles.count).to eq(2)
  174. expect(ticket.articles.last.body).to match(/<img style="width:360px;height:327px;"/i)
  175. expect(ticket.articles.last.content_type).to eq('text/html')
  176. # send channel message 3
  177. post callback_url, params: read_message('channel1_message_content3'), as: :json
  178. expect(response).to have_http_status(:ok)
  179. expect(Ticket.count).to eq(3)
  180. ticket = Ticket.last
  181. expect(ticket.title).to eq('Can you help me with my feature?')
  182. expect(ticket.state.name).to eq('new')
  183. expect(ticket.articles.count).to eq(2)
  184. expect(ticket.articles.last.body).to match(/<img style="width:360px;height:327px;"/i)
  185. expect(ticket.articles.last.content_type).to eq('text/html')
  186. # send message3
  187. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  188. .with(body: { 'file_id' => 'AAQCABO0I4INAATATQAB5HWPq4XgxQACAg' })
  189. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AAQCABO0I4INAATATQAB5HWPq4XgxQACAg","file_path":"abc123"}}', headers: {})
  190. stub_request(:get, "https://api.telegram.org/file/bot#{token}/abc123")
  191. .to_return(status: 200, body: 'ABC2', headers: {})
  192. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  193. .with(body: { 'file_id' => 'BQADAgADDgAD7x6ZSC_-1LMkOEmoAg' })
  194. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123BQADAgADDgAD7x6ZSC_-1LMkOEmoAg","file_path":"abc123"}}', headers: {})
  195. post callback_url, params: read_message('personal3_message_content3'), as: :json
  196. expect(response).to have_http_status(:ok)
  197. expect(Ticket.count).to eq(3)
  198. ticket = Ticket.last
  199. expect(ticket.title).to eq('Can you help me with my feature?')
  200. expect(ticket.state.name).to eq('new')
  201. expect(ticket.articles.count).to eq(3)
  202. expect(ticket.articles.last.body).to match(/<img style="width:200px;height:200px;"/i)
  203. expect(ticket.articles.last.content_type).to eq('text/html')
  204. expect(ticket.articles.last.attachments.count).to eq(1)
  205. # send channel message 2
  206. post callback_url, params: read_message('channel1_message_content2'), as: :json
  207. expect(response).to have_http_status(:ok)
  208. expect(Ticket.count).to eq(3)
  209. ticket = Ticket.last
  210. expect(ticket.title).to eq('Can you help me with my feature?')
  211. expect(ticket.state.name).to eq('new')
  212. expect(ticket.articles.count).to eq(3)
  213. expect(ticket.articles.last.body).to match(/<img style="width:200px;height:200px;"/i)
  214. expect(ticket.articles.last.content_type).to eq('text/html')
  215. expect(ticket.articles.last.attachments.count).to eq(1)
  216. # update message1
  217. post callback_url, params: read_message('personal3_message_content4'), as: :json
  218. expect(response).to have_http_status(:ok)
  219. expect(Ticket.count).to eq(3)
  220. ticket = Ticket.last
  221. expect(ticket.title).to eq('Can you help me with my feature?')
  222. expect(ticket.state.name).to eq('new')
  223. expect(ticket.articles.count).to eq(3)
  224. expect(ticket.articles.last.body).to match(/<img style="width:200px;height:200px;"/i)
  225. expect(ticket.articles.last.content_type).to eq('text/html')
  226. expect(ticket.articles.last.attachments.count).to eq(1)
  227. expect(ticket.articles.first.body).to eq('UPDATE: 1231444')
  228. expect(ticket.articles.first.content_type).to eq('text/plain')
  229. # send voice5
  230. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  231. .with(body: { 'file_id' => 'AwADAgADVQADCEIYSZwyOmSZK9iZAg' })
  232. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AwADAgADVQADCEIYSZwyOmSZK9iZAg","file_path":"abc123"}}', headers: {})
  233. post callback_url, params: read_message('personal3_message_content5'), as: :json
  234. expect(response).to have_http_status(:ok)
  235. expect(Ticket.count).to eq(3)
  236. ticket = Ticket.last
  237. expect(ticket.title).to eq('Can you help me with my feature?')
  238. expect(ticket.state.name).to eq('new')
  239. expect(ticket.articles.count).to eq(4)
  240. expect(ticket.articles.last.content_type).to eq('text/html')
  241. expect(ticket.articles.last.attachments.count).to eq(1)
  242. # send channel message 4 with voice
  243. post callback_url, params: read_message('channel1_message_content4'), as: :json
  244. expect(response).to have_http_status(:ok)
  245. expect(Ticket.count).to eq(3)
  246. ticket = Ticket.last
  247. expect(ticket.title).to eq('Can you help me with my feature?')
  248. expect(ticket.state.name).to eq('new')
  249. expect(ticket.articles.count).to eq(4)
  250. expect(ticket.articles.last.content_type).to eq('text/html')
  251. expect(ticket.articles.last.attachments.count).to eq(1)
  252. # start communication #4 - with sticker
  253. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  254. .with(body: { 'file_id' => 'AAQDABO3-e4qAASs6ZOjJUT7tQ4lAAIC' })
  255. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AAQDABO3-e4qAASs6ZOjJUT7tQ4lAAIC","file_path":"abc123"}}', headers: {})
  256. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  257. .with(body: { 'file_id' => 'BQADAwAD0QIAAqbJWAAB8OkQqgtDQe0C' })
  258. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123BQADAwAD0QIAAqbJWAAB8OkQqgtDQe0C","file_path":"abc123"}}', headers: {})
  259. post callback_url, params: read_message('personal4_message_content1'), as: :json
  260. expect(response).to have_http_status(:ok)
  261. expect(Ticket.count).to eq(4)
  262. ticket = Ticket.last
  263. if Rails.application.config.db_4bytes_utf8
  264. expect(ticket.title).to eq('💻')
  265. else
  266. expect(ticket.title).to eq('')
  267. end
  268. expect(ticket.state.name).to eq('new')
  269. expect(ticket.articles.count).to eq(1)
  270. expect(ticket.articles.last.body).to match(/<img style="/i)
  271. expect(ticket.articles.last.content_type).to eq('text/html')
  272. expect(ticket.articles.last.attachments.count).to eq(1)
  273. # send channel message #5 with sticker
  274. post callback_url, params: read_message('channel1_message_content5'), as: :json
  275. expect(response).to have_http_status(:ok)
  276. expect(Ticket.count).to eq(4)
  277. ticket = Ticket.last
  278. if Rails.application.config.db_4bytes_utf8
  279. expect(ticket.title).to eq('💻')
  280. else
  281. expect(ticket.title).to eq('')
  282. end
  283. expect(ticket.state.name).to eq('new')
  284. expect(ticket.articles.count).to eq(1)
  285. expect(ticket.articles.last.body).to match(/<img style="/i)
  286. expect(ticket.articles.last.content_type).to eq('text/html')
  287. expect(ticket.articles.last.attachments.count).to eq(1)
  288. # start communication #5 - with photo
  289. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  290. .with(body: { 'file_id' => 'AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABNQoaI8BwR_z_2MFAAEC' })
  291. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABNQoaI8BwR_z_2MFAAEC","file_path":"abc123"}}', headers: {})
  292. post callback_url, params: read_message('personal5_message_content1'), as: :json
  293. expect(response).to have_http_status(:ok)
  294. expect(Ticket.count).to eq(5)
  295. ticket = Ticket.last
  296. expect(ticket.title).to eq('-')
  297. expect(ticket.state.name).to eq('new')
  298. expect(ticket.articles.count).to eq(1)
  299. expect(ticket.articles.last.body).to match(/<img style="/i)
  300. expect(ticket.articles.last.content_type).to eq('text/html')
  301. expect(ticket.articles.last.attachments.count).to eq(0)
  302. post callback_url, params: read_message('personal5_message_content2'), as: :json
  303. expect(response).to have_http_status(:ok)
  304. expect(Ticket.count).to eq(5)
  305. ticket = Ticket.last
  306. expect(ticket.title).to eq('Hello, I need your Help')
  307. expect(ticket.state.name).to eq('new')
  308. expect(ticket.articles.count).to eq(2)
  309. expect(ticket.articles.last.body).to match(/Hello, I need your Help/i)
  310. expect(ticket.articles.last.content_type).to eq('text/plain')
  311. expect(ticket.articles.last.attachments.count).to eq(0)
  312. end
  313. it 'with two bots and different groups' do
  314. Ticket.destroy_all
  315. # configure telegram channel
  316. token1 = 'valid_token1'
  317. token2 = 'valid_token2'
  318. bot_id1 = 123_456_789
  319. bot_id2 = 987_654_321
  320. group1 = create(:group)
  321. group2 = create(:group)
  322. UserInfo.current_user_id = 1
  323. Channel.where(area: 'Telegram::Bot').destroy_all
  324. Setting.set('http_type', 'https')
  325. Setting.set('fqdn', 'example.com')
  326. # channel 1 - try valid token
  327. stub_request(:post, "https://api.telegram.org/bot#{token1}/getMe")
  328. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id1},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot1\"}}", headers: {})
  329. bot1 = Telegram.check_token(token1)
  330. expect(bot1['id']).to eq(bot_id1)
  331. # channel 1 - valid token, host and port
  332. stub_request(:post, "https://api.telegram.org:443/bot#{token1}/setWebhook")
  333. .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id1}" })
  334. .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {})
  335. channel1 = Telegram.create_or_update_channel(token1, { group_id: group1.id, welcome: 'hi!' })
  336. # start communication #1
  337. callback_url1 = "/api/v1/channels_telegram_webhook/#{channel1.options[:callback_token]}?bid=#{channel1.options[:bot][:id]}"
  338. post callback_url1, params: read_message('personal1_message_start'), as: :json
  339. expect(response).to have_http_status(:ok)
  340. # send message1
  341. post callback_url1, params: read_message('personal1_message_content1'), as: :json
  342. expect(response).to have_http_status(:ok)
  343. expect(Ticket.count).to eq(1)
  344. ticket1 = Ticket.last
  345. expect(ticket1.title).to eq('Hello, I need your Help')
  346. expect(ticket1.state.name).to eq('new')
  347. expect(ticket1.articles.count).to eq(1)
  348. expect(ticket1.articles.first.body).to eq('Hello, I need your Help')
  349. expect(ticket1.articles.first.content_type).to eq('text/plain')
  350. expect(ticket1.articles.first.from).to eq('Test Firstname Test Lastname')
  351. expect(ticket1.articles.first.to).to eq('@ChrispressoBot1')
  352. # channel 2 - try valid token
  353. UserInfo.current_user_id = 1
  354. stub_request(:post, "https://api.telegram.org/bot#{token2}/getMe")
  355. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id2},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot2\"}}", headers: {})
  356. bot2 = Telegram.check_token(token2)
  357. expect(bot2['id']).to eq(bot_id2)
  358. # channel 2 - valid token, host and port
  359. stub_request(:post, "https://api.telegram.org:443/bot#{token2}/setWebhook")
  360. .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id2}" })
  361. .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {})
  362. channel2 = Telegram.create_or_update_channel(token2, { group_id: group2.id, welcome: 'hi!' })
  363. # start communication #1
  364. callback_url2 = "/api/v1/channels_telegram_webhook/#{channel2.options[:callback_token]}?bid=#{channel2.options[:bot][:id]}"
  365. post callback_url2, params: read_message('personal3_message_start'), as: :json
  366. expect(response).to have_http_status(:ok)
  367. # send message2
  368. post callback_url2, params: read_message('personal3_message_content1'), as: :json
  369. expect(response).to have_http_status(:ok)
  370. expect(Ticket.count).to eq(2)
  371. ticket2 = Ticket.last
  372. expect(ticket2.title).to eq('Can you help me with my feature?')
  373. expect(ticket2.state.name).to eq('new')
  374. expect(ticket2.articles.count).to eq(1)
  375. expect(ticket2.articles.first.body).to eq('Can you help me with my feature?')
  376. expect(ticket2.articles.first.content_type).to eq('text/plain')
  377. expect(ticket2.articles.first.from).to eq('Test Firstname2 Test Lastname2')
  378. expect(ticket2.articles.first.to).to eq('@ChrispressoBot2')
  379. end
  380. def read_message(file)
  381. JSON.parse(File.read(Rails.root.join('test', 'data', 'telegram', "#{file}.json")))
  382. end
  383. end
  384. end