-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
153 lines (129 loc) Β· 4.33 KB
/
Program.cs
File metadata and controls
153 lines (129 loc) Β· 4.33 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Text;
using System.Net.Http;
using System.Threading.Tasks;
using Telegram.Bot;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
//Version 1.0.0
namespace pokemonbot
{
class Program
{
public static string token = "Your_Telegram_Bot_Token_Here";
public static long chatid = "Your_Chat_ID_Here";
public static async Task<string> getstat(string pokemon)
{
try
{
string url = "https://pokeapi.co/api/v2/pokemon/" + pokemon.ToLower();
using var http = new HttpClient();
HttpResponseMessage resp = await http.GetAsync(url);
if (!resp.IsSuccessStatusCode)
{
return $"Pokemon '{pokemon}' not found!";
}
string json = await resp.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(json)) return "Null response from API.";
dynamic? data = JsonConvert.DeserializeObject(json);
if (data == null) return "Error deserializing JSON.";
StringBuilder sb = new StringBuilder();
string name = data.name != null ? (string)data.name : "unknown";
string id = data.id != null ? data.id.ToString() : "unknown";
string height = data.height != null ? data.height.ToString() : "unknown";
string weight = data.weight != null ? data.weight.ToString() : "unknown";
string types = "Unknown";
if (data.types != null)
{
var typeList = new List<string>();
foreach (var typeInfo in data.types)
{
if (typeInfo.type?.name != null)
{
typeList.Add(typeInfo.type.name.ToString());
}
}
types = string.Join(", ", typeList);
}
sb.AppendLine($"π **{char.ToUpper(name[0]) + name.Substring(1)}**");
sb.AppendLine($"π ID: {id}");
sb.AppendLine($"π Height: {height}");
sb.AppendLine($"βοΈ Weight: {weight}");
sb.AppendLine($"π― Types: {types}");
return sb.ToString();
}
catch (Exception ex)
{
return $"Error getting Pokemon data: {ex.Message}";
}
}
public static string RandomPokemonName()
{
Random rnd = new Random();
string[] pokemon =
{
"pikachu", "bulbasaur", "charmander", "squirtle", "eevee",
"mewtwo", "snorlax", "gengar", "dragonite", "charizard",
"venusaur", "blastoise", "meowth", "psyduck", "jigglypuff",
"machamp", "alakazam", "arbok", "raichu", "lapras",
"vaporeon", "jolteon", "flareon", "lucario", "greninja"
};
return pokemon[rnd.Next(pokemon.Length)];
}
public static async Task Main()
{
try
{
var bot = new TelegramBotClient(token);
Console.WriteLine("Bot started!");
bot.StartReceiving(UpdateHandler, ErrorHandler);
while (true)
{
await Task.Delay(1000);
}
}
catch (Exception ex)
{
Console.WriteLine($"Critical error: {ex}");
Console.ReadLine();
}
}
private static async Task UpdateHandler(ITelegramBotClient bot, Update update, CancellationToken ct)
{
if (update.Message?.Text != null)
{
long chatId = update.Message.Chat.Id;
string messageText = update.Message.Text.ToLower();
try
{
if (messageText == "get pokemon"){
string pokemonName = RandomPokemonName();
string pokemonInfo = await getstat(pokemonName);
await bot.SendMessage(
chatId: chatId,
text: pokemonInfo,
cancellationToken: ct
);
}
}
catch (Exception ex)
{
await bot.SendMessage(
chatId: chatId,
text: $"β Error: {ex.Message}",
cancellationToken: ct
);
}
}
}
private static Task ErrorHandler(ITelegramBotClient bot, Exception ex, CancellationToken ct)
{
Console.WriteLine($"Erorr: {ex.Message}");
return Task.CompletedTask;
}
}
}