# Code not connecting to twitch chat

**URL:** <https://staging-discuss.dev.twitch.com/t/code-not-connecting-to-twitch-chat/46109>\
**Category:** Chat and IRC\
**Created:** [June 15, 2023, 5:19pm UTC](https://staging-discuss.dev.twitch.com/t/code-not-connecting-to-twitch-chat/46109 "2023-06-15T17:19:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agent\_kittycat](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/agent_kittycat/32/25087_2.png) [@Agent\_kittycat](https://staging-discuss.dev.twitch.com/u/Agent_kittycat)\
**Post date:** [June 15, 2023, 5:19pm UTC](https://staging-discuss.dev.twitch.com/t/code-not-connecting-to-twitch-chat/46109/1 "2023-06-15T17:19:19Z")

</div>

hi, im making a chat reader on twitch and am trying to test it, but my connection test isn’t coming trough and my chat messages on twitch’s end aren’t being sent towards my program,

any1 know how to fix this?

also im trying to do this using my offline chat, does this create problems?

code: (C#)

```auto
using System;

using [System.IO](https://system.io/);

using TwitchLib.Client;

using [TwitchLib.Client.Events](https://twitchlib.client.events/);

using TwitchLib.Client.Models;

namespace Chatbot

{

class Program

{

private static TwitchClient twitchClient;

static void Main(string[] args)

{

var credentials = new ConnectionCredentials("agentkittycat", Environment.GetEnvironmentVariable("TWITCH_ACCESS_TOKEN"));

twitchClient = new TwitchClient();

twitchClient.Initialize(credentials, "agentkittycat");

twitchClient.OnMessageReceived += OnMessageReceived;

twitchClient.OnConnected += OnConnected;

twitchClient.Connect();

Console.ReadLine();

}

private static void OnMessageReceived(object sender, OnMessageReceivedArgs e)

{

if ([e.ChatMessage.Channel](https://e.chatmessage.channel/) == "#agentkittycat")

{

Console.WriteLine("i got a message!");

// Get the newest message

var newestMessage = e.ChatMessage.Message;

// Save the message to a text file

File.WriteAllText("chat.txt", newestMessage);

}

}

private static void OnConnected(object sender, OnConnectedArgs e)

{

twitchClient.SendMessage("#agentkittycat", "Hello, I'm your chatbot and I'm now connected!");

Console.WriteLine("Chatbot connected to Twitch chat.");

}

}

}

```

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [July 26, 2023, 1:37pm UTC](https://staging-discuss.dev.twitch.com/t/code-not-connecting-to-twitch-chat/46109/5 "2023-07-26T13:37:21Z")

</div>

Closed on July 26, 2023, 1:37pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
