-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_telegram_dev.rb
More file actions
92 lines (80 loc) · 2.73 KB
/
test_telegram_dev.rb
File metadata and controls
92 lines (80 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env ruby
require 'bundler/setup'
require_relative 'config/environment'
puts "🤖 TELEGRAM INTEGRATION DEVELOPMENT TESTER"
puts "=" * 50
# Check if bot is configured
token_present = Rails.application.credentials.telegram_bot_token.present?
puts "✅ Bot token configured: #{token_present}"
if !token_present
puts "❌ No bot token found. Add one to Rails credentials."
exit 1
end
# Test 1: Create/find test user
puts "\n1️⃣ Setting up test user..."
user = User.find_or_create_by(email_address: 'telegram_dev_test@example.com') do |u|
u.first_name = 'Dev'
u.last_name = 'Tester'
end
puts "✅ User: #{user.full_name} (#{user.email_address})"
# Test 2: Generate verification token
puts "\n2️⃣ Generating verification token..."
token = user.generate_telegram_verification_token
puts "✅ Token: #{token}"
puts "✅ Expires: #{user.telegram_verification_token_expires_at}"
puts "📱 To test manually, send this to your bot:"
puts " /start #{token}"
# Test 3: Test TelegramBotService initialization
puts "\n3️⃣ Testing TelegramBotService..."
service = TelegramBotService.new
puts "✅ Service initialized"
# Test 4: Simulate webhook processing
puts "\n4️⃣ Testing webhook processing..."
fake_chat_id = 555555555
fake_update = {
'update_id' => 12345,
'message' => {
'message_id' => 1,
'chat' => { 'id' => fake_chat_id },
'from' => { 'id' => fake_chat_id, 'username' => 'dev_tester' },
'text' => '/start'
}
}
puts "📨 Processing fake /start command..."
service.process_webhook(fake_update)
puts "✅ Webhook processing completed"
# Test 5: Link account simulation
puts "\n5️⃣ Simulating account linking..."
user.update!(
telegram_user_id: fake_chat_id.to_s,
telegram_username: 'dev_tester',
telegram_verification_token: nil,
telegram_verification_token_expires_at: nil
)
puts "✅ Account linked: #{user.telegram_linked?}"
puts "✅ Username: #{user.telegram_username}"
puts "✅ Notifications: #{user.telegram_notifications_enabled?}"
# Test 6: Test notification sending (will fail with fake chat_id, but shows the flow)
puts "\n6️⃣ Testing notification system..."
begin
result = service.send_notification(
user: user,
message_type: 'test',
content: 'Test notification from development'
)
if result
puts "✅ Notification sent successfully"
else
puts "⚠️ Notification failed (expected with fake chat_id)"
end
rescue => e
puts "⚠️ Notification error (expected): #{e.message}"
end
puts "\n🎉 DEVELOPMENT TESTING COMPLETE!"
puts "\n" + "=" * 50
puts "NEXT STEPS FOR REAL TESTING:"
puts "1. Visit http://localhost:3000/profile/edit"
puts "2. Generate a real token"
puts "3. Send /start <token> to your actual bot"
puts "4. Verify the account links successfully"
puts "=" * 50